Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 };?

Tags:

I'm reading about the initialized values by default of an array/struct and have this question:

is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?

if it's not, what's difference?

like image 628
Jack Avatar asked May 28 '12 18:05

Jack


2 Answers

is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?

No.

memset(&mystruct, 0, sizeof mystruct) ; 

... will tell the compiler to call a function that we expect will set during execution the data in mystruct to zero.

mystruct = { 0 }; 

... will set tell the compiler set by itself the data to zero, which means it will:

  • if possible, set the data in mystruct to zero at compilation (e.g. for static variables, as tristopia and Oli Charlesworth remarked in the comments)
  • or if not (e.g. auto variables), to generate the assembly code that will set the data to zero when the variable is initialized (which is better than calling a function to do that).

Note that perhaps the compiler could optimize the memset into a compile-time instruction (like replacing the first version with the second version), but I wouldn't rely on that as memset is a function from the runtime library, not some language intrinsic (I'm not a compiler writer/language lawyer, though).

Coming from C++, my own viewpoint is that the more you can do at compilation and the more the compiler knows at compile time, before the execution even starts, the better: It enables the compiler to possibly optimize the code and/or generate warning/errors.

In the current case, using the mystruct = { 0 }; notation to initialize a struct is always safer than using the memset because it is very very easy write the wrong thing in C with a memset without the compiler complaining.

The following examples show that it is easy for the code to do something different than it appears to do:

// only the 1st byte will be set to 0 memset(&mystruct, 0, sizeof(char)) ;            // will probably overrun the data, possibly corrupting // the data around it, and you hope, crashing the process. memset(&mystruct, 0, sizeof(myLARGEstruct)) ;   // will NOT set the data to 257. Instead it will truncate the // integer and set each byte to 1 memset(&mystruct, 257, sizeof(mystruct)) ;      // will set each byte to the value of sizeof(mystruct) modulo 256 memset(&mystruct, sizeof(mystruct), 0) ;        // will work. Always. mystruct = { 0 } ; 
like image 111
paercebal Avatar answered Oct 12 '22 10:10

paercebal


This is a completely pedantic answer, but given that the internal representation of a null pointer is not guaranted to be 0 the behavior of memset versus brace-initialization would differ (memset would do the wrong thing). That said, I've never heard of an implementation that took on this liberty to have a non all 0 bit pattern for null.

like image 20
K-ballo Avatar answered Oct 12 '22 11:10

K-ballo