The following code creates an object on the stack:
Object o;
When creating an object on the heap we can use:
Object* o;
o = new Object();
rather than:
Object* o = new Object();
When we split the heap object-creation over two lines and call the constructor on the second line (o = new object()
), does this mean in the first line (Object* o
) the pointer was created on the stack? So Object o
puts the object on the stack, whereas Object* o
puts the pointer to a future object on the stack?
My second question involves if the two lines of code were called outside of a class. I recently read (Global memory management in C in stack or heap?) that global variables are not contained on the stack/heap but actually another part of memory? If this is the case, would Object* o
create a pointer which would sit in this other part of the memory and it points to the heap object?
In both your examples, local variables of
Object*
type are allocated on the stack. The compiler is free to produce the same code from both snippets if there is no way for your program to detect a difference.The memory area for global variables is the same as the memory area for static variables - it's neither on the stack nor on the heap. You can place variables in that area by declaring them
static
inside the function. The consequence of doing so is that the instance becomes shared among concurrent invocations of your function, so you need to carefully consider synchronization when you use statics.Here is a link to a discussion of the memory layout of a running C program.
The two forms are the same with one exception: temporarily, the new
(Object *)
has an undefined value when the creation and assignment are separate. The compiler may combine them back together, since the undefined pointer is not particularly useful. This does not relate to global variables (unless the declaration is global, in which case it's still true for both forms).Actually, neither statement says anything about heap or stack:
creates an object with automatic storage meaning that the storage location is determined by the context in which the object is declared: If the code is in a function, this happens to be the call stack. But the line could also be a class member or, as you’ve noted, outside of a function / class.
To illustrate why this is different:
Now the object
pf->o
is created on the heap, not on the stack, even though (or rather, because) it has automatic storage.Conversely,
simply declares a pointer, nothing more. The pointer’s storage is indistinguishable from any other object’s: it has automatic storage. Furthermore, the initialising expression has no effect on the variable storage.
What the pointer points to is a completely different matter. It might be a heap-allocated object (using
new
for instance) or it might point to another automatically allocated object. Consider:A)
`` B)
I think A and B has no difference. In both the cases o is a pointer to class Object. statement new Object() creates an object of class Object from heap memory. Assignment statement assigns the address of allocated memory to pointer o.
One thing I would like to mention that size of allocated memory from heap is always the sizeof(Object) not sizeof(Object) + sizeof(void *).
C++ offers three different ways to create objects:
Consider your case,
and:
Both forms are the same. This means that a pointer variable o is created on the stack (assume your variables does not belong to the 3 category above) and it points to a memory in the heap, which contains the object.