🔒 Locked
sizeof(struct) vs. sizeof(class)
Advertisement
Original Post
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
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.
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.
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).
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 .
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.
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.
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.
|
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
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:
|
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.
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.
Then do not mix and match class and struct. 
If you want to eliminate dependencies, you might want to check out these useful articles:
http://www.gotw.ca/gotw/024.htm
http://www.gotw.ca/gotw/028.htm
Edited by - null_pointer on January 10, 2002 2:36:15 PM
If you want to eliminate dependencies, you might want to check out these useful articles:
http://www.gotw.ca/gotw/024.htm
http://www.gotw.ca/gotw/028.htm
Edited by - null_pointer on January 10, 2002 2:36:15 PM
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....
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....
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)
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();
//...
}
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?
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?
Since, after all, structs are just classes in which members are public by default, I have a better question: Why not use structs in C++?
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
Chris Barry (crbarry at mts.net)
My Personal Programming Depot
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.
|
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement