1

I am trying to write a struct to a file, but am getting a segmentation fault at run time:

#include<stdio.h>

//just a struct for purposes of demonstration
struct my_struct{
  int prop1;
  int prop2;
};

//writes dummy struct to filename.dat
void writeStruct(){
  FILE *file_pointer;
  file_pointer = fopen("filename.dat","w");

  //define and assign variables to a quick dummy struct
  struct my_struct *this_struct;

  this_struct->prop1=0; //seg faults here!
  this_struct->prop2=1;

  //write struct to file
  fwrite(this_struct, sizeof(*this_struct), 1, file_pointer);

  fclose(file_pointer);

}

int main(){
  writeStruct();
  return 0;
}

Can anyone help me understand the seg fault and achieve the purpose of the program?

user1717828
  • 7,122
  • 8
  • 34
  • 59
  • Change `struct my_struct *this_struct;` to `struct my_struct *this_struct = new my_struct();`. – Der Kommissar May 17 '15 at 00:52
  • this line: 'struct my_struct *this_struct;' defines a pointer to 'la la' land. That is why the code invokes a seg fault event, because it tries to access memory in la la land for setting the struct fields and for writing the struct. 'new' does not work in C, that is a C++ reserved word. suggest simply defining an instance of the struct 'struct my_struct this_struct;' and then setting the fields similar to: 'this_struct.prop1 = 1;' – user3629249 May 17 '15 at 00:57

3 Answers3

3

You've only defined a pointer of the struct, not pointing to any memory. So just don't use a pointer:

...
  //define and assign variables to a quick dummy struct
  struct my_struct this_struct;

  this_struct.prop1=0;
  this_struct.prop2=1;

  //write struct to file
  fwrite(&this_struct, sizeof this_struct, 1, file_pointer);
...

Or since it's wanted that way use malloc to allocate the memory:

...
  //define and assign variables to a quick dummy struct
  struct my_struct *this_struct;

  this_struct = malloc(sizeof *this_struct);
  this_struct->prop1=0;
  this_struct->prop2=1;

  //write struct to file
  fwrite(this_struct, sizeof *this_struct, 1, file_pointer);
...

Don't forget to call free before the pointer goes out of reach.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • I'm new to C and trying to get my bearings with pointers and memory. Could you alternatively show how to correctly "point to memory" to create the struct dynamically? – user1717828 May 17 '15 at 00:33
  • Just added the dynamical part – tynn May 17 '15 at 00:40
  • Damn, @MattMcNabb deleted his comment. Do you have the link explaining why "casts are bad"? – user1717828 May 17 '15 at 01:49
  • [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – tynn May 17 '15 at 01:55
  • @user1717828 in general, casts are bad as they can hide compiler warning or error messages that would alert you to an error. Also they can make the code harder to read. Try to avoid them except as a last resort. – M.M May 17 '15 at 01:59
2

You didn't allocate memory for the struct, that's why you got a seg fault.

Also you need check the file_pointer before use it. if the file fail to open, you will also get into trouble.

Tim3880
  • 2,563
  • 1
  • 11
  • 14
1

You declared this_struct as a pointer but didn't initialize it (= new or malloc). The segfault happens because the uninitialized pointer is random.

You should either assign the pointer to a new or malloc object, or declare it as a non pointer and when you write it use &this_struct.

Ron Kuper
  • 817
  • 5
  • 14