Let's say I have some class who's only member is an int. If it wasn't in a class, the int alone would be 4 bytes. Does the class take more than 4 bytes of memory (in C++)?
-
If you have a virtual function in that class, then yes. Otherwise, no. But it's generally a (compiler) implementation decision, i.e., not defined by the standard of the C++ language. And the same goes for the size of an `int` being 4 bytes, by the way. – goodvibration Sep 13 '17 at 20:51
2 Answers
The decision about how big a class ends up being is implementation-specific and depends on a lot of different factors. Sometimes, due to structure and class padding, a class might end up bigger than the size of its members. If you have any virtual functions in your class, then you'll typically end up with a virtual function table pointer (vtable pointer) at the front of the class that adds a bit of space. And it's entirely possible that the compiler might just For The Heck Of It make your class bigger than the size of its members if it think it will help out in some way (or if you have a lazy compiler!)
In your case, with a single 32-bit integer, I'd be surprised if the class ended up being any larger than the integer itself, since you aren't using any virtual functions and there aren't any members to insert padding bytes between. However, you cannot necessarily rely on this across systems.
If you're working on an application where it's absolutely essential that your class be the same size as the fields - perhaps, for example, if you're reading raw bytes and want to reinterpret them as class objects - you could use a static_assert
to check for this:
class MyClass {
...
};
static_assert(sizeof(MyClass) == sizeof(int), "MyClass must have the same size as an integer.");
Many compilers have custom options (often through #pragma
directives) that you can tune to ensure that classes get sized in a way that you'd like, so you could also consider reading up on that.

- 362,284
- 104
- 897
- 1,065
-
`#pragma pack(push, 1)` (followed by `#pragma pack(pop)` after the class definition, to ensure it only affects the target class) works on MSVC and GCC. – Xirema Sep 13 '17 at 21:05
-
I actually just asked out of curiosity, I'm not planning on using it for anything. – BadProgrammer99 Sep 14 '17 at 00:23
The actual size is implementation-dependent, so it can change across different compilers and architectures due to padding and other implementation details. Never trust a simple sum like in the following pseudocode:
size = sizeof(member1) + ... + sizeof(memberN)
Also if the class has virtual functions, yes, it can be more than 4 bytes.
Moreover, in the case of virtual functions and class inheritance the size can be complicated to be understood at first sight:
- Each class that include virtual functions will store a vtable in memory with function pointers to these virtual functions.
- Class A, with virtual functions, that inherit from another class B, that has virtual functions too, could need more than one table to store both A and B function pointers.
See this answer for more details: how to determine sizeof class with virtual functions?

- 932
- 6
- 17
-
1Please flag the question as duplicate instead of linking that in another answer. – user0042 Sep 13 '17 at 20:49
-
-
-
I think an user with low experience that doesn't even know about virtual functions can search for a more generic question like this one, and then he can read about virtual functions using the link. So that's why I think it's better to keep both questions and my answer. – turbopapero Sep 13 '17 at 20:52
-
1the answer should be selfcontained. Links can break at any time (even within SO) so you should at least summarize what is relevant to answer the question here – 463035818_is_not_an_ai Sep 13 '17 at 21:21
-
1Don't ignore padding. Left to it's own devices, `struct test {char a; int b}` probably isn't going to be `sizeof(char) + sizeof(int)` – user4581301 Sep 13 '17 at 21:45
-