Re: inline constructor
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 10 Jan 2007 20:20:16 -0500
memset is incredibly dangerous to use here. You should do
struct DATATYPE
{
DATATYPE() { ::ZeroMemory(&u, sizeof(u)); }
union {
struct {...};
struct {...};
struct {...};
} u;
}
The use of m_ seems more than a little gratuitous here. If you have a structure named
followed by a . or ->, the only thing you will see is a member variable. The point of m_
was to distinguish member accesses from local variables and parameters in class methods,
and serves no purpose for a simple struct. (I don't think it serves a useful purpose
anyway, but that's another discussion).
The problem with memset is that you are presuming the layout, which is not safe. Since
you want to zero out the entire union, you should do that. If you were to modify the
union so that some subcomponent had more than 60 bytes, your code would not be reliable or
robust.
In debug mode, the constructor is called as a subroutine. Even in release mode, the
subroutine is generated, although the optimizer seems to collapse it into inline code at
the declaration site.
I tried both your example and my example, and both seem to erase 60 bytes of data, so I
see nothing that should cause it to fail. I determined this as you could have, by reading
the generated assembly code.
joe
On Wed, 10 Jan 2007 12:37:00 -0800, JD <jdt_young@xxxxxxxxx> wrote:
Hi,Joseph M. Newcomer [MVP]
Can anybody spot any error in the following inline constructor
DATATYPE()? m_data (in a union) should be ready to be set during the
constructing process even the constructor is inline, right? Just want
to double make sure. Thanks. JD
struct DATATYPE
{
DATATYPE() {memset(m_data, 0, 15*sizeof(float));}
union
{
struct
{
float m_d1;
float m_d2;
float m_d3;
float m_d4;
};
struct
{
float m_x1;
float m_y1;
float m_z1;
};
struct
{
float m_data[15];
};
};
};
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- inline constructor
- From: JD
- inline constructor
- Prev by Date: Re: inline constructor
- Next by Date: Re: How to run Visual C++ program on Another computer
- Previous by thread: Re: inline constructor
- Next by thread: Re: How to run Visual C++ program on Another computer
- Index(es):
Relevant Pages
|