-
April 10th, 2012, 08:50 AM
#1
is this a valid use of memset?
This is some code I've inherited in a project that I need to modify - the original was done in Borland C++ 3.1 in the DOS days. I don't use memset, so I'm wondering if this is valid. The point of it is to initialize the contents of an array of structures to zero:
Code:
struct hsdata
{
unsigned long wgpin;
unsigned long pxpin;
unsigned long iclasspin;
char iclass_csn[21];
unsigned int hsindex;
};
void some_function()
{
struct hsdata hs_unsorted[2400]; // array of structures
int z;
// Clear hsdata structure
for(z = 0; z < 2400; z++)
{
memset(&hs_unsorted[z],0,sizeof(hsdata));
}
}
Is this an OK thing to do?
Thanks
-
April 10th, 2012, 09:21 AM
#2
Re: is this a valid use of memset?
Though this is not exactly the way C++ snobs recommend, you can do this, since hsdata is POD type. If you use memset, just zero the whole array with one memset operation:
Code:
memset(&hs_unsorted[0], 0, sizeof(hs_unsorted));
C++ way is to add default constructor to the structure and initialize all members there.
-
April 10th, 2012, 10:40 AM
#3
Re: is this a valid use of memset?
Thanks Alex. I had forgotten that I can put a constructor in the struct. That's what I'll do.
-
April 10th, 2012, 11:19 AM
#4
Re: is this a valid use of memset?
Originally Posted by Dave C
Is this an OK thing to do?
Using memset in this way makes the assumption that a bit pattern of all zeros is equivalent to explicitly setting the relevant variable/member type to 0. I.e it assumes that this code
Code:
variableType variableName = 0;
results in a bit pattern of all zeroes in the memory used by 'variableName'. This is guaranteed to be the case (in C/C++) only for:- character types (char, and the signed/unsigned variants)
- integer types (int, unsigned long, signed short etc.)
- stuctures containing only members which can be safely initialized (i.e. no member functions)
- arrays of a type which is safe to initialize (including arrays of structures)
floats, doubles and pointers are usually safe, but are not guaranteed to be safe by the language specifications. That is, a NULL pointer, or a float or double of value 0.0 may not be represented internally by a zero bit pattern. They usually are, but to write fully portable code you shouldn't assume this.
Your hsdata structure (and the array) is covered by this, so you can safely use memset.
But, a better way would be to either add a default constructor (as Alex F said) if it is C++ or use an initializer (which works for C also):
Code:
struct hsdata
{
unsigned long wgpin;
unsigned long pxpin;
unsigned long iclasspin;
char iclass_csn[21];
unsigned int hsindex;
};
void some_function()
{
struct hsdata hs_unsorted[2400] = {0}; // array of structures
...
}
This initializes every member of each element of the array as if you had initalized them in a constructor using 'variableName = 0/0.0/NULL'
-
April 11th, 2012, 08:14 AM
#5
Re: is this a valid use of memset?
-
April 11th, 2012, 08:31 AM
#6
Re: is this a valid use of memset?
Another problem with the memset approach is that at some point you may add a non-POD to your struct, and code somewhere else in the program that you have no knowledge of will call memset and break your struct's integrity. That can be pretty hard to find.
-
April 11th, 2012, 08:47 AM
#7
Re: is this a valid use of memset?
Originally Posted by Peter_B
[...]or use an initializer
or even better:
Code:
hsdata hs_unsorted[2400] = {};
in c++11, this will work with any type ( POD or non POD ) by zero-initializing elements ( which basically tantamounts to recursively default construct and initialize to zero non-pod and pod members, respectively ). That said, AFAIK only gcc supports this at the moment ...
Originally Posted by GCDEF
That can be pretty hard to find.
again in c++11, you can use <typetraits> to test for pod-ness at compile time in proximity of such memset calls ...
-
April 11th, 2012, 09:00 AM
#8
Re: is this a valid use of memset?
Originally Posted by superbonzo
again in c++11, you can use <typetraits> to test for pod-ness at compile time in proximity of such memset calls ...
That doesn't help with legacy code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|