Segmentation fault error

This is a discussion on Segmentation fault error within the C Programming forums, part of the General Programming Boards category; Dear All, I am getting Segmentation fault error for below code, how to erase the same. Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    32

    Segmentation fault error

    Dear All,

    I am getting Segmentation fault error for below code, how to erase the same.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    typedef struct {
    	char *buffer;
    
    }sample1;
    
    
    
    int main(void)
    {
    sample1 *dev;
    
    dev = ( sample1 * ) malloc(sizeof(sample1));
    
    
    printf("enter buffer string\n");
    strcpy(dev-> buffer, "ashok");
    
    printf("Now buffer string\n");
    
    printf(" %s\n",dev-> buffer);
    
    
    free(dev);
    
    
    	return 0;
    }
    Thanks ...

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char *buffer is not initialized pointer
    is coontains some random address - you cannot write there.

    Allocate some buffer using malloc, store result in the buffer member before copying there anything

    2. Do not cast malloc - see FAQ (you will see that you are missing stdlib.h include)
    3. indent your code consistently
    Last edited by vart; 03-01-2008 at 01:25 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    You did not allocate space for buffer, and you should #include <stdlib.h> for malloc().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    I tried with

    buffer = (char *) malloc(10);

    it leads me to other errors, anyone please send back working code


    Thanks in advance ...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    What is your current code? Have you incorporated the advice vart and I gave you?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    here is updated code

    Code:
    int main(void)
    {
    sample1 *dev;
    
    dev = ( sample1 * ) malloc(sizeof(sample1));
    
    buffer = (char *) malloc(10);   // showing error
    dev->buffer= (char *) malloc(10);   // showing error
    
    printf("enter buffer string\n");
    strcpy(dev-> buffer, "ashok");
    
    printf("Now buffer string\n");
    
    printf(" &#37;s\n",dev-> buffer);
    
    
    free(dev);
    free(buffer);
    
    	return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    There is no variable named buffer in main(). So you should remove the lines that have to do with buffer and keep those lines that have to do with dev and dev->buffer.

    I would expect it to be along these lines:
    Code:
    int main(void)
    {
        sample1 *dev;
    
        dev = malloc(sizeof(sample1));
        dev->buffer = malloc(10);
    
        printf("enter buffer string\n");
        strcpy(dev->buffer, "ashok");
    
        printf("Now buffer string\n");
        printf(" &#37;s\n", dev->buffer);
    
        free(dev->buffer);
        free(dev);
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You do not need to cast malloc. It's not an error, but it need not be there.
    You allocate memory for 'buffer' a variable that does not exist. Remove that statement.
    You must free 'dev->buffer' before freeing 'dev'. It's the same as if someone told you to throw out of your house the trash and then throw away the key to the house, and you throw away the key first.
    Try to indent a bit better, and it might be good idea to not put whitespace when accessing members of a struct through a pointer.
    Code:
    dev-> buffer; /* In expressions it might be an error */
    dev->buffer;  /* Will have enough precedence in most cases */
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,310
    Quote Originally Posted by xuftugulus View Post
    You do not need to cast malloc. It's not an error, but it need not be there.
    .... unless you are using a C++ compiler to compile your C code.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    here is the entire code,
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
    	char *buffer;
    
    }sample1;
    
    int main(void)
    {
    
    sample1 *dev;
    
    dev = ( sample1 * ) malloc(sizeof(sample1));
    dev->buffer =  malloc(10);
    
    printf("enter buffer string\n");
    strcpy(dev-> buffer, "ashok");
    
    printf("Now buffer string\n");
    
    printf(" &#37;s\n",dev-> buffer);
    
    
    free(dev->buffer);
    free(dev);
    
    	return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    here is the entire code
    Looks okay. All you are missing now is the #include <stdlib.h> for malloc and free, and of course the indentation.

    Also, if you want to cast malloc, be consistent and write:
    Code:
    dev = ( sample1* ) malloc(sizeof(sample1));
    dev->buffer = (char*) malloc(10);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    thanks all, for your great support

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by xuftugulus View Post
    it might be good idea to not put whitespace when accessing members of a struct through a pointer.
    Code:
    dev-> buffer; /* In expressions it might be an error */
    dev->buffer;  /* Will have enough precedence in most cases */
    I'm pretty sure there is no difference between those two statements.

  14. #14
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by robwhit View Post
    I'm pretty sure there is no difference between those two statements.
    Of course whitespace doesn't matter except at the three places where it matters.
    It just might be confusing to read when for example there are also variables with the same name as a struct member, and the thing is inside an expression.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What are those three places, out of curiosity? As far as I can think of at the moment, whitespace is only required to separate tokens that would be taken as one token were the whitespace removed; for example:
    Code:
    int x;
    return 0;
    (In other words, whitespace matters when separating alphanumeric characters.)

    Whitespace is also significant in the case of the preprocessor -- for example, preprocessor directives have to begin with a '#' that is the first non-alphanumeric character on the line, and they have to end on a newline, or perhaps the end of the file.

    And of course, whitespace also matters in strings, because if you removed that whitespace the string would change.

    I guess that's three places, actually. Maybe I just answered my own question.

    As for whitespace in this particular case -- I occasionally wrap lines at a place like this, if they are particularly long:
    Code:
    structure
        ->member;
    I suppose it is indeed just a matter of preference.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21