Results 1 to 5 of 5

Thread: segmentation error using strcpy() in gcc

  1. #1
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu 6.10 Edgy

    segmentation error using strcpy() in gcc

    Can anyone explain what I am doing wrong in the create_node function below?
    I get the following error when I run the program through dbg :

    Program received signal SIGSEGV, Segmentation fault.
    0xb7e69723 in strcpy () from /lib/tls/i686/cmov/libc.so.6

    I assume I am using strcpy incorrectly, but I don't know why. The program implements a linked list of structures, originally written in Borland C++ builder and worked fine on windows. Any help would be much appreciated.

    Code:
       /* node for list of string 'variables' */
      typedef struct str_node
      {
         char *value;
         char name;
         struct str_node *next;
      } node;
    
    
      node* create_node(char *str, char *n)
      {
          node *p;
    
          p = (node*) malloc(sizeof(node));
          p->next = NULL;
          strcpy(p->value, str);   /* error arises here */
          p->name = *n;
    
          return p;
      } /* end create_node */
    Last edited by lee797; February 14th, 2007 at 11:26 PM.

  2. #2
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: segmentation error using strcpy() in gcc

    Before I comment on the error, let me say a few things. You stated that this was C++ code, but it looks suspiciously like C code to me. Is this a C or C++ project? You should post your code inside a code block to make it more readable. You shouldn't be using strcpy ever, but before posting solutions I would like to know which language, C or C++ you are trying to do this in, because the solution would differ based on the language.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  3. #3
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu 6.10 Edgy

    Re: segmentation error using strcpy() in gcc

    My apologies, it is indeed c code, however it was originally compiled in
    C++ Builder (as C) as that is the program we use in my college. Regarding the code
    readability - yes I just noticed that feature, sorry about that.
    Last edited by lee797; February 14th, 2007 at 10:58 PM.

  4. #4
    Join Date
    Jul 2005
    Beans
    1,535
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: segmentation error using strcpy() in gcc

    Ok, here is some code that works, followed by an explanation

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
     /* node for list of string 'variables' */
    typedef struct str_node
    {
       char *value;
       char name;
       struct str_node *next;
    } node;
    
    
    node* create_node(char *str, char n, int len)
    {
    
       node *p;
    
       p = (node*) malloc(sizeof(node));
       p->next = NULL;
       p->value = (char*) malloc(len * sizeof(char)); /* allocate room for the string */
       strncpy(p->value, str, len); /* use strncpy, not strcpy, for extra safety */
       (p->value)[len-1] ='\0'; /* null terminate for even more safety */
       p->name = n;
    
       return p;
    } /* end create_node */
    
    int main(void)
    {
       node* a_node = create_node("string value", 'a', 13);
       printf("a_node->value = %s \n", a_node->value);
       return 0;
    }
    The problem with your code is that you were not allocating space for the nodes string value before trying to set it. In the code I posted, I pass the size of the string as a third input to the create_node function, and I malloc the node's string based on that size. Then I use strncpy instead of strcpy to copy the string into the newly allocated space. The function strncpy is safer than strcpy because it will copy only N bytes instead of checking for a null terminator, but it isn't perfect. This is why on the subsequent line I null terminate the string for safety. This protects against someone passing a string larger than len, and doesn't hurt if the string is the correct length, since strings are null terminated anyways.

    Lastly, I changed the char n parameter to pass by value, since it is only a char and no need to complicate things by passing a pointer.

    I also added a small main just to test it out. And you can see that passing a string with length longer than len is still safe, since I null terminate it manually.

    If you have any other questions about the code, just post back.
    Last edited by hod139; February 14th, 2007 at 11:18 PM.
    When I invented the Web, I didn't have to ask anyone's permission.
    ~Tim Berners-Lee on Net Neutrality
    -------------------------------------
    Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).

  5. #5
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu 6.10 Edgy

    Re: segmentation error using strcpy() in gcc

    No questions, thanks for your help.

Bookmarks

Posting Permissions

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