Archived

This topic is now archived and is closed to further replies.

mkaltner

sizeof(struct) vs. sizeof(class)

Recommended Posts

mkaltner    122
Quick question. I''m implementing a file loading architecture and ran into something strange. Question: Is there an overhead when using classes? When crating the same thing in structure and class, I get a 4 byte difference. Is this correct or did I miss something? Thanks, - Mike

Share this post


Link to post
Share on other sites
Qoy    127
The extra 4 bytes is a pointer to the vtable. This will only be there if your class has virtual functions.

Share this post


Link to post
Share on other sites
mkaltner    122
quote:
Original post by Qoy
The extra 4 bytes is a pointer to the vtable. This will only be there if your class has virtual functions.


Hmm, you learn something every day.
Darn virtual deconstructor. =)

THANKS!

- Mike

Share this post


Link to post
Share on other sites
SteveC    122
Something to consider if you are storing structs to a file:

make sure you explicitly specify the structure packing in the header files which declare the struct

in VC++

#pragma pack(2) or something like that

struct
{
...
}


otherwise in the future if you change structure packing size or someone else tries to use your files they may not be able to properly read the structure from the file because their compiler setings may be packing the structure differently which will cause the data members to be read incorrectly.

Share this post


Link to post
Share on other sites
Stoffel    250
quote:
Original post by mkaltner
Darn virtual deconstructor. =)



2 things to note:
1) It''s destructor, not deconstructor. To deconstruct means to analyze. To destruct means to destroy.
2) Structs can have virtual functions too. The only difference between a class and a struct is the default access (private for class, public for struct).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Stoffel: You''re only partially correct. A struct in C++ is handled as a class with Defualt Public members... a struct in C is a struct that cannot contain member functions, but only data members. Just because MSVC handles structs as classes, doesn''t make it true for everything ;o).

Billy

Share this post


Link to post
Share on other sites
DrPizza    160
quote:
Original post by Anonymous Poster
Stoffel: You''re only partially correct. A struct in C++ is handled as a class with Defualt Public members... a struct in C is a struct that cannot contain member functions, but only data members. Just because MSVC handles structs as classes, doesn''t make it true for everything ;o).

Billy


Given that C doesn''t have a class keyword, you''ve failed to make any kind of a point.

C++ has both struct and class, and the only difference between the two is the default member access -- private for class, public for struct. That''s it .

Share this post


Link to post
Share on other sites
Stoffel    250
I was going to reply, but DrPizza did it for me. =)

There is one other slight difference, and I''m not really sure why it is (probably because of the default access), but you cannot inherit classes from structs and vice versa, nor can you forward-declare a class which is actually a struct nor vice-versa. I think this just annoys me more than anything (especially the latter).

So the only difference between structs and classes are the default access _and_ the fact that they are different types.

Share this post


Link to post
Share on other sites
Shannon Barber    1681
quote:
Original post by Stoffel
...but you cannot inherit classes from structs and vice versa...

I swear I've done this... I know I've had structs that inherited from other structs - and actually I know I've inherited struct from classes. I may not have ever tried to inherit a class from a struct though.

    
struct Packet
{
DWORD PacketID;
};
struct ChatPacket : public Packet
{
WORD wLength;
char* szChatter;
};

struct IMyInterface
{
virtual void DoSomething()=0;
};

class CMyObject : public IMyInterface
{
public:
virtual void DoSomething() {}

};

//don't think I've tried this:

class Stuff
{
int morestuff;
};
struct Zoid : public Stuff
{
};

class SeemsToWork : Zoid
{
int yetmorecrap;
};

Is that what you meant, or something else?

I think structs and class may default to different types of inheritence as well (if you don't specify the type), though I could be mistaken (that's something I just never do).

...
I just tested inheriting a struct from a class and it works fine on MSVC - and structs default to public inheritence whereas classes default to private.

So struct and classes are different types (never though of it like that before, but it seems true enough), default to different member declaration privacy, and default to different inheritence privacy. (What's the real word for privacy here? scope? access class?)

Edited by - Magmai Kai Holmlor on January 9, 2002 11:55:24 PM

Share this post


Link to post
Share on other sites
DrPizza    160
quote:
There is one other slight difference, and I''m not really sure why it is (probably because of the default access), but you cannot inherit classes from structs and vice versa, nor can you forward-declare a class which is actually a struct nor vice-versa. I think this just annoys me more than anything (especially the latter).

The former, no. MSVC++ 7 disagrees, Comeau disagrees, g++ disagrees.

This code:
  
class CB
{
};

struct CD : public CB
{
};

struct SB
{
};

class SD : public SB
{
};

int main(int argc, char* argv[])
{
CB cb;
CD cd;
SB sb;
SD sd;
}

compiles cleanly (except for unused variable warnings).

The latter is just as well. It would be untidy to see a struct forward declaration and then have it implemented as a class.

Share this post


Link to post
Share on other sites
Stoffel    250
color me silly, I thought you couldn''t do that. The forward decl thing really pisses me off, though. My current crusade is eliminating dependencies, the first big step of which is to not #include other headers in header files, so I''m really into forward-declaring types that are arguments to class functions and passing by reference or pointer so that only the .cpp file needs the class/struct/typedef defined. However, you have to use exactly the right thing (class, struct, or typedef) or you won''t compile. It''s making my crusade a little difficult.

Share this post


Link to post
Share on other sites
ANSI2000    122
See the problem is that compilers dont follow the rules that where set for each language that beeing C and C++... I dont think anywhere in the ANSI C++ specs there such a type "struct". Unless am completly wrong!! If so tell me...

Why compiler allow you to do such things... Is because structs and classes are so alike that the compiler vendors decided to make them inter operable....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
nope, structs are definently in the C++ standard. Otherwise they wouldn''t be in The C++ Programming Language by Bjarne Stroustrup

p.s. everyone should buy that book

Share this post


Link to post
Share on other sites
Enjolras    122
Check 9.4 of the standard. I''ll quote a bit of it:

"A structure is a class defined with the class-key struct; its members
and base classes (_class.derived_) are public by default
(_class.access_)."

Nick



Share this post


Link to post
Share on other sites

A poster above told to use packing for file structs
like that:

#pragma pack(2)

wich means to use members of size that divides by 2 bytes.
Well, I think it''s a good idea to type

#pragma pack()

after that struct, to switch back to the pack value of
project settings/default for the rest of the code!

That could be a "slightly" speed gain on pentium+ CPUs :-)
(if project settings/default have 4 or 8 bytes)


Share this post


Link to post
Share on other sites
kupps    122
what's the point to using structs in c++?

if the only difference between a class and a struct is the default access method and u should be declaring the access method anyway, why not just use classes?


Edited by - kupps on January 10, 2002 7:21:03 PM

Share this post


Link to post
Share on other sites
Shannon Barber    1681
quote:
Original post by kupps
what''s the point to using structs in c++?

if the only difference between a class and a struct is the default access method and u should be declaring the access method anyway, why not just use classes?



So you don''t have to type public and a bunch of useless accesors for things that are really structures and not classes...

my favorite example:

struct CWaveFormat : WAVEFORMATEX
{
CDQuality();
RadioQuality();
//...
}

Share this post


Link to post
Share on other sites
bakery2k1    712
What''s the point in using structs in c++? There is none.
I would always declare things as classes, even:

class SIMPLE_VERTEX
{
public:
VECTOR3D position, normal
};

simply because if I do find myself wanting to add member functions, I can easily. How long can it take to type "public:" anyway?

Share this post


Link to post
Share on other sites
23yrold3yrold    941
I believe (don''t quote me) that a struct''s member variables are arranged in memory differently than a classes, specifically in the order they''re declared, to keep backwards compatibility with C. Classes don''t necessarily do that for whatever reason. It might also explain the OP query. This is just something I picked up once on comp.lang.c++ so I may be wrong; anyone else know anything about it?

Chris Barry (crbarry at mts.net)
My Personal Programming Depot

Share this post


Link to post
Share on other sites
null_pointer    289
IIRC, you are correct, but only if the struct does not contain any C++ features, like constructors, a destructor, member functions, overloaded operators and so forth. It is just a compatibility feature.

  
struct C { int data; };
struct CPP { int data; ~CPP() {} };
 
int main()
{
C c = { 5 }; // ok, no problem

CPP cpp = { 5 }; // error: cannot use struct initializer syntax

 
return 0;
}

Share this post


Link to post
Share on other sites