I disagree with unwind on the initilization of floats and pointers "that assumption doesn't hold at all" - well it holds most of the time.
It is pretty safe to assume float 0
is all zeros in my opinion, as you are likely to have IEEE 754 floats. See If all bits are 0, what is the value of IEEE floating point?
And it's pretty safe to assume a null pointer is zero i.e. NULL == 0
. Although also not guaranteed.
If you want to make sure of these assumptions and prove that in your environment that memset
is safe, then I propose the following:
void testFloatAssumptions()
{
const float fzero=0;
float f;
memset(&f,0,sizeof(f));
if(memcmp(&f,&fzero,sizeof(f))!=0)
{
//then you have a float problem w.r.t. memset(...,0,...)
}
}
void testPointerAssumptions()
{
const int* nullPointer=NULL;
int* pointer;
memset(&pointer,0,sizeof(pointer));
if(memcmp(&pointer,&nullPointer,sizeof(pointer))!=0)
{
//then you have a pointer problem w.r.t. memset(...,0,...)
}
}
The alternative is a lot of manual coding for situations which you are unlikely to come across.