eric
eric

Reputation: 454

linked list add node changes all nodes values instead of just one

Alright I've been working on this for hours now and can't figure out my issue. I have a double linked list and when I try and add a new node to it it will change all the values in the linked list.

right now this is what I have:

void createSub(sibs *root, char *name, int size) {
    if (root->subSibs == NULL) {
        root->subSibs = (sibs *)malloc(sizeof(sibs));
        root->subSibs->name = name;
        root->subSibs->time_stamp = createTimeStamp();
        root->subSibs->nextSib = NULL;
    }

    sibs *temp = root->subSibs;
    if (temp != NULL) {
        while(temp->nextSib != NULL)
            temp = temp->nextSib;
    }
    sibs *t = (sibs *)malloc(sizeof(sibs));

    t->name = name;
    t->time_stamp = createTimeStamp();
    t->nextSib = NULL;

    if(temp != NULL)
        temp->nextSib = t;

    printf("root->subSibs->name = %s\n", root->subSibs->name);
    (root->numSub)++;
}

this might not be perfect considering I've changed it a million times. Can someone tell me what I'm doing wrong here?

root is the root node I'm working with and subSibs is pointer to the linked list. What I am doing is adding a name and timestamp to each node in that linked list pointed to by root->subSibs.

What I get out is:

createSub(root, name1, size);

prints:

root->subSibs1: name1;

createSub(root, name2, size);

prints:

root->subSibs1: name2;
root->subSibs2: name2;

etc...

It will probably be some really really stupid mistake but any help would be amazing. I've been trying for hours and just need someone to tell me why it will change the names.

Also if I do strcpy(root->subSibsi->name, name); for i = 0-5; root->subSibs1->name prints garbage and then root->subSibs2->name prints correct name and then root->subSibs3->name prints garbage (same as subSibs1->name) and so on...

Upvotes: 0

Views: 1182

Answers (3)

tuxuday
tuxuday

Reputation: 3037

In your node, name is char *. In other words it can point to some arbitrary memory location. To begin with it has some garbage value, so it points to nothing from programmers point of view and cannot to be used to store value - which is what you did.

Now if we allocate a portion of memory, big enough to store string pointed by argument char *name , we can make your node's name to point to it. Once you do that you can copy argument name * to node name *

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409364

Without more information (like how you call the function) I guess that you have a single buffer (probably a char array) and that you pass the address to that same buffer for each call to createSubs. This will make every node you create to point to the same buffer, which will always contain the last string entered.

Upvotes: 1

cnicutar
cnicutar

Reputation: 182664

t->name = name;

You are copying pointers. Instead you likely want to duplicate the memory:

t->name = strdup(name);

Or:

t->name = malloc(strlen(name) + 1);
strcpy(t->name, name);

Upvotes: 4

Related Questions