0

like data members are created where the object is created (Each object has its unique data members) and member functions(which is code, not a data) are stored in code memory section and it is common for all the other objects. Now my question is, where does the object and the variables that is created in the member function is stored. example:

class A
    
{
    
int a,b;
    
public:
    
void foo(int x,int y)
    
{
    
a=x;
    
b=y;
    
A obj_1;
    
}
    
};
    
int main()
    
{
    
A obj;
    
obj.foo(1,2);
    
return 0;
    
}

are these object(obj_1) and the variables(x,y) stored where the obj is or anywhere else.

Hoping for your answers guys. thanks in advance!

searched on google and youtube too, but didn't get the answer that i want. if anyone knows, help me to get this.

Indu
  • 37
  • 8
  • 1
    `obj_1`, `x` and `y` are all local variables. The memory for them is allocated on the *stack*. Memory allocated on the stack is also deallocated when the variables are no longer in use. – john Dec 17 '22 at 14:15
  • Related: [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – kotatsuyaki Dec 17 '22 at 14:15
  • my doubt is, the object obj and the member variables is created on the stack too, so this obj_1,x,y are also stored in where the obj and member variables are? – Indu Dec 17 '22 at 14:20
  • 1
    Member functions are not special in this regard. They work exactly like normal functions, using the stack in the same way. – Mat Dec 17 '22 at 14:22
  • Member function `A::foo(int, int)` is more or less the same as free function `foo(A* this, int, int*)`. It just takes an extra "hidden" parameter called `this`. – yeputons Dec 17 '22 at 14:22
  • I got it. thank you guys. but what about the variable? i have no idea regarding these variables which are declared in the functions – Indu Dec 17 '22 at 14:28

1 Answers1

0

Functions are loaded in memory stack frames....like memory is allocated to a program by stack concept....The concepts of stack frames for memory allocation is used because to free space after the process and during it after its scope termination...Let me explain....

The space or memory is allocated to each function on stack. There is sub-stacks inside that part of stack that stores inside it....

Means storage is the same but there is further division of that storage inside this function.

In addition, Function has its own memory but how that will be determined that how much space will be allocated to that function???..It will be as be calculations of its members and then requesting that space on stack frames...

Take it clear, there will be no memory allocation to member functions until the class is used in main() function block; Until they are called they just have the way how much and how space will be allocated...

For father details, study role of stack frames in memory allocation or concept of stack frames in memory allocation to a program.

Asjid Ali
  • 57
  • 4