Objects at runtime

Started by
4 comments, last by iMalc 19 years, 2 months ago
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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): 8sizeof(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;}
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 hereclass A{     void function1();};// End up herevoid 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,
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.
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
The static variables go in the globals data area.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement