You are on page 1of 2

8/9/2017 c++ - Where are functions of an object stored in memory?

- Stack Overflow

x Dismiss

Join the Stack Overflow Community

Stack Overflow is a community of 7.5 million


programmers, just like you, helping each other.
Join them; it only takes a minute:

Sign up

Where are functions of an object stored in memory?

Suppose we have a class:

class Foo
{
private:
int a;
public:
void func()
{
a = 0;
printf("In Func\n");
}
}

int main()
{
Foo *foo = new Foo();
foo->func();
return 0;
}

When the object of the class Foo is created and initialized, I understand that integer a will take up 4 bytes of memory. How is the function
stored? What happens in memory / stack /registers / with the program counter when calling foo->func()?

c++ memory

edited Jul 1 '14 at 9:39 asked Jul 1 '14 at 7:40


shaveenk
657 2 11 22

3 Answers

Your function is not virtual, it is thus statically called : the compiler inserts a jump to the code
segment corresponding to your function. No additional memory is used per instance.

Were your function virtual, your instance would carry a vpointer, which would be dereferenced
to find its class' vtable, which would then be indexed to find the function pointer to be called,
and finally jump there. The additional cost is thus one vtable per class (probably the size of
one function pointer, times the number of virtuals functions of your class), and one pointer per
instance.

Note that this is a common implementation of virtual calls, but is in no way enforced by the
standard, so it could actually not be implemented like that at all but your chances are quite
good. The compiler can also often bypass the virtual call system altogether if it has knowledge
of the static type of your instance at compile time.

answered Jul 1 '14 at 7:50


Quentin
32.1k 3 52 107

"the compiler inserts a jump to the code segment". Where is this jump information stored? shaveenk Jul 1
'14 at 7:54

@shaveenk Directly at the calling site, i.e. another part of the code segment. For a static call it gets

https://stackoverflow.com/questions/24504913/where-are-functions-of-an-object-stored-in-memory 1/2
8/9/2017 c++ - Where are functions of an object stored in memory? - Stack Overflow
hardcoded, so you just end up with a plain goto 0xSomewhere; in the assembly (after some parameters
tweaking) Quentin Jul 1 '14 at 7:57

The short answer: It will be stored in the text or code section of the binary only once
irrespective of the number of instances of the class created.

The functions are not stored separately anywhere for each instance of a class. They are
treated the same way as any other non-member function would be. The only difference is that
the compiler actually adds an extra parameter to the function,which is a pointer of the class
type. For example the compiler will generate the function prototype like this:

void func(Foo* this);

(Note that this may not be the final signature. The final signature can be much more cryptic
depending on various factors including the compiler)

Any reference to a member variable will be replaced by

this-><member> //for your ex. a=0 translates to this->a = 0;

So the line foo->func(); roughly translates to:

1. Push the value of Foo* on to the stack. #Compiler dependent


2. call func which will cause the instruction pointer to jump to the offset of func in the
executable #architecture dependent Read this and this
3. Func will pop the value from stack. Any further reference to a member variable would be
preceded by dereferencing of this value

edited Jul 1 '14 at 8:42 answered Jul 1 '14 at 7:56


bashrc
3,097 1 9 34

+1; as for the reason you mentioned about the term signature, sometimes the alternative prototype is used,
although this usage here can be argued. legends2k Jul 1 '14 at 8:04

Since i have mentioned the return type as well, prototype should be the write word. Edited the answer to
reflect the same. bashrc Jul 1 '14 at 8:43

such a great answer Utkarsh Srivastav May 12 at 17:30

Member functions are just like regular functions, they are stored in the "code" or "text" section.
There is one thing special with (non-static) member functions, and that is the "hidden" this
argument that is passed along to function. So in your case, the address in foo will be passed
to func .

Exactly how that argument is passed, and what happens to registers and stack is defined by
the ABI (Application Binary Interface), and varies from processor to processor. There is no
strict definition for this, unless you tell us what the combination of compiler, OS and processor
is being used (and assuming that information is then publicly available - not all compiler/OS
vendors will tell this very clearly). As an example, x86-64 will use RCX for this on WIndows,
and RDI on Linux, and the call instruction will automatically push the return address onto the
stack. On an ARM processor [in Linux, but I think the same applies in Windows, I just have
never looked at that], R0 is used for the this pointer, and the BX instruction used for the call,
which as part of itself stores lr with the pc of the instruction to return to. lr then has to be
saved [probably on the stack] in func , since it calls printf .

answered Jul 1 '14 at 7:54


Mats Petersson
99.7k 7 66 147

https://stackoverflow.com/questions/24504913/where-are-functions-of-an-object-stored-in-memory 2/2