Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I hav one structure like this struct linked_list { struct linked_list *next; char *data; }; struct linked_list *tmp = NULL; char *test_string = "some data"; tmp = malloc(sizeof(*tmp)); strncpy(tmp->data,test_string,strlen(test_string)); ...
  1. #1
    Linux Newbie
    Join Date
    Oct 2006
    Posts
    107

    linked list doubt

    Hi,

    I hav one structure like this

    struct linked_list {
    struct linked_list *next;
    char *data;
    };

    struct linked_list *tmp = NULL;
    char *test_string = "some data";
    tmp = malloc(sizeof(*tmp));


    strncpy(tmp->data,test_string,strlen(test_string));

    iam getting error segmentation fault. can any body help me .


    Thanks in Advance

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    tmp = malloc(sizeof(*tmp));
    must be:

    Code:
    tmp = malloc(sizeof(struct linked_list));
    You must also allocate memory for the member data of the struct.

    Regards

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    14

    linked list doubt

    Hi,
    You have to allocate memory of size struct linked list not of size *tmp because of sizeof(*tmp) allocates memory for the pointer not for the structure. That's why you are getting segmentation fault.
    Try the following
    tmp = malloc(sizeof(struct linked_list));

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by yugandhargv View Post
    Hi,
    You have to allocate memory of size struct linked list not of size *tmp because of sizeof(*tmp) allocates memory for the pointer not for the structure. That's why you are getting segmentation fault.
    Try the following
    tmp = malloc(sizeof(struct linked_list));
    Is this a vital tidbit of information that I neglected to mention earlier?

    Regards

  5. #5
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Shouldn't it be a good approach to type cast before assigning the bytes to tmp (structure pointer variable).


    Quote Originally Posted by Franklin52 View Post
    Is this a vital tidbit of information that I neglected to mention earlier?


    Regards
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    In C, casts from (void *) are made automatically. In C++, you would need to specifically make the cast.

    I suppose if it helps you to manually cast, it wouldn't hurt. But it's pretty rare to see it: we all know that malloc is allocating the memory for that structure.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    The main reason for the segfault is
    Code:
    strncpy(tmp->data,test_string,strlen(test_string));
    without allocating memory for tmp->data.

    The statement
    Code:
    tmp = malloc(sizeof(*tmp));
    is perfectly legal and valid.
    The Unforgiven
    Registered Linux User #358564

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...