4 ms·
No, it is true. The point is that the code in the example is incompetently written, if you want to zero out a struct foo, you should memset sizeof(struct foo),
by fhars 7y ago
No, it is true. The point is that the code in the example is incompetently written, if you want to zero out a struct foo, you should memset sizeof(struct foo), not /* manually calculate what you think the size of foo will turn out to be */ ...
- acqq 7y ago> if you want to zero out a struct foo, you should memset sizeof(struct foo), not /* manually calculate what you think the size of foo will turn out to be Exactly. Any other approach than using sizeof(struct foo) is definitely wrong. What author tried to do with sizeof(char) * 2 never worked in C except by accident, and it was so even in 1975 when no modern standard existed and "undefined" wasn't misused.
- tumult 7y agoIt's fine to write this if you know it's OK. There's nothing inherently wrong with it, as far as C99 is concerned. Of course, if you want to zero the entire region of the struct, sizeof(MyGuy) is better. (Though, it may write to useless padding bytes that never get read... but, it might not be useless if you're a kernel and don't want to leak information via uninitialized padding!)