Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation to functions in C++

I am still a C++ newbie. Just came to read that the static member function of a class is not object specific - there is a single copy of the member functions for all the objects.

Now two questions arise in my mind :

  1. What is the difference between an ordinary function and a static function "in terms of memory allocation only" ?

  2. What if the member function contains some local variables ? In that case the function "should" have a separate copy of that variable - specific to the object invoking the function... How is this problem solved in C++ ?

Thanks !

like image 954
dev Avatar asked Aug 04 '10 07:08

dev


People also ask

How much memory is allocated to a function in C?

Syntax: ptr = (cast-type*) malloc(byte-size) For Example: ptr = (int*) malloc(100 * sizeof(int)); Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.

What is memory allocating function?

Dynamic Memory Allocation is a process in which we allocate or deallocate a block of memory during the run-time of a program. There are four functions malloc(), calloc(), realloc() and free() present in <stdlib. h> header file that are used for Dynamic Memory Allocation in our system.

How the memory allocation is done for functions and objects?

There are two basic types of memory allocation: When you declare a variable or an instance of a structure or class. The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory.

How memory allocated by malloc () or calloc () function can be deallocated?

The answer is of course "you can't", and thus malloc() can't either.


2 Answers

What is the difference between an ordinary function and a static function "in terms of memory allocation only" ?

Nothing. A static function is just like a global function except for the scope.

Even for non-static member functions, there's no extra memory required. The member function int C::f(int arg1, int arg2) is just syntactic sugar for something like int C__f(C* this, int arg1, int arg2).

What if the member function contains some local variables ? In that case the function "should" have a separate copy of that variable - specific to the object invoking the function...

There's a copy of the local variables for each invocation of the function (unless they're static). This is why recursion in possible in C++.

How is this problem solved in C++ ?

Function calls are based around "stack frames". A stack frame consists of:

  • The arguments to the function (including the implicit this if applicable).
  • Any other non-static local variables in the function.
  • The "return address", which tells the processor where to resume execution once the function is done.

Whenever a function is called, a stack frame is created. When the function returns, the stack frame is destroyed. If a function is called recursively, each level of recursion gets its own stack frame. For example, if you have

int factorial(int n) {
    if (n <= 1)
        return 1;
    else
        return factorial(n - 1) * n;
}

Then when you call factorial(3), a stack frame gets created like so:

------------------------ stack pointer (SP)
n = 3
RA = <in main()>

When the recursive call is made to factorial(2), an additional frame is added to the top of the stack

------------------------ SP
n = 2
RA = <in factorial()>
------------------------
n = 3
RA = <in main()>

Another recursive call is made, to factorial(1).

------------------------ SP
n = 1
RA = <in factorial()>
------------------------
n = 2
RA = <in factorial()>
------------------------
n = 3
RA = <in main()>

This is the base case for the recursion, and the return value of 1 is stored in a register. The function call being complete, the top stack frame is destroyed, and execution continues at saved return address.

------------------------ SP
n = 2                                      
RA = <in factorial()>
------------------------
n = 3
RA = <in main()>

Now, the call to factorial(2) can compute its return value (2), and another stack frame can be destroyed:

------------------------ SP
n = 3
RA = <in main()>

Finally, we can compute the result of the original function call (6), and destroy this stack frame too.

like image 162
dan04 Avatar answered Sep 22 '22 00:09

dan04


  1. no difference
  2. local variables are created and destroyed on the stack per invocation of the function
like image 40
Paul R Avatar answered Sep 20 '22 00:09

Paul R