2

Take these two classes for example (C++)

class B1 {
public:

};

class B2 {
public:
  void f0 () {}
  void f1 () {}
};

How much bigger would class B2 be in memory Vs B1

I feel like it's one of two answers:

A single 4 byte int pointer on 32 bit systems PER method.

Or something similar to what happens with Virtual Method Tables http://en.wikipedia.org/wiki/Virtual_method_table

Where there would be one 4 byte int pointer that points to a table for each class so it can look up it's methods, which would make sense, but I don't know if this happens for non-virtual methods.

Thanks.

Edit : Thanks for all of the awesome and quick replies :) (Also marked answer)

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
  • 1
    A simple trick: Test it in your compiler and see if you can falsify your hypothesis. – Kerrek SB Mar 05 '12 at 19:10
  • 1
    Note that even if you try it and your this does not falsify you're hypothesis, it doesn't mean your hypothesis is valid. – André Caron Mar 05 '12 at 19:16
  • possible duplicate of [In C++, where in memory are class functions put?](http://stackoverflow.com/questions/648647/in-c-where-in-memory-are-class-functions-put) (with credit to [@Sid](http://stackoverflow.com/users/559095/sid) for finding it) – Ben Voigt Mar 05 '12 at 19:28

2 Answers2

8

None.

Non-virtual methods don't increase a class's size.

As for virtual methods, only the first one added to a class will increase its size, all subsequent ones do not.

The fact that it's inline or not also doesn't affect the class size.

The reason for this is that extra memory is not needed. Just imagine if all instances of all classes held pointers to all methods in the class and all parent classes. That would be a huge waste of memory.

B2 b;
b.f0();

the compiler can simply generate code to call B2::f0(). The this pointer is passed as an under-the-hood parameter so that the method know on which instance of the class to operate.

For a simple test:

class B1 {
public:

};

class B2 {
public:
  void f0 () {}
  void f1 () {}
};

//...
assert( sizeof(B1) == sizeof(B2) );
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • How would you prove this is true in C++ and why is this the case? – mmacdermaid Mar 05 '12 at 19:14
  • 1
    @mmacdermaid I already explained why this is the case: 1) there's no need for it, as the methods need no dynamic binding (like in the case of virtual methods) 2) huge memory waste. As for proving it, you can compare the size of your 2 classes. – Luchian Grigore Mar 05 '12 at 19:16
  • Add a method and use sizeof to calculate before and after size. – Sid Mar 05 '12 at 19:16
  • I caught your reply before you edited. Thanks for the update. – mmacdermaid Mar 05 '12 at 19:16
  • 1
    @mmacdermaid: I'm not sure the behavior is actually required by the standard, since it's (reportedly) possible to implement a C++ interpreter. However, in all practical implementations (and therefore, all popular compilers), the method address is hard-coded by the compiler at compile time and never needs to be looked-up/accessed/computed at run-time, so there is no need to store a pointer to look up the address. – André Caron Mar 05 '12 at 19:19
  • @AndréCaron that's a good point. I didn't bother to look in the standard, but I'm assuming that if someone is bright enough to write a compiler, he would never have non-virtual methods increase object size. – Luchian Grigore Mar 05 '12 at 19:21
0

Luchian's answer is correct. I just want to add that code is kept on the text segment which is different from where the data resides (data segment). Hence, the only time methods affect an object's size is when there is (at least one) a virtual method hence forcing a vptr to be placed inside the object (for every instance). Read this to get more enlightened on the segments part: In C++, where in memory are class functions put?

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Sid
  • 7,511
  • 2
  • 28
  • 41
  • 1
    Since this is not an answer, I suggest you make this a comment. Nice link, BTW! – André Caron Mar 05 '12 at 19:28
  • That's a good reference, which appears to cover this question completely enough to mark this one as a dupe. – Ben Voigt Mar 05 '12 at 19:28
  • I would add that once there is at least one virtual method, than there will ALSO be one "instance" of a virtual table per object. Its size will be the size of pointer multiply by the number of virtual methods for this object (but in practice it might be compiler dependent). – Guy Avraham Jan 28 '22 at 13:18