R
roberts.noah
I ran across some code that called memset(this, 0, sizeof(*this)) in
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions and no constructor in Struct or
any of its internals. Code like this does not initialize the object to
0:
struct SOut {
int x;
struct SIn {
int y;
SIn : y() {} // or this could be a virtual function...
} in;
}
....
SOut out = SOut(); // initializes y but not x.
out = SOut(); // clears y but not x...
So, since using either approach breaks if given certain circumstances
the question of which is more correct seems unclear. Since this is a
performance issue the real question is which is the faster, memset(x,
0, sizeof(x)) or x = X()? Another question is if the above code should
be clearing the value of x...so long as SIn has no constructor or
virtual functions it works, but as soon as it does the default
constructor of SOut no longer clears x. What law in C++ governs this
behavior?
Is using memset immoral even if you KNOW that the object will never
have any virts? The only reason why I would think it might be is that
these structs have member functions...and calling memset on this
/seems/ icky. I can definately see an argument comming over this and I
would like to be sure of my position...and IANAL.
Thanks.
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions and no constructor in Struct or
any of its internals. Code like this does not initialize the object to
0:
struct SOut {
int x;
struct SIn {
int y;
SIn : y() {} // or this could be a virtual function...
} in;
}
....
SOut out = SOut(); // initializes y but not x.
out = SOut(); // clears y but not x...
So, since using either approach breaks if given certain circumstances
the question of which is more correct seems unclear. Since this is a
performance issue the real question is which is the faster, memset(x,
0, sizeof(x)) or x = X()? Another question is if the above code should
be clearing the value of x...so long as SIn has no constructor or
virtual functions it works, but as soon as it does the default
constructor of SOut no longer clears x. What law in C++ governs this
behavior?
Is using memset immoral even if you KNOW that the object will never
have any virts? The only reason why I would think it might be is that
these structs have member functions...and calling memset on this
/seems/ icky. I can definately see an argument comming over this and I
would like to be sure of my position...and IANAL.
Thanks.