S
sarathy
Hi all,
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.
1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.
2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.
2.1 Do they go in heap or in stack?
2.1.1 If they go in heap, then how will the local stack variable gain
access to an object in heap. Does the compiler maintain any object
reference internally? ( I have read about memory allocation in Java,
where in object references are being stored in the variable rather than
the object itself. Hence it is completely OK, since the local reference
in method can refer to an object in heap, since they have the reference
available with them.)
2.1.2 If they go in stack, then can objects reside in Stack?
3. Will 2 have the same effect on the following 2 cases.
Object a;
Object b = new Object();
i.e Is the memory allocation for both Object a and b same. I
understand that the memory created by "new" should be explicitly
destroyed using "delete". [Let us assume that i am not overloading new
and delete]. But other than that, is there any difference between
memory allocation for object a and b. If no, then how will the rules in
2 affect them.
4. I've read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?
Thanks and regards,
Sarathy
I need a few clarifications regarding memory allocaion in C++.
I apologize for the lengthy explanation.
1. In C++, Objects are allocated in heap.
What does heap refer to? Is it an area in RAM/Memory or does it refer
to a data structure being used for storing objects.
2. In C++, functions and its local variables go in stack.
If local variables that are primitives go in stack, it is OK. But what
about local variables that are objects.
2.1 Do they go in heap or in stack?
2.1.1 If they go in heap, then how will the local stack variable gain
access to an object in heap. Does the compiler maintain any object
reference internally? ( I have read about memory allocation in Java,
where in object references are being stored in the variable rather than
the object itself. Hence it is completely OK, since the local reference
in method can refer to an object in heap, since they have the reference
available with them.)
2.1.2 If they go in stack, then can objects reside in Stack?
3. Will 2 have the same effect on the following 2 cases.
Object a;
Object b = new Object();
i.e Is the memory allocation for both Object a and b same. I
understand that the memory created by "new" should be explicitly
destroyed using "delete". [Let us assume that i am not overloading new
and delete]. But other than that, is there any difference between
memory allocation for object a and b. If no, then how will the rules in
2 affect them.
4. I've read that C++ maintains, 2 memory locations to store objects
namely "heap" and "free store". Is there any big deal in it?
Thanks and regards,
Sarathy