Thread: Memory Problem - I think...

  1. #1
    Unregistered
    Guest

    Unhappy Memory Problem - I think...

    Hey everyone,

    I'm having this serious problem with this linke dlist implementation. It is susposed to be part of a flight reservtion system (basic introductory problem in C) - but everytime the execution of initiateNode begins, the program crashes, saying that "The instruction at xxxxxx referenced memory at xxxxxxx. The memory could not be written"

    I understand that this is a problem with reserved memory trying to be used, but I don't known how to fix it -

    Heres the parts of the code that are most likely the culprits!

    -------------------Code------------------------------

    //-------------------------- initiateNode --------------------------//

    /* This function initialises a node, allocates memory for the node,
    and returns a pointer to the new node. Name, Phone and Flight number
    are added at this stage. Uses add function to establish node
    on the actual linked list.*/

    struct node * initiateNode(char *name, char *flight_no, int phone)
    {
    struct node *ptr;
    ptr = (struct node *) malloc( 1, sizeof(struct node ) );
    //In case of an error, ptr set to NULL, so...
    if(ptr == NULL)
    return (struct node *) NULL;
    //Otherwise take passenger details and.
    else
    {
    strcpy(ptr->name, name);
    ptr->phone = phone;
    strcpy(ptr->flight_no, flight_no);
    return ptr;
    //This is pointer to the new node.
    }
    }


    //-------------------------- add --------------------------//

    /* This adds a node to the end of the list (doesn't fill in detials).*/
    void add(struct node *newNode)
    {
    if( head == NULL )
    {
    //If this is the first node...
    head = newNode;
    }
    (*end).next = newNode;
    (*newNode).next = NULL;
    // Set next field to signify the end of list
    end = newNode;
    // Make end to point to the last node
    }


    This is the implementation part - not all of it!


    case 2:
    printf("\nEnter name: ");
    scanf("%s", &name);
    printf("\nEnter phone number: ");
    scanf("%d", &phone);
    printf("\nCurrent Flights Available: ");
    for(i = 0; i < number; i++)
    {
    printf("%s\n", current_flight[i].flight_no);
    }//Ignore this part
    printf("\nEnter Flight number for passenger: ");
    scanf("%s", &flight_no);
    ptr = initiateNode(name, flight_no, phone);
    add(ptr);
    break;

    There is also two global variables

    struct node *head = NULL;
    struct node *end = NULL;


    _________________end code_________________

    I know its probably too much to ask, but does anyone have any ideas???

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If end==NULL then this line will fail -

    (*end).next = newNode;
    zen

  3. #3
    Unregistered
    Guest

    Talking

    So what should end be set to??

    I have tried removing these two global variables -
    But program won't compile if I do.

    struct node *head = NULL;
    struct node *end = xxxx; //???????

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can check whether end points to anything in a similar way to your test for whether head does. If so attach newnode to the head and end and not end->next.
    zen

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    To elaborate on Salem's answer, if you have:

    char * name;

    you CAN use it, but you need to malloc it before the copy. So, instead of just:

    strcpy(ptr->name, name);

    you need:

    ptr->name = (char *) malloc(strlen(name) + 1);
    strcpy(ptr->name, name);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with atl memory cleaning
    By Mariam1989 in forum Windows Programming
    Replies: 1
    Last Post: 11-11-2008, 12:35 PM
  2. Memory problem...?
    By Xzyx987X in forum Windows Programming
    Replies: 4
    Last Post: 06-30-2004, 05:02 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. memory allocation problem with 2 dimensional array
    By nano_nasa in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2002, 11:34 AM