For example, say I have a class Temp:
class Temp
{
public:
int function1(int foo) { return 1; }
void function2(int bar) { foobar = bar; }
There's a utility call pahole
(for 'Poke-A-HOLE') that's nominally intended to study how object layouts get padded, but is great for visualizing object size and layout in general.
If you want detailed information about how objects are represented in memory at run-time, the ABI (Application Binary Interface) specification is the place to look. You'll need to look determine which ABI your compiler implements; for example, GCC versions 3.2 and above implement the Itanium C++ ABI.
If you want to examine the layout of a particular structure, the offsetof(s,member)
macro may also be of use. It tells you how far from the base address of a structure a particular member lives:
struct foo {
char *a;
int b;
};
// Print placement of foo's members
void printFoo() {
printf("foo->a is %zu bytes into a foo\n", offsetof(struct foo, a));
printf("foo->b is %zu bytes into a foo\n", offsetof(struct foo, b));
}
int main() {
printFoo();
return 0;
}
Would print on a typical 32-bit machine:
foo->a is 0 bytes into a foo
foo->b is 4 bytes into a foo
Whereas on a typical 64 bit machine, it would print
foo->a is 0 bytes into a foo
foo->b is 8 bytes into a foo
I've always wondered this sort of thing, so I decided to come up with a complete answer. It's about what you might expect, and it is predictable (yay)! Thus, with the information below, you ought to be able to predict the size of a class.
Using Visual Studio Community 2017 (Version 15.2), in Release mode with all optimizations disabled and RTTI (Run-time Type Information) off, I have determined the following:
Shortish answer:
First of all:
<size of pointer> == 4
bytes<size of pointer> == 8
bytesclass ChildClass: virtual public ParentClass
Now, my findings are that:
<size of variable>
bytes<size of variables>
bytes<size of pointer>
bytes<size of pointer>
bytes<size of pointer>
bytes<size of pointer>
bytes to the total, wrapping all of the member variables in as many <size of pointer>
bytes increments as is necessary to cover <total size of member variables>
- yeah, you read that right... (see my guess as to what's going on in Conclusions...)Note that I even tried having the function() cout some text, creating an instance of the class, and calling the function; it doesn't change the size of the function class (it's not an optimization)! I was somewhat surprised, but it actually makes sense: member functions don't change, so they can be stored external to the class itself.
Conclusions:
<size of pointer>
bytes, adding <size of pointer>
bytes to said class. This vtable can only exist once per class (either it does or it doesn't), of course.<size of pointer>
bytes at a time, even if it doesn't need to consume that much memory, I'm guessing because it's adding a vtable "helper block" for each <size of pointer>
bytes of memory or something...Long answer:
I determined all of this using this code:
#include <iostream>
using namespace std;
class TestA
{
};
class TestB: public TestA
{
};
class TestC: virtual public TestA
{
};
class TestD
{
public:
int i;
};
class TestE: public TestD
{
public:
int j;
};
class TestF: virtual public TestD
{
public:
int j;
};
class TestG
{
public:
void function()
{
}
};
class TestH: public TestG
{
public:
void function()
{
}
};
class TestI: virtual public TestG
{
public:
void function()
{
}
};
class TestJ
{
public:
virtual void function()
{
}
};
class TestK: public TestJ
{
public:
void function() override
{
}
};
class TestL: virtual public TestJ
{
public:
void function() override
{
}
};
void main()
{
cout << "int:\t\t" << sizeof(int) << "\n";
cout << "TestA:\t\t" << sizeof(TestA) << "\t(empty class)\n";
cout << "TestB:\t\t" << sizeof(TestB) << "\t(inheriting empty class)\n";
cout << "TestC:\t\t" << sizeof(TestC) << "\t(virtual inheriting empty class)\n";
cout << "TestD:\t\t" << sizeof(TestD) << "\t(int class)\n";
cout << "TestE:\t\t" << sizeof(TestE) << "\t(inheriting int + int class)\n";
cout << "TestF:\t\t" << sizeof(TestF) << "\t(virtual inheriting int + int class)\n";
cout << "TestG:\t\t" << sizeof(TestG) << "\t(function class)\n";
cout << "TestH:\t\t" << sizeof(TestH) << "\t(inheriting function class)\n";
cout << "TestI:\t\t" << sizeof(TestI) << "\t(virtual inheriting function class)\n";
cout << "TestJ:\t\t" << sizeof(TestJ) << "\t(virtual function class)\n";
cout << "TestK:\t\t" << sizeof(TestK) << "\t(inheriting overriding function class)\n";
cout << "TestL:\t\t" << sizeof(TestL) << "\t(virtual inheriting overriding function class)\n";
cout << "\n";
system("pause");
}
Output:
32 (x86) bits:
int: 4
TestA: 1 (empty class)
TestB: 1 (inheriting empty class)
TestC: 4 (virtual inheriting empty class)
TestD: 4 (int class)
TestE: 8 (inheriting int + int class)
TestF: 12 (virtual inheriting int + int class)
TestG: 1 (function class)
TestH: 1 (inheriting function class)
TestI: 4 (virtual inheriting function class)
TestJ: 4 (virtual function class)
TestK: 4 (inheriting overriding function class)
TestL: 8 (virtual inheriting overriding function class)
64 (x64) bits:
int: 4
TestA: 1 (empty class)
TestB: 1 (inheriting empty class)
TestC: 8 (virtual inheriting empty class)
TestD: 4 (int class)
TestE: 8 (inheriting int + int class)
TestF: 24 (virtual inheriting int + int class)
TestG: 1 (function class)
TestH: 1 (inheriting function class)
TestI: 8 (virtual inheriting function class)
TestJ: 8 (virtual function class)
TestK: 8 (inheriting overriding function class)
TestL: 16 (virtual inheriting overriding function class)
If you want information on multiple inheritance, go figure it out your darn self! -.-
Member functions dont account for the size of the objects of a particular class. The size of the object depends only on the member variables. In case of classes that contain virtual functions, the VPTR gets added to the object layout. So the size of the objects is basically size of the member variables + the size of the VPTRs. Sometimes this may not be true as Compilers try to locate member variables at the DWORD boundary.