Memory allocation in linked lists

This is a discussion on Memory allocation in linked lists within the C Programming forums, part of the General Programming Boards category; I'm getting a classic example of printing out a linked list but all nodes contain the value of the last ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    12

    Memory allocation in linked lists

    I'm getting a classic example of printing out a linked list but all nodes contain the value of the last inserted node. I've seen some information about this , but I still cannot figure out how to apply it to my situation. I've attached my code and the test file I've been using. The output I've been getting when I print the linked list is something along the line of:

    markets -> markets -> markets -> markets -> (ad nauseum)

    It would be greatly appreciated if someone could take a look. I've been stuck on this issue for hours.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    newPtr->data = value;
    strcpy is for strings.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    I changed that line to

    strcpy(value,newPtr->data);

    and now I just get a segmentation fault. I didn't think that was an issue, since when I print the linked list, it shows strings at each node.. just the same string over and over.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    Woops, tried

    strcpy(newPtr->data,value)

    like it should be. Still get segmentation fault.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's just a pointer, you need to change it to an array.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    12
    Awesome!! You just helped me fix the biggest problem I had! Now, a new issue is coming up, though. Now the last item input into the linked list doesn't show up in the list. In the test file, "markets." doesn't appear in the end of the linked list. Any ideas?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    new code plz.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 12:29 PM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 04:06 PM
  3. Multiple Linked Lists w/HeadNode
    By tofugirl in forum C Programming
    Replies: 12
    Last Post: 12-10-2005, 10:21 AM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 07:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21