3 replies
by tehilinski
My understanding of the C and C++ standards is that member variables of a structure are not guaranteed to be contiguously allocated. For example:
struct RawData {
float x;
int n;
};
Here, if x and n are not contiguous, then memset will overwrite memory not allocated to the variables.
Also, on some hardware architectures, floats == 0.0 do not have zero in their machine representation, due to a bias value. In this case, filling an array of floats with NULL may not be the same (depending upon the implementation of memset) as assigning zero to the individual variables.
by john torjo
You are true: variables are not guaranteed to be contigously allocated. But in this case, you don't care about the "memory between" (the memory that is not used) - it can be initialized with zeros.
However, you're right about the float. Same applies to double/ long double. In such a (rare) case, you will need a different technique.
Thanks.
Best,
John
by achalk
Why not just:
struct fred f = {0};
Regards
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending