Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 } ;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...