0

this is my student struct with the information I read in via binary

   typedef struct student_t {

     //data in here

     } student_t;

this is how I have declared them in main

double_linkedlist_t* listPtr;
student_t students;
node_t* node = NULLL;

this is how my file is initializzed

FILE *file;
printf("What file would you like to select?\n");
fgets( filename, MAX_NAME_LENGTH, stdin );
strtok(filename, "\n");
fflush(stdin);
file = fopen(filename, "rb+wb");

This is what my fwrite looks like in my main. I keep getting a segmentation fault and all the examples that I have seen on how to write a doubly linked list to a binary file have you do this. So I come here for an explanation on why or maybe some changes to what I have. I also have my doubly linked list declared as listPtr in main, and my node as node.

this is my Init

     node_t* Init_Node( student_t data ) 
     {
       node_t* node = (node_t*) malloc( sizeof( node_t ) );
       node -> students = data;
       node->nextPtr = NULL; 
       node->prevPtr = NULL;
       return node;
     }

this is my node struct along with the prototype to nodeinit

     typedef struct node_t
     {
       student_t students;
       struct node_t* nextPtr;
       struct node_t* prevPtr;
      } node_t;

     //prototypes
     node_t* Init_Node( student_t );


            node = listPtr->headPtr; //segmentation faults here
            while(node!=NULL)
            {
                fwrite(node, sizeof(student_t), 1, file);
                node=node->nextPtr; 
            }

any help on why this is happening would be greatly appreciated, I have never really written to a binary file with a doubly linked list :/.

CodeHero
  • 23
  • 9
  • how is `file` initialized? – AShelly Mar 10 '15 at 03:43
  • 4
    Most likely you're using a stray, `NULL` or otherwise illegal pointer. You need to provide more code to show us what you do, how you initialize the list, etc., please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Mar 10 '15 at 03:43
  • 1
    Also, I should point out that you should not expect that you can write memory pointers (like `node->nextPtr`) to disk and have them be meaningful when you read them back. – AShelly Mar 10 '15 at 03:45
  • Also, have you tried running in a debugger to really locate the line of the crash *in your code*? What are the values of the involved variables there? Do they look valid? – Some programmer dude Mar 10 '15 at 03:46
  • @JoachimPileborg is what i updated enough info? – CodeHero Mar 10 '15 at 03:50
  • @JoachimPileborg and yes i have and i put in my code where the fwrite is where it segmentation faults but i dont think,it should really be doing that – CodeHero Mar 10 '15 at 03:51
  • listPtr is very likely NULL – dvhh Mar 10 '15 at 03:52
  • 1
    Not really enough. You say the crash happens in the `node = listPtr->headPtr` expression, which indicates that `listPtr` is not correct. Have you checked it? – Some programmer dude Mar 10 '15 at 03:52
  • Also note: `fflush(stdin);` will set error `EBADF`. It does not work on input streams. See `man 3 fflush`. – David C. Rankin Mar 10 '15 at 03:57
  • @JoachimPileborg i set my doubly linked list to listPtr which i believe i could use headptr since thats in its struct. But i pur how i declared it which i could be wrong in how i did that. I have tested to see if the listptr headptr is there and it is. It creates a full list and it does set the headptr – CodeHero Mar 10 '15 at 04:00
  • How do you *initialize* the `listPtr` variable? – Some programmer dude Mar 10 '15 at 04:03
  • @JoachimPileborg as in my insertnode function? – CodeHero Mar 10 '15 at 04:07
  • Yes, but also how you initialize `listPtr` *before* calling your insert function for the first time. Do you set the pointer to `NULL` anywhere? Do you allocate memory for it somewhere? – Some programmer dude Mar 10 '15 at 04:13
  • @JoachimPileborg the only memory allocation i have is in my node initilization im guessing thats bad? and the only time i set listptr to NULL is listPtr-headPtr when i creat the list. – CodeHero Mar 10 '15 at 04:17

1 Answers1

1

Since you don't show a more complete code, I will only be able to guess, but the problems is this:

double_linkedlist_t* listPtr;

It seems this is a local variable declared inside the main function, and the problem with this is that local variables inside functions are not initialized. So by having the declaration and not making the pointer actually point somewhere, it will point to a seemingly random location.

Uninitialized local (non-static) variables will have an indeterminate value, and dereferencing the pointer without making it point anywhere valid will lead to undefined behavior which is a very common cause of crashes.

There are two possible solutions:

  1. Allocate memory and make the pointer point to it:

    double_linkedlist_t* listPtr = malloc(sizeof *listPtr);
    
  2. Don't declare it as a pointer to begin with, and when a pointer is needed use the address-of operator & to get one:

    double_linkedlist_t list;
    list.headPtr = NULL;
    list.tailPtr = NULL;
    
    ...
    
    callSomeFunction(&list);
    
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I made some fixes because i what you jad said there and it worked now i just gotta somehow get it to where i can write more than just one student in and delete properly. Thank you for your help! – CodeHero Mar 10 '15 at 04:35