Jump to content
  • Advertisement
Sign in to follow this  
Endar

Objects at runtime

This topic is 4899 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

Okay, this isn't really a question, its just to confirm something. After C++ classes are compiled, and a program is run with them, the member functions turn out like this:
// Start here
class A{
     void function1();
};

// End up here
void function1(A* aObject)
{
     // blah
}

Right?? So, if there was a situation like:
class B{
     bool value;
     int integer;
     float y;
     string hello;
     char c;

     void function1(){
          // blah
     }

     int function2(){
          // blah
     }
};

Then basically, the expression "sizeof(B)" would end up being the size of all the member variables. Right? okay, assuming that we have that right, which is good for me, but I was just struck with something else, what if in that class you have static variables? I assume that they wouldn't be counted with the 'sizeof' expression. So, what, are they just left to fend for themselves?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Endar
Okay, this isn't really a question, its just to confirm something.

After C++ classes are compiled, and a program is run with them, the member functions turn out like this:

*** Source Snippet Removed ***

Right??

So, if there was a situation like:

*** Source Snippet Removed ***

Then basically, the expression "sizeof(B)" would end up being the size of all the member variables. Right?

okay, assuming that we have that right, which is good for me, but I was just struck with something else, what if in that class you have static variables? I assume that they wouldn't be counted with the 'sizeof' expression. So, what, are they just left to fend for themselves?
Yeah. So the following program outputs
sizeof(A): 8
sizeof(B): 8

#include <cstdlib>
#include <iostream>

class A {
int a;
int b;
int func() {return a; };
};

class B {
int a;
int b;
static int c;
static int d;
int func() {return a; };
};

int B::c;
int B::d;

int main() {

std::cout << "sizeof(A): " << sizeof(A) << std::endl;
std::cout << "sizeof(B): " << sizeof(B) << std::endl;

system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
Okay, this isn't really a question, its just to confirm something.

After C++ classes are compiled, and a program is run with them, the member functions turn out like this:


// Start here
class A{
void function1();
};

// End up here
void function1(A* aObject)
{
// blah
}


Right??

Well, this is nearly true, but is perhaps oversimplified. In most compilers, this is not sent to the function as the other parameters. For example, VC++ moves this into ecx while it pushes the other parameters on the stack.

But this is only a slight (implementation) difference :)

Regards,

Share this post


Link to post
Share on other sites
If you use virtual functions, your compiler adds a pointer to the vtable to the data of your class, so:

class A {
int a;
void func() {}
};

class B {
int b;
virtual void func() {}
};

sizeof(A) is 4
sizeof(B) is 8

BTW what does:
class C {}
sizeof(C) = ?

return ?

For gcc sizeof(C) is 1.

Share this post


Link to post
Share on other sites
Quote:
Original post by nmi
BTW what does:
class C {}
sizeof(C) = ?

return ?

For gcc sizeof(C) is 1.


That's correct. The compiler inserts one byte there - nothing can be 0 bytes long - imagine an array of such 0-bytes-long objects - the address of 1st element would be the same as the address of 10000000th element. That would cause big troubles.

Oxyd

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!