0

Whenever object is created for a class, memory space will be allocated for a class. So my question is: Do memory created for only member variables or for member functions also?? If memory is created for member functions, then where they will be stored??

3 Answers3

1

Traditionally executable files had three sections. One for initialized data, one for uninitialized data, and one for code. This traditional partitioning is still very much in use, and code, no matter where it comes from, is placed in a separate section.

When an operating system load an executable file into memory, it puts the code in a separate place in memory that it marks as executable (on modern memory-protected systems) and all code are stored there separate from the objects themselves.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Member functions are just code located in the code segment. they are present exact one time, no matter how many objects you have. They are nearly exactly the same as ordinary functions except that their first parameter is the this pointer, that is hidden in the language but present as a parameter on the executable code.

But there are two kinds of member functions:

  1. "normal"
  2. virtual

there is no difference between them in the sense of code size however they are called differently. Calls to normal functions can be determined by compiletime and the other are indirect calls via the function pointers-

If a class has a virtual member functions (the class is "polymorph") the compiler needs to create a "vtable" for this class (not object).

Each object does contain a pointer to the vtable of its class. this is necessary to access the correct virtual function if the object is accessed by a pointer that is of a base classes type.

Example:

class A{
public:  bool doSomething();
int i;
};

class B:public A {
public:  bool doSomething();
int j;
}

//
B b;
A* a = &b;
a->doSomething(); // <- A::doSomething() is called;
//

neither of this classes needs a vtable.

Example 2:

class A{
public:  virtual bool doSomething();
int i;
};

class B:public A {
public:  bool doSomething();
int j;
}

//
B b;
A* a = &b;
a->doSomething(); // <- B::doSomething() is called;
//

A (and all its childs) get a vtable. Then an object is created the objects vtable pointer is set to the correct table, so that independently from the Type of the pointer the correct function is called.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

Only the member variables (plus padding between and after them) contribute to the sizeof of a class.

So in that sense regular functions do not take up space as far as an object is concerned. Member functions are little more than regular static functions with a implicit this pointer as a hidden argument.

Saying that though, a virtual function table might be the way an implemention deals with polymorphic types, and that will take up some space, but will probably only be a pointer to a table used by all objects of that particular class.

  • @BaummitAugen: of course you are correct. I've changed it. –  Sep 30 '16 at 15:20
  • thats not true. each object of a polymorphic class has an additional pointer to the vtable. so the objects size is the sum of members, padding and the vtable pointer – vlad_tepesch Sep 30 '16 at 16:18