Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are functions of an object stored in memory?

Tags:

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()?

like image 416
shaveenk Avatar asked Jul 01 '14 07:07

shaveenk


People also ask

Are functions stored in stack or heap?

Functions are objects. Therefore, the function's identifier is in the stack, and the function's value is stored in the heap. A function creates an activation object when it's called.

How are functions and variables stored in memory?

Most modern architectures act mostly the same way; block-scope variables and function arguments will be allocated from the stack, file-scope and static variables will be allocated from a data or code segment, dynamic memory will be allocated from a heap, some constant data will be stored in read-only segments, etc.

Where are function definitions stored?

The standard C library functions are provided in libraries along with your compiler and the rest of your toolchain. For unix-like systems, that's usually in libraries called libc and libm .

How are functions stored in memory python?

Python stores object in heap memory and reference of object in stack. Variables, functions stored in stack and object is stored in heap.

How are Java objects stored in memory?

- GeeksforGeeks How are Java objects stored in memory? In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new (), the object is allocated on Heap, otherwise on Stack if not global or static.

What is the function of memory in a computer?

Depending on the type of data it stores and the role it plays in computer operation, however, memory performs several different functions. Although all these functions involve data storage, RAM, ROM, flash memory, and hard drives, each performs a different and necessary process to keep a computer and its peripherals working.

Where is an object stored if it is created inside block?

- GeeksforGeeks Where is an object stored if it is created inside a block in C++? There are two parts of memory in which an object can be stored: stack – Memory from the stack is used by all the members which are declared inside blocks/functions. Note that the main is also a function.

What are the two parts of memory in C++?

There are two parts of memory in which an object can be stored: 1 stack – Memory from the stack is used by all the members which are declared inside blocks/functions. Note that the main is also a function. 2 heap – This memory is unused and can be used to dynamically allocate the memory at runtime.


1 Answers

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
like image 61
bashrc Avatar answered Oct 23 '22 22:10

bashrc