Thread: Segmentation Fault and linked lists

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Question Segmentation Fault and linked lists

    Hello, this is my first submission at this forum, hope you'll help ; )
    I wrote this program to see if I knew how to manipulate linked lists:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct List {
        int number;
        struct Lista *next;
    } list;
    
    main () {
        int p, i;
        lista *begin, *new;
            inicio->prox = NULL;
            scanf("%d", &p);
            new = ( list * ) malloc( sizeof( list ) );
            new->next = NULL;
            begin = new;
            for ( i = 0; i < p; i++ ) {
                new = ( list * ) malloc( sizeof( lista ) );
                new->number = i;
                new->next = begin;
                begin = new;
            }
            while ( new->next != NULL ) {
                printf("%d\n", (*new).number);
                new = new->next;
            }
            for ( i = 0; i < p; i++ ) {
                new = begin;
                begin = begin->next;
                free(new);
            }
        return 0;
    }
    It simply reads an integer 'n' from the keyboard and then generates a linked list with 'n' elements, with each element receiving, progressively, a number (from 0 to 'n') to "number". Then it prints each "number" and frees the allocated memory.
    When I try to compile it with gcc (I'm at a Fedora OS), it returns something like "Segmentation fault (kernel image recorded)" ("Falha de segmentacao (imagem do nucleo gravada).
    Can you tell me what's wrong?
    Thanks a lot!
    Last edited by Salem; 11-09-2010 at 12:57 PM. Reason: Added code tags - learn to use them yourself

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you don't use code tags nobody will read your code.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You aren't initializing inicio, so it's pointing to a garbage address. There is no valid object for which to set the prox pointer. Attempting to access invalid memory is causing your segmentation fault. Set inicio to NULL instead of inicio->prox at the top of the function.

    A couple notes. Declare main to return an int and don't cast your malloc results.

    Also, compile your programs with "gcc -ggdb ..." to generate debugging symbols. Then learn to use gdb to track down these problems. It's an amazing tool once you get a handle on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault with Linked Lists
    By mkylman in forum C Programming
    Replies: 1
    Last Post: 04-27-2010, 11:30 PM
  2. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 07:23 AM
  4. about linked lists
    By armin_miewes in forum C Programming
    Replies: 4
    Last Post: 01-08-2004, 11:17 AM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM

Tags for this Thread