Pattern: Wrong use of memset()
Issue: -
Use sizeof(varname)
when you take the size of a particular variable. sizeof(varname)
will update appropriately if someone changes the variable type either now or later. You may use sizeof(type)
for code unrelated to any particular variable, such as code that manages an external or internal data format where a variable of an appropriate C++ type is not convenient.
Example of incorrect code:
Struct data;
memset(&data, 0, sizeof(Struct));
Example of correct code:
Struct data;
memset(&data, 0, sizeof(data));