Dave's Brain

Browse - programming tips - zero struct

Date: 2004Aug22
Updated: 2005Jun1

Q.  It's such a chore setting each struct member to zero.
    Is there a faster/less tedious way?

A.  Use ZeroMemory() like this:

struct MyStruct
{
	int	a;
	float	b;
	char	c;

	MyStruct() // default constructor
	{
		ZeroMemory((LPBYTE)this, sizeof(*this));
	}
};

This works for *some* classes too but be very careful.
If you have virtual functions, it will klobber your vtable!
So only use on the most simple classes.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: