views:

59

answers:

2

I get this:

LLIST *mylist[N];

Where N is the number of rows of the input file. Then mylist[i] is a pointer to the ith linked list.

I call a function

LLIST *list_add(LLIST **p, int i){
    LLIST *n;

    n = (LLIST *) malloc(sizeof(LLIST));

    if (n == NULL)
        return NULL;

    n->next = *p; /* the previous element (*p) now becomes the "next" element */
    *p = n;       /* add new empty element to the front (head) of the list */
    n->data = i;
    return p;
}

So in my Main I say something like

LLIST *mylist[N];
list_add(&mylist[0],1);
list_add(&mylist[0],2);
list_add(&mylist[1],3)
list_add(&mylist[1],4);
list_print(mylist[0]); // Print mylist[0]
list_print(mylist[1]); // Print mylist[1]

My Print_list function is:

Void *list_print(LLIST *n) {
    if (n == NULL){ 
        printf("list is empty\n");
    }

    while (n != NULL){
        printf("%d",n->data); 
        n = n->next;
    }
}

When I do list_print(mylist[0]), it prints out 2 1.

When I do list_print(mylist[1]), I get a segmentation fault.

What is going on?

+1  A: 

You don't seem to initialize the contents of the mylist array, and it might contain values that point at random memory locations. When just adding new elements this doesn't matter, new elements are inserted at the beginning, never accessing the invalid pointers. But when you try to print the list, the print function will follow the invalid pointer at the end of the list and produce a segmentation fault.

To avoid this add an initializer to the array declaration:

LLIST *mylist[N] = {NULL};

This will set all elements of mylist to NULL.

sth
Best guess with what we're been given, except that from http://stackoverflow.com/questions/2106691/c-issue-cant-figure-how-to-assign-pointer-to-beginning-of-list there seems to be a special initializing function for the first node.
dmckee
Well I have it initialized as LLIST mylist*[2]; Every Time I run this I get different results. It is now printing *mylist[1]; but when i print mylist[0]; it prints 2 1 1 (repeated 1);
MRP
LLIST *mylist[N] = {NULL}; fixed the issue I guess.. I need to do more testing. I'm not confident though.
MRP
@MRP: `LLIST *mylist[2]` *decalares* mylist to be a *uninitialized* two element array of `LLIST*`s. But at that point the values of `mylist[0]` and `mylist[1]` could be *anything*. Accessing them at that point (without setting) to something known invokes undefined behavior. You are at risk of nasal demons.
dmckee
Well I have tested it using this code and it does print out the correctly:int main(void){int i,j;LLIST *mylist[10] = {NULL};for(i=0; i< 10; i++){ for(j=0; j<10; j++) { list_add( }}for(i=0; i< 10; i++){ list_print(mylist[i]);}}I am a newbie at C programming but I understand concepts of programing... I dont know if I'm still at risk with the given code.
MRP
Crap...Sorry for the editing
MRP
So pretty much i get a print out like this...9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210
MRP
The test works, but am I still at risk?btw, Thank you all for your help!
MRP
This does workLLIST *mylist[10] = {NULL};But would if I wanted to do this I get errors:x=10;LLIST *mylist[x] = {NULL};
MRP
A: 

This does work LLIST *mylist[10] = {NULL}; But would if I wanted to do this I get errors:

x=10; LLIST *mylist[x] = {NULL};

MRP