Thread: strtok(), strdup() error

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    strtok(), strdup() error

    I don't really know how to even begin solving this problem. Basically, I have a structure:

    Code:
    struct Items{
    char*desc;
    char*name;
    char*behave;
    struct Areas*loc;
    struct Items*next;
    struct Items*prev;
    USE_FUNCTION use;
    };
    I want to fill out the fields of the structure...

    Code:
    fgets (input, 255, file); if ((p = strchr(input, '\n')) != NULL) *p = '\0';
    			new=createitem("NULL", "NULL", currentarea);
    			name=strtok(input,","); 
    			desc=strtok(NULL, ",");
    			behavior=strtok(NULL, ","); 
    			new->name=strdup(name); 
    			new->desc=strdup(desc); 
    			new->behave=strdup(behavior); 
    			new->loc=&a[currentarea]; printf("%s", new->name);
    			new->prev=NULL; new->next=NULL; printf("%s", new->name);
    The problem here is that when I assigned new->prev and new->next to NULL, the value of new->name changes! Apparently it is resetting the value to NULL. I'm not sure why at all, but it is causing segmentation faults.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I changed the code slightly:
    Code:
    new->loc=&a[currentarea]; printf("%s", new->name);
    			new->prev=NULL; printf("%s", new->name);
    			new->next=NULL; printf("%s", new->name);
    If the name is "paperweight", then it will print "paperweightpape" rather than the expected "paperweightpaperweightpaperweight"

    This is the createitem() function:

    Code:
    Items*createitem(char*name, char*desc, int currentarea){
    
    Items*new=(Items*)malloc(sizeof(Items*));
    new->next=NULL;
    new->prev=NULL;
    new->behave=NULL;
    new->desc=NULL;
    printf("\nCreated: %p", new);
    return(new);
    
    }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I might have just fixed it by changing this line: [Items*new=(Items*)malloc(sizeof(Items*));} to

    Code:
    Items*new=(Items*)malloc(sizeof(Items));
    Could someone explain how exactly my code is affected by setting the malloc size to the size of a pointer. Obviously its the incorrect size, I just wonder what is happening.
    Last edited by Adam Rinkleff; 07-31-2011 at 05:24 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are more problems than you appear to realize...

    First... if you are using fgets to load a line from the file, the companion function would be sscanf() to burst out your data. strtok() is destructive.

    Second rather than using strdup() you should create actual char array[] space in your struct...
    Code:
    struct Items{
    char desc[64];
    char name[32];
    char behave[16];
    struct Areas*loc;
    struct Items*next;
    struct Items*prev;
    USE_FUNCTION use;
    };
    Then you can use sscanf() to load the struct directly...
    Code:
    fgets(input, 255, file)
    sscanf(%s,%s,%s", items.desc, items.name, items.behave)
    Believe me, it's going to work a lot better than what you have.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adam Rinkleff View Post
    I might have just fixed it by changing this line: [Items*new=(Items*)malloc(sizeof(Items*));} to

    Code:
    Items*new=(Items*)malloc(sizeof(Items));
    Could someone explain how exactly my code is affected by setting the malloc size to the size of a pointer. Obviously its the incorrect size, I just wonder what is happening.
    So if you allocate four bytes for an object, and then try to write 437 bytes to that object, where are the other 433 bytes going to go? (Hint: on top of other things, is where.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strdup
    By darren78 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2010, 08:29 AM
  2. Strdup
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2010, 12:52 PM
  3. strtok Error
    By it01y2 in forum C Programming
    Replies: 2
    Last Post: 01-18-2010, 12:14 PM
  4. Replies: 15
    Last Post: 11-11-2007, 10:40 AM
  5. strdup
    By hankspears in forum C Programming
    Replies: 4
    Last Post: 05-09-2002, 02:02 PM