Re: I'm confused about the space a class takes in a structure?
Victor Bazarov wrote:
Pep wrote:
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];
};
It makes a difference what kind of "methods" this class has. If it
doesn't have user-defined constructors, or virtual functions, it's
most likely a POD class, which means it's safe to use 'memcpy' on it.
typedef struct
{
foo fooVar;
char charArray[8];
} bar;
Tell us, why do you do the wicked typedef dance here? Why don't you
simply write
struct bar
{
foo fooVar;
char charArray[8];
};
?
The original code which runs over 100's of 1,000's of lines was
written by a non programmer! So that is the syntax he used and one
that I have ashamedly got used to doing on this project :(
He also did things like code a module for one entity of the business
over around 125,000 lines of code then did a complete copy of the
source files, just changing the names of the files and classes held in
them to handle another entity which differs minimally in function.
Makes a mockery of polymorphism, overloading and just about any other C
++ features you can name ROFL
Still the original author made a couple of million bucks for his
efforts so I guess it proves that clean code was not an issue to ones
earnings in the dot com era.
So if you do a sizeof(foo) you get back 8 as a result
OK, so your 'short' is 2 bytes long, most likely.
and a
sizeof(bar) returns 16.
It seems that the compiler adds no padding in the 'bar' objects.
However the class foo has methods as well.
Again, depends on what methods those are.
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?
If sizeof(foo) == sizeof(short[4]), there are no "internal members"
with which you need to concern yourself.
Well the sizes I am getting back from foo and bar are the same using
sizeof() but foo does have a couple of ctors and a couple of embedded
classes, here's a bit more of the class to show the relevant parts
class foo
{
public:
class ZeroDivide : public std::exception
{
const char * what() const throw() ;
};
class Overflow : public std::exception
{
const char * what() const throw() ;
};
foo(unsigned long ls = 0, unsigned long ms = 0);
foo(unsigned char c[8]);
unsigned short shortArray[4];
};
The compiler I'm using is GNU g++ version 3.3.5
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask