0

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?

Travier
  • 205
  • 2
  • 9
  • Short answer. Yes, it is on the stack or somewhere equivalent. In this context "stack or somewhere equivalent" just means that it will be freed when the function exits. – Adrian Ratnapala Apr 19 '14 at 11:34
  • 1
    It is not the `*` which tells the compiler to use the heap. You can have `int* y = &x` which creates a pointer `y` on the stack and `y` points to `x` which is also on the stack. It is the `new` keyword which constructs objects on heap (and returns pointer to the newly created object). – eerorika Apr 19 '14 at 11:36

3 Answers3

5

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

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.
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Yes, that is what I meant. So at some way, some of the memory management in C++ is automatic, if you choose to declare variables on the stack, isn't it? And is it OK to declare on the stack, or do I need to always declare on the heap with the * sign? – Travier Apr 19 '14 at 11:34
  • Yes, stack memory is automatically freed. It is okay to declare on the stack unless you need that memory to persist beyond the lifetime of the function. – merlin2011 Apr 19 '14 at 11:35
  • OK, But if I declare a variable on the heap with the * in a method, I cannot use it outside of the method, right? Does it mean I have to declare it on the heap inside the class scope to use it in other methods? – Travier Apr 19 '14 at 11:38
  • @Travier, You aren't really declaring a variable on the heap. You are **allocating** an object on the heap, and declaring a **pointer** to that object whereever you are putting the declaration. – merlin2011 Apr 19 '14 at 11:42
  • You can do **new** inside any method, and just return a copy of the pointer using `return`, so anyone else can use your object. Just do not forget to `delete` it when you are done with it. – merlin2011 Apr 19 '14 at 11:43
1

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.

Christian Aichinger
  • 6,989
  • 4
  • 40
  • 60