4

So I have read this about if class definitions occupy memory and this about if function occupy memory. This is what I do not get: How come class definitions do not occupy memory if functions do, or their code does. I mean, class definitions are also code, so shouldn't that occupy memory just like function code does?

  • 1
    From the top answer of the first question you linked: _"Classes themselves use memory if they have methods that are not optimized away, if they have a vtable (caused by use of the virtual keywords), or similar."_ – YSC Aug 16 '17 at 11:59
  • 7
    A class definition is only used by the compiler as a blueprint for objects of that class. Once the program is compiled the class definition itself doesn't exist in the final executable, only the objects created for the class. – Some programmer dude Aug 16 '17 at 12:00
  • 1
    Class methods do take space, exactly as free functions. Vtables and typeid info take space. For the rest, the structure of the class is implicit in the generated code, there's no separate class entity in the generated binary. – Matteo Italia Aug 16 '17 at 12:06
  • @Someprogrammerdude and why aren't function definitions also used as blueprints for the point of the call? –  Aug 16 '17 at 12:07
  • Because the code for the function must exist somewhere? – Some programmer dude Aug 16 '17 at 12:11
  • @Someprogrammerdude but why must function definitions exist somewhere then? –  Aug 16 '17 at 12:45
  • 1
    Because a function definition is the *code that is being executed when the function is being called*. Are you perhaps mixing up function *declarations* with *definitions*? – Some programmer dude Aug 16 '17 at 12:47
  • @Someprogrammerdude Well okay, thanks. And no I am not :) But can't you say that class definition is what is being instantiated when an object of the class is constructed? –  Aug 16 '17 at 12:50
  • 1
    But it is. A class declaration is simply `class foo;`. A class definition is `class foo { /* lots of member variables and functions, some as declarations and some as definitions */ };` Without a definition of a class, you can't create an instance of the class. – Some programmer dude Aug 16 '17 at 12:53
  • @Someprogrammerdude Exactly, so why isn't it needed in the executable? Like couldn't function definitions also be used as blueprints? After an answer to this, I actually think that I get it :) –  Aug 16 '17 at 12:54

2 Answers2

5

It is not entirely correct to say that class definitions do not occupy memory: any class with member functions may place some code in memory, although the amount of code and its actual placement depends heavily on function inlining.

The Q&A at the first link talks about sizeof, which shows a per-instance memory requirement of the class, which excludes memory requirements for storing member functions, static members, inlined functions, dispatch tables, and so on. This is because all these elements are shared among all instances of the class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks! But did that answer the question: "How come class definitions do not occupy memory if functions do, or their code does. I mean, class definitions are also code, so shouldn't that occupy memory just like function code does?"? If so, please point it out.. :) –  Aug 16 '17 at 14:43
  • 1
    @sdsadasdasd Class definition is a grouping construct for everything that goes into the class. In this way they are similar to namespaces. They exist only during the compilation. Once the compiler is done, things that are included in class definitions, such as member functions, static members, etc. go into memory, while class definitions themselves are discarded as not needed at run-time. – Sergey Kalinichenko Aug 16 '17 at 15:02
  • Thanks! It makes more sense now. But how would you explain the reason why functions do not work like class definitions? I mean couldn't the compiler just replace each function call with the instructions given by the function itself and thus being able to get rid of the function code before execution, just like class definitions? I really that you will answer because it would be sad otherwise ::)) –  Aug 17 '17 at 14:01
  • 1
    @sdsadasdasd The compiler will replace some functions with their instructions. This is called inlining; when this happens to functions with internal linkage, these functions do not occupy a separate space in memory. However, most functions are not inlined. The compiler replaces them with a sequence of instructions to jump to the place in memory where the function code is stored, rather than using the code of the function repeatedly. – Sergey Kalinichenko Aug 17 '17 at 14:08
1

You don't need to keep the class definition anywhere, because the details of how to create an instance of a class are encoded in its constructors.
(In a sense, the class definition is code, it's just not represented explicitly.)

All you need to know in order to create an object is

  1. How big it is,
  2. Which constructor to use for creating it, and
  3. Which its virtual functions are.

To create an instance of class A:

  1. Reserve a piece of memory of size sizeof(A) (or be handed one),
  2. Associate that piece of memory with the virtual functions of A, if any (usually held in a table in a predetermined location), and
  3. Tell the relevant A constructor where the A should be created, and then let it do the actual work.

You don't need to know a thing about the types of member variables or anything like that, the constructors know what to do once they know where the object is to be created.
(Every member variable can be found at an offset from the beginning of the object, so the constructor knows where things must be.)

To create a function, on the other hand, you would need to store its definition in some form and then generate the code at runtime. (This is usually called "Just-in-time" compilation.)

This requires a compiler, which means that you need to either

  1. Include a compiler in every executable, or
  2. Provide (or require everyone to install) a shared compiler for all executables (Java VMs usually contain at least one).

C++ compilers instead generate the functions in advance.
Abusing terminology a little, you could say that the functions are "instantiated" by the compilation process, with the source code as a blueprint.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thanks! But so regarding functions, the program need the instructions from a specific function in order to execute the functions (because it is running the instructions provided in the function), as opposed to class definitions that are simply blueprints for the compiler about how to instantiate objects... Am I right? Otherwise, please correct me :-) –  Aug 17 '17 at 10:48