Thread: C-string problem in linked lists

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    20

    C-string problem in linked lists

    hi,
    I have a linked list of struct that contains string and int.
    (name and price) the list is sorted by price (ascending order)
    i built a function that returns the name of a product by its place
    in the list.
    Code:
     
    char* get_name(pData data, int place)
    {
     int i;
     char* name;
     Node* temp=data->tail;
     
     for (i=1; i<place;i++)
      {
        if (temp->prev==NULL) return NULL;
        temp=temp->prev;
      }
    
      name=(char*)malloc(sizeof(char*)*strlen(temp->name));
      strcpy(name,temp->name);
      return name;
    }
    the function returns the correct pointer but after the malloc,
    the name of the last (data->tail) product in the list changes i.e.
    before the malloc: data->tail->name="A string with 24 chars"
    and after the malloc: data->tail->name="A string with 24 chars!" (or A instead of !)
    whats wrong with the code?
    thanks

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    name = (char *) malloc(sizeof(char *) * strlen(temp->name));
    Why all the casts? Read this.

    And you have not allocated enough space. You need to allow one more space on the call to malloc. Read your documentation for strlen, and you will find that strlen calculates the length of the string given to it, not including the terminating '\0' character.

    Also, you should check the return value of malloc. What happens if malloc fails and returns a null pointer? What would happen call strcpy and hand it a null pointer instead of an address to your newly located memory?

    I've got a funny feeling about this:

    Code:
     for (i=1; i<place;i++)
    How about posting a working example of the code so we can see it in action?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    ok i changed the malloc:
    Code:
     name = malloc(sizeof(char *) * (strlen(temp->name)+1));
    didn't help....
    the strange thing is that after the first time the malloc was executed,
    every string in the list with length of 24 characters was added an extra
    character in the end (first it was '!' now its '1')

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Again, how about posting an example of your working code so we can see what is going on when it runs?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    I attached the files...
    I work in unix enviroment so i attached also the makefile....
    not so sure it will work in windows....

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Look up every instance of a call to malloc in data.c - get rid of all the casts on the return of malloc. It's not necessary.

    Code:
    nd->name=(char*)malloc(sizeof(char)*strlen(name));
    sizeof(char) will always be 1. As well, you can see that you did not allocate enough space for the terminating '\0'. Fix that and see what happens.

    Again, you should not be using the pointer returned from malloc until you check that value against NULL.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    thanks a lot !!!
    it worked.....

    how can you explain the why only a specific string in the list was changed?
    (no matter where in the list it was?)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM