10

Say I have an object that exists in high quantity, stores little data about itself, but requires several larger functions to act upon itself.

class Foo
{
public:
    bool is_dead();

private:
    float x, y, z;
    bool dead;
    void check_self();
    void update_self();
    void question_self();
};

What behavior can I expect from the compiler - would every new Foo object cause duplicates of its methods to be copied into memory?

If yes, what are good options for managing class-specific (private-like) functions while avoiding duplication?

If not, could you elaborate on this a little?

Adrian
  • 2,276
  • 2
  • 19
  • 25
  • 1
    No, they are not duplicated. Why would you think otherwise? What would be the benefit? @Arusekk Use static or const why? – user207421 Jul 26 '14 at 12:47
  • 2
    I have to say I'm disappointed about the downvotes (and likely auto-deletion) my question receives. I googled but didn't find anything about "vtables". Hobbyists don't tend to get into C++ by reading thick books on the fundamental concepts behind the language; they start with simple examples and iron out their misunderstandings over time. I'll stick to user forums in the future, lesson learned. – Adrian Jul 26 '14 at 13:22
  • 5
    Seriously, people, why the eff do you downvote something like this? Just because the author COULD have come by this information othewise? He has a point, not everyone is interested in reading about the specification of the language. After all, its not that interesting. What an unfriendly place SO has become. – TeaOverflow Jul 26 '14 at 13:36
  • Although I did not downvote, perhaps some of them are because this type of question has been asked and answered before. For instance, when googling the first sentence of the question's title, the first result is http://stackoverflow.com/questions/648647/in-c-where-in-memory-are-class-functions-put – TheUndeadFish Jul 26 '14 at 14:21

2 Answers2

6

C++ methods are simply functions (with a convention about this which often becomes the implicit first argument).

Functions are mostly machine code, starting at some specific address. The start address is all that is needed to call the function.

So objects (or their vtable) need at most the address of called functions.

Of course a function takes some place (in the text segment).

But an object won't need extra space for that function. If the function is not virtual, no extra space per object is needed. If the function is virtual, the object has a single vtable (per virtual class). Generally, each object has, as its first field, the pointer to the vtable. This means 8 bytes per object on x86-64/Linux. Each object (assuming single inheritance) has one vtable pointer, independently of the number or of the code size of the virtual functions.

If you have multiple, perhaps virtual, inheritance with virtual methods in several superclasses you'll need several vtable pointers per instance.

So for your Foo example, there is no virtual function (and no superclass containing some of them), so instances of Foo contain no vtable pointer.

If you add one (or many hundreds) of virtual functions to Foo (then you should have a virtual destructor, see rule of three in C++), each instance would have one vtable pointer.

If you want a behavior to be specific to instances (so instances a and b could have different behavior) without using the class machinery for that, you need some member function pointers (in C++03) or (in C++11) some std::function (perhaps anonymous closures). Of course they need space in every instance.

BTW, to know the size of some type or class, use sizeof .... (it does include the vtable[s] pointer[s] if relevant).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I see. So the compiler is aware that - unlike (non-static) member variables - methods need not be stored more than once. Thanks! – Adrian Jul 26 '14 at 12:47
-6

Methods exists for every class in program, not for every object. Try to read some good books about c++ to know so easy facts about language.

Valner
  • 36
  • 2