Re: I'm confused about the space a class takes in a structure?
* Pep:
I'm getting weird results at the moment so thought I'd rebase my
knowledge of c++ storage with your help :)
I have a class used as a type in a struct and the struct is handled by
a 3rd party binary writed for persistent storage. I am confused by the
results I am getting and am not sure how the 3rd party writer is
seeing the size of the stuct.
class foo
// I have not included the methods for brevity
{
public:
unsigned short shortArray[4];
};
typedef struct
{
foo fooVar;
char charArray[8];
} bar;
So if you do a sizeof(foo) you get back 8 as a result and a
sizeof(bar) returns 16. However the class foo has methods as well. Now
if I was to do any form of binary copy on bar such as using memcpy for
example using the sizeof(bar) as the length, will I definitely be
copying the complete data in bar or will the operation be thrown out
by things like internal members in foo created by the compiler, like
the vtbl in foo?
TIA :)
Binary level copying is ungood for serializing pointer values (do you
understand why?), and hence also for instances of a class with one or
more virtual member functions (hence, in practice, vtable pointer).
Use some other method of serialization; this is discussed at length in
the FAQ, although not as clearly as I'd wished.
And in general, don't use memcpy etc., and more importantly, to avoid a
lot of such problems, simply stop thinking about micro-optimizations:
they're evil evil evil. An incorrect program can be as fast as you want
since it doesn't need to do anything, but generally that's not what you
ultimately want. So, think first correctness, not micro-optimization.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?