Thread: Segmentation Fault in main()

  1. #1
    Registered User
    Join Date
    Jul 2008

    Segmentation Fault in main()

    Hi - I'm fairly new to C and just wrote a very simple generic queue. I wrote a small loop to test out my queue (add 1-5 to the queue and then print it), and it works fine. The problem is that I get a 'segmentation fault' message at the end of the run. I traced it using gdb and found that it occurs after the "return 0;" in my main function. I made sure I freed the memory for the queue and its nodes after their use, so I have no idea why this happens. Any help will be great - thanks!

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0
    Any source?

  3. #3
    Registered User
    Join Date
    Jun 2005
    The basic rule of program crashes is that symptoms (like seg fault) become visible at or after execution of the code that causes them. That's because the operating system sends the SEGV signal when it detects a problem: the only real guarantee you have is that a problem cannot be detected before the cause of it happens.

    Getting a seg fault after returning from main() is a result of some invalid operation on a pointer (dereferencing NULL, falling off the end of an array, dereferencing a dangling pointer, accessing something that has been freed, etc) in your program.

    Without seeing code, it is not possible to conclude anything else.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Attached is my code - I'm testing the queue in the tester.c function, where the seg fault happens. Thanks!
    Last edited by sa125; 07-27-2008 at 10:39 AM. Reason: Removed code so as to not confuse others.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > sprintf(s, "node %d", i);
    You've just trashed memory.
    Do not pass go, do not collect $200, proceed directly to segfault.

    Use an array, not an uninitialised pointer for s

    > pn->data = malloc(sizeof(item));
    > memcpy(pn->data, item,strlen((char *)item));
    1. You don't allocate enough space for the data (only the size of void*)
    2. You don't copy enough data with the memcpy - you forget the \0

    pn->data = malloc( strlen(item) + 1 );
    strcpy( pn->data, item );

    > /* Define helpful prints */
    > #define STOP 'STOP\n';
    Except if you ever tried to use it, you would need to replace all those ' ' with " "
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    The larch
    Join Date
    May 2006
    char *s;
    sprintf(s, "node %d", i);
    In main you are using an uninitialized pointer.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  2. Replies: 7
    Last Post: 12-10-2004, 01:58 AM
  3. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. segmentation fault before main
    By Cristian Negresco in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2002, 09:14 AM

Tags for this Thread