Size of an object in memory at runtime

Actually the const should be dealt with at compile time and not cost any memory. A static variable on the other hand would be stored in memory although only one copy for all instances of the class as you mentioned. In the C++ implementations I have seen the only overhead for each object instance above the memory used by the member variables is a single pointer for the vtable (virtual function table). This would not apply to the class in your example since it has no virtual functions. I don't think that this is required by the standard so different compilers will handle it differently but this is definitely what Visual C++ does. Josh On 28/11/11 11:48 AM, skunkworks-request@lists.my.co.ke wrote:
Message: 2 Date: Mon, 28 Nov 2011 11:49:34 +0300 (EAT) From: Steve Obbayi<steve@sobbayi.com> To: Skunkworks Mailing List<skunkworks@lists.my.co.ke> Subject: Re: [Skunkworks] Size of an object in memory at runtime Message-ID:<97d9ac43-36ee-45f0-9699-2bbeeda8daa6@obbayi> Content-Type: text/plain; charset="utf-8"
In C++ to determine the size of a class in memory you look at the variables linked to the class
so take for example:
class myClass {
int x = 2; int y = 3; const int z = 10;
myClass(int a, int b) { x = a; y = b; }
int addInt() { return x+y; }
}
The size of this class instance in memory is basically the size of the two variables x and y which would be about 8 bytes total plus some overhead which could be a single byte. Notice the constant int z. This is not part of the instance of a C++ class. Reasoning is even if you make ten instances of the class all you will have is 10 instances of x and 10 instances of y which is total 80 bytes. z will only be one instance. Thats why you can use z without creating an object of the class.
The object is stored in the heap with the values and the pointer to that object is stored in the stack eg
MyClass happy = new MyClass(10, 20);
happy is the address of "new MyClass(10, 20)" is stored in the stack and takes up 1 byte (lets assume the underlying pointer is stored in a single byte) "MyClass(10, 20)" is stored in the heap and takes up 10 x sizeof(int) + 20 x sizeof(int) + overhead.
There is an argument about the memory of member functions. They behave the same way. calculate the size of the local variables in the member functions. Stuff like for loops, while, if else. those are language constructs and are handled internally by the language hence the overhead.
It is slightly more complicate than this but I hope you get the idea behind memory management in C++ and C also (structs and unions)
Steve

The overhead normally stores the pointer to the object address in the heap. In addition to other information included by the implementing framework. It is highly dependent on the target platform, so theoretically you can leave it out and that accounts for differences when you run sizeof(MyClassbject) and when you manually calculate the class size based on its data. When optimizing your code for low memory footprint usage its valuable knowledge knowing how manually determine how much memory your object will take by simply looking at the code. ----- Original Message ----- | From: "Josh Handley" <josh@bridgeinternationalacademies.com> | To: skunkworks@lists.my.co.ke | Sent: Monday, November 28, 2011 11:54:50 AM | Subject: [Skunkworks] Size of an object in memory at runtime | | Actually the const should be dealt with at compile time and not cost | any | memory. A static variable on the other hand would be stored in | memory | although only one copy for all instances of the class as you | mentioned. | | In the C++ implementations I have seen the only overhead for each | object | instance above the memory used by the member variables is a single | pointer for the vtable (virtual function table). This would not | apply | to the class in your example since it has no virtual functions. I | don't | think that this is required by the standard so different compilers | will | handle it differently but this is definitely what Visual C++ does. | | Josh | | | _______________________________________________ | Skunkworks mailing list | Skunkworks@lists.my.co.ke
participants (2)
-
Josh Handley
-
Steve Obbayi