Thread: Segmentation fault?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Question Segmentation fault?

    Hello all, I'm a beginner in C.

    I want to implement a stack data structure.

    typedef struct stackNode
    {
    int value;
    struct stackNode *next;
    } stackElement;

    typedef stackElement *stackPtr;

    stackPtr newElement(void)
    {
    stackPtr element;
    element = (stackPtr)malloc(sizeof(stackElement));
    element->next = NULL;
    return(element);
    }

    in main function I declare it...

    int main(void)
    {
    stackPtr tmp;
    stackPtr stack = NULL;
    ....

    tmp = newElement();
    return 0;
    }

    why every time i try to use newElement()
    it gives a runtime error (Segmentation fault,
    core dumped)?
    And what does the error mean?

    Thanks for the help,
    Ronald

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    > 1. first off, the (stackPtr) cast is not necessary (and occasionally wrong) in ANSI-C. You do not need it, if you have #included stdlib.h, and if you haven't, then you'll get a warning.

    This is true for most compilers, but some will still give errors about casting from void *; not every compiler out there will allow you to leave the casting off.

  3. #3
    Unregistered
    Guest
    > This is true for most compilers, but some will still give errors
    > about casting from void *; not every compiler out there will
    > allow you to leave the casting off

    This is like saying "My program compiles without warnings... if I turn them off for the compiler! That supress errors flag is nice!"

    Quzah.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Angry

    Ok, let me get this straight...

    20 seconds ago, on my last post, I was signed in.
    Now, I reply to this thread, and suddenly I'm not signed in again?!
    Have I ever mentioned how much I hate the timer on this board?
    I thought so...

    Quzah.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    3
    Ok, I have changed my code ...

    stackPtr newElement(void)
    {
    stackPtr element;

    element = malloc(sizeof(stackElement));
    if ( element == NULL )
    {
    fprintf( stderr, "Out of memory" );
    exit(1);
    }

    element->next = NULL;
    return(element);

    }

    and alternate it with cast and no-cast with
    (stackPtr in front of malloc). But still give
    the seg fault error.
    Any other explanation?

    Thanks for the help
    Ronald

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    3
    I think it's not because of the malloc.
    Just declaring
    stackPtr tmp;

    will give me Seg fault error.

    Maybe there's something wrong with my structure?
    typedef struct stackNode
    {
    int value;
    struct stackNode *next;
    } stackElement;

    typedef stackElement *stackPtr;


    Thanks for the help,
    Ronald

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I think it's not because of the malloc.
    > Just declaring
    > stackPtr tmp;

    The problem is not with this. Just declaring a variable instance does _NOT_ give you a segfault. The error is elsewhere in your program.

    Defining a structure also will not give you a segfalt.

    Quzah.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    52
    Ok,

    I can't see why you are getting segmentation faults, for some reason sometimes if you put '#define MyMalloc mallc(x)' in there it fixes things, I have _NO_ idea why :/

    Um, I doubt this will make any difference, but also, if you are using Linux or FreeBSD or something, you can try a program called 'gdb'. Simply type 'gdb ./PathAndOrBinaryName' then 'rrun BinaryName'. When you get the seg fault, type 'back' in the console and you will get where the error occured
    - Daniel Wallace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM