Skip to main content
GameDev.net gamedev.net
🔒 Locked

sizeof problem

Started by TEUTON Mar 29, 2006 at 5:04 AM 17 replies 1.5k views
Original Post
TEUTON
TEUTON
Why does the compiler shows some size even if we don't have any members in the class?
demonkoryu
demonkoryu
Because to take the address of an object (even if it's a class with no data members), it must "be somewhere", take up some space, so the compiler reserves a minimal amount of memory for the class and reports it to you via sizeof.
rpg_code_master
rpg_code_master
I imagine that you are talking about doing something like this: -

#include <iostream>using namespace std;class CFoo{    public:        CFoo( void )        {        }        virtual ~CFoo( void )        {        }} // end class CFoovoid main( void ){    cout << "The size of CFoo is " << sizeof( CFoo ) << "." << endl;} // end main


The reason there is some size is because the actual methods themselves take up a little bit of memory. That is where the code inside of them is stored.
ProgramMax
ProgramMax
I don't think member functions increase the size of your class.

They are just like global, non-member functions. The only difference is that there is a hidden parameter that gets passed into the function. Wether you know it or not the first parameter is the 'this' parameter.

If your empty class has a size it is likely due to some sort of padding. Or something else...but not functions.
demonkoryu
demonkoryu
Quote:
Original post by rpg_code_master
The reason there is some size is because the actual methods themselves take up a little bit of memory. That is where the code inside of them is stored.


That's wrong. They don't take up memory inside the class. The only way how methods have anything to do with an object's size is the hidden pointer to the vtable (virtual function lookup table), if the class contains virtual functions.

BTW, (sorry can't resist :P) there has never been a void main().
And I am curious to know what an explicit void parameter should communicate...

PS: Even if I'm an a$%hole today... it's only because I'm sick.
moeron
moeron
virtual functions will definately lead to an increase in size.

[edit]
beat by Konfusius
moe.ron
Evil Steve
Evil Steve
Quote:
Original post by ProgramMax
I don't think member functions increase the size of your class.

They are just like global, non-member functions. The only difference is that there is a hidden parameter that gets passed into the function. Wether you know it or not the first parameter is the 'this' parameter.

If your empty class has a size it is likely due to some sort of padding. Or something else...but not functions.
virtual functions do. The class needs a vtable to handle virtual functions, which is the size of a pointer (I think).

But that's not the problem. Konfusius is correct. Otherwise you couldn't do this:
class CFoo{   // Empty class};void SomeFunc(CFoo* pFoo){   // Do something with pFoo}// Calling the function:CFoo theFoo;SomeFunc(&theFoo);  // <-- What is the address of the object if it has no size?

The C++ standard dictates that two different objects cannot have the same address, so you couldn't say "Well, all objects of size 0 should have address X" or anything.
Nitage
Nitage
Quote:
Original post by TEUTON
Why does the compiler shows some size even if we don't have any members in the class?


Quote:
ISO14882(C++ standard) section 9.3
Complete objects and member subobjects shall have nonzero size
rpg_code_master
rpg_code_master
Quote:
Original post by Konfusius
BTW, (sorry can't resist :P) there has never been a void main().
And I am curious to know what an explicit void parameter should communicate...

It works, I use it all the time.

I just double tested on Visual Studio 2005 here at work, and it runs fine.

#include <conio.h>#include <iostream>using namespace std;int main( void ){	cout << "hello!";	while ( !_kbhit( ) )		;        return 0;}
demonkoryu
demonkoryu
Yes it works (on some compilers), but it's not valid C++ nor C.
The C++ standard allows one of the following signatures:

int main()int main(int argc, char* argv[])


And why the extra (void) ? Just curious...
rpg_code_master
rpg_code_master
Quote:
Original post by Konfusius
Yes it works (on some compilers), but it's not valid C++ nor C.

I agree with that. I guess I should change [smile]
demonkoryu
demonkoryu
Quote:
Original post by rpg_code_master
Quote:
Original post by Konfusius
Yes it works (on some compilers), but it's not valid C++ nor C.

I agree with that. I guess I should change [smile]


One thing less to worry about! [lol]
Nitage
Nitage
Quote:
Original post by rpg_code_master
Quote:
Original post by Konfusius
BTW, (sorry can't resist :P) there has never been a void main().
And I am curious to know what an explicit void parameter should communicate...

It works, I use it all the time.

I just double tested on Visual Studio 2005 here at work, and it runs fine.


Quote:

ISO14882(C++ standard) section 3.6.1.2
An implementation shall not define the main function. The function shall not be overloaded. It shall have a return type of int but otherwise it's type is implementation defined. All implementations shall allow both of the following definitions of main:
int main() { /*...*/ }
int main(int argc, char* argv[]) { /*...*/ }


Evil Steve
Evil Steve
Quote:
Original post by Konfusius
And why the extra (void) ? Just curious...
It's an old C thing. In C, before the C99 (Or Csomething) standard, any function not taking any arguments had to be declared as return_type function_name(void).
TEUTON
TEUTON
Thank You Everyone

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.