Do functions go to the heap or stack if the class is on the heap?

Viewed 576
class Foo{

    public:
        void foo(){
            int x, y; // go to the HEAP or the stack?
        }
};

int main(){
    Foo *f = new Foo();
    f -> foo();

    delete f;
}

if I create a class instance on the HEAP and activate a "class method" where is the function local variables and "metadata" stored? on the HEAP because the class is on the HEAP or the stack?

3 Answers

Do functions go to the heap or stack if the class is on the heap?

The C++ language does not have such thing as "heap" or "stack".

Automatic variables have automatic storage. Dynamic objects have dynamic storage. Static variables have static storage. Thread local variables have thread local storage.

Functions do not have storage at all. Only objects (and sometimes references) do.


About typical implementation of C++ language on von Neumann architectures: The instructions of the program are stored separately from stack and heap. So, the answer to the question is: Neither.

where is the function local variables ... stored?

Automatic variables - i.e. those that are local to a block within a function and are not static - such as f, x and y are stored in automatic storage.

In a typical implementation, automatic storage is implemented either by storing the object on the execution stack, or in a CPU register, or nowhere (if their storage is not observable).

Your title is a little bit misleading because functions is a different concept to their local variables.

The technically correct answer to your question is it depends, and you can check eerorika answer for the standard specification details. The thing is that there are many specific scenarios where the concepts of heap and stack memory are not really easy to define, such as when you allocate memory in the heap and assign it as stack region for a particular thread.

If you run your example program in your PC, the Foo instance (pointed by f) is allocated on the heap (and any member variables it may define, in your example there are none). f, x and y local variables are located in the stack.

If you are using a Linux platform, I suggest you to run your program using a debugger like GDB, evaluate the addresses of all those variables with

p &f
p f
p &x
p &y

and then look for the area in memory they belong to by looking at /proc/1234/maps file, where 1234 is the process id of your program. You can ask gdb for that as well.

You will notice that in those regions there will be some areas containing the executable and several shared library files. That would be the area where the program instructions will be located, which is the memory where the functions are actually located (neither part of the stack or the heap).

The machine instructions and the data are placed separately ( e.g. see code segment on wikipedia, or search for application memory layout ). Despite you mix code and data using c++, later the compiler separates the stuff automatically( with C, you can not mix data and code ).

When you call a function( which is not inline ), a stack frame is created and pushed on the stack( LIFO! ) and then the processor jumps on the piece of code of the function. But if the stack is too big, you get stack overflow exception( like this site is named :D ), because in c++ the stack is limited. After the end of the function, the stack pointer is moved and the old frame( that was pushed before the current ) is active( is currently on the top ). You can imagine it in this way, it is to simulate entering a subprogram( usually while executing another subprogram) efficiently and safely( except if the stack is not exceeded :D ).

So, all input arguments and all local variables for a function/class method are placed on the stack. The size of the stack frame can be calculated on compile time( before you start your program, based on the variables and their sizes ).

Related