CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    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

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  3. #3
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    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.

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: is this a valid use of memset?

    Quote Originally Posted by Dave C View Post
    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'

  5. #5
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: is this a valid use of memset?

    Thank you very much.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: is this a valid use of memset?

    Quote 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 ...

    Quote 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 ...

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: is this a valid use of memset?

    Quote Originally Posted by superbonzo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured