Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

sizeof problem

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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.

Share this post


Link to post
Share on other sites
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 CFoo

void 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!