segmentation fault

Discussion in 'C Programming' started by dentzigui, Nov 11, 2006.

  1. dentzigui

    dentzigui Guest

    Hello, I hope to be clear. Forgive my english and forgive me if this
    question is trivial.

    struct _data {
    struct _data *next;
    char *item; };
    ....
    srtuct _data *last;

    last->next = (struct _data *) malloc (sizeof (struct _data)); ----> why
    this causes a segmentaion fault?

    thanx.
     
    dentzigui, Nov 11, 2006
    #1
    1. Advertising

  2. dentzigui

    Ian Collins Guest

    dentzigui wrote:
    > Hello, I hope to be clear. Forgive my english and forgive me if this
    > question is trivial.
    >

    You don't have to ask twice!

    --
    Ian Collins.
     
    Ian Collins, Nov 11, 2006
    #2
    1. Advertising

  3. dentzigui

    Bill Pursell Guest

    dentzigui wrote:
    > Hello, I hope to be clear. Forgive my english and forgive me if this
    > question is trivial.
    >
    > struct _data {
    > struct _data *next;
    > char *item; };
    > ...
    > srtuct _data *last;
    >
    > last->next = (struct _data *) malloc (sizeof (struct _data)); ----> why
    > this causes a segmentaion fault?


    At the time you make this call, last probably doesn't point to a valid
    memory location. Here is a piece of code that does what you want
    in 2 different ways. Understanding the differences will help.

    #include <stdlib.h>
    #include <stdio.h>

    struct data {
    struct data *next;
    char *item;
    };

    void
    print_list(struct data *list)
    {
    printf("%s\n", list->item);
    if (list->next != NULL)
    print_list(list->next);
    }

    void *
    initialize_entry(char *item)
    {
    struct data * ret;
    ret = malloc(sizeof *ret);
    if (ret == NULL) {
    perror(NULL);
    exit(EXIT_FAILURE);
    }
    ret->item = item;
    ret->next = NULL;

    return ret;
    }

    int
    main(void)
    {
    struct data head_array[1];
    struct data *head_ptr;

    head_ptr = initialize_entry("First item in the ptr list");
    head_ptr->next = initialize_entry("End of ptr list");

    head_array->item = "First item in the array.";
    head_array->next = initialize_entry("End of array list");

    print_list(head_array);
    print_list(head_ptr);

    return EXIT_SUCCESS;
    }
     
    Bill Pursell, Nov 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    885
  2. Pud
    Replies:
    0
    Views:
    602
  3. Replies:
    0
    Views:
    555
  4. Ivan Vecerina
    Replies:
    0
    Views:
    500
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,709
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page