-1

where does a member function of class gets its memory allocated?

Is stack frame the answer for it?

And does it gets allocated whenever we call the member function?

Is that like whenever we call a member function using an object it gets allocated in the stack frame and when the function reaches it return statement that allocated stack frame gets deallocated?

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
Manisha P
  • 111
  • 2
  • 9

1 Answers1

1

where does a member function of class gets its memory allocated?

Allocation of memory for function data is implementation defined.

Is stack frame the answer for it?

There is no requirement for a C++ implementation to use a stack structure.
A stack structure for local variables is a convenience, but not a requirement.

And does it gets allocated whenever we call the member function?

Function local variables may get created when execution enters the function. There is no requirement that an implementation must create the variables. The variables could exist in global memory and initialized when execution enters a function.

Is that like whenever we call a member function using an object it gets allocated in the stack frame and when the function reaches it return statement that allocated stack frame gets deallocated?

A popular implementation is to allocate space on a stack for local variables. Space allocation would involve incrementing or decrementing a stack pointer.
When execution leaves the scope, the stack is adjusted accordingly.

The implementation is allowed to use registers, so there is a possibility that no memory is allocated and the stack is not changed. The implementation may push registers onto a stack before function execution and pop them afterwards.

The answers to your questions are implementation defined. Small embedded systems may use more registers or global memory rather than using a stack. There many possible data structures that can be used and still meet the C++ language standard.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154