Question

I'm a beginner programmer in C++, and have a question about variables.

If, for example, I have this method:

int wow() 
{
    int x = 99;
    return 0;
}

When this method exits (returns 0), does it destroy the variable x?

And is this variable stored in the stack or the heap?

I know for example you could do int *x = new int(99), and then it would be in the heap.

But without the *, is it inside the stack?

And in the above method, when it exits, is x destroyed?

Was it helpful?

Solution

When this method exits (returns 0), does it destroy the variable x?

Yes. x has automatic storage duration, and is destroyed when it goes out of scope - in this case, when the function returns.

And is this variable stored in the stack or the heap?

On the stack.

I know for example you could do int *x = new int(99), and then it would be in the heap.

Indeed; the object created by new has dynamic storage duration, and is on the heap. Note that there's a second object x, a pointer to the object on the heap. x itself is an automatic object on the stack. (Pedantically, C++ calls the source of dynamic memory the "free store", not the "heap").

But without the *, is it inside the stack?

Without the *, that wouldn't compile. new creates an object in memory taken from the heap, and gives the address of it. * indicates that x is a pointer, so can hold that address. Without it, it would be int and couldn't hold the address.

And in the above method, when it exits, is x destroyed?

The pointer x is; but the object it points to is not. Dynamic objects (created with new) last until they are explicitly destroyed with delete. If that never happens, then you have a memory leak - memory was allocated but never released.

Avoid dynamic allocation unless you really need it; and when you do, always use smart pointers, containers, etc. to manage it safely, and avoid using new and juggling raw pointers.

OTHER TIPS

In your case, x will typically be kept in a processor register and not take up any memory at all (with an optimizing compiler). If the compiler needs to free the register, it would spill the variable to the stack.

The compiler never automatically places objects on the heap.

int x = 99; declares a variable on the stack.

It is "destroyed" when the function returns in the sense that the memory is freed for use by other functions, but there is usually no explicit wipe.

To be more explicit about destroy, it means that we cannot reliably expect the same value to be there the next time we look at that memory location. However, because there is no explicit overwrite, there is some chance it will be there the next time we look at the memory location.

Clarification about the * symbol when used in declarations.

int *ptr declares a pointer to integer, but this pointer can point at the stack or the heap, depending on where you decide to point it.

Heap:

int* ptr = new int(100);
int* ptr2 = ptr; // Now you have two pointers to the same location in the heap

Stack

int stackMe = 100;
int* ptrToStack = &stackMe; // Point ptrToStack at an `int` on the stack.

In either case, you can modify the value through the pointer.

*ptr = 5; // set the `int` that ptr points to, to 5.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top