Pepperwork
Pepperwork

Reputation: 105

Understanding of variable allocations

Could you help me understand where variables are stored (stack, heap, static memory). How can I determine it? I mean not intuitively but I would like to have some sign printed on the screan what is where. Is it possible?

So far I tried to have a look at where the variables are stored by printing their addresses. But that didn't give me much. Could you have a look and give me a piece of advice. If I've made a mistake (see my comments to the program), please tell me about that.

#include "stdafx.h"
#include <iostream>
using namespace std;

int * p1 = new int [3]; // Static memory as it is a global array;
namespace P {int * p2 = new int[3];} // Static memory as it is a namespace; 
namespace {int * p3 = new int[3];} // Static memory as it is a namespace;
using namespace P;

int _tmain(int argc, _TCHAR* argv[])
{
    int * p4 = new int[3]; // Heap as there is a new operator.
    static int * p5 = new int [3]; // Static memory as the variable is static.

    cout << "p1: "<< p1 << endl;
    cout << "p2: "<< p2 << endl;
    cout << "p3: "<< p3 << endl;
    cout << "p4: "<< p4 << endl;
    cout << "p5: "<< p5 << endl;
    cout << endl; 
    cout << "p5 - p4: " << p5 - p4 << endl;
    cout << "p4 - p3: " << p5 - p4 << endl;
    cout << "p3 - p2: " << p5 - p4 << endl;
    cout << "p2 - p1: " << p5 - p4 << endl;

    system("pause");
}

Upvotes: 2

Views: 229

Answers (2)

Eric Lippert
Eric Lippert

Reputation: 660058

The storage used for a particular variable is an implementation detail of your compiler; a compiler implementation is entirely within its rights to use any storage mechanism it chooses so long as the minimum requirements for variable lifetime is met.

You've identified three common storage areas: the dynamically-allocated long-term store, aka "the heap", the dynamically-allocated short-term store, aka "the stack", and the statically-allocated long-term store, aka "static memory". You have omitted registers; everyone always omits registers for some reason. And of course, the idea that there is one heap is nonsense; there can be many heaps, each with their own dynamic allocation strategy.

A variable refers to a storage location which stores a value. Where the variable lives depends on how it is created. Variables which are created by allocating memory off of the heap are, obviously, on the heap. Variables that are long-lived and of known size, such as static variables, are typically allocated out of static memory; as an implementation detail, that memory might be allocated out of some heap, but a compiler is not required to do so.

Short-lived local variables are the interesting ones; these can be allocated on the stack or in registers, at the whim of the compiler. Clearly if you take the address of a local variable then it cannot be enregistered, since registers have no address.

In your particular example you've made twenty variables. p1, p2, p3 and p5 will likely be allocated out of static memory. You never take the address of p4, so p4 is probably enregistered. If it is not enregistered then it is probably on the stack. All the other variables (p1[0], p1[1], p1[2], ... ) are allocated on the heap because you explicitly heap-allocated them.

Does that make sense?

Upvotes: 11

juanchopanza
juanchopanza

Reputation: 227420

Only the pointer is static here. The stuff it points to is on "the heap", meaning it is dynamically allocated and the caller needs to take care of de-allocating. Where the data actually is is another matter. It depends on the platform, and on what new is defined to do.

static int * p5 = new int [3]; 

Have a look at this GotW. Thanks to @AlokSave for posting it in the comments.

Upvotes: 2

Related Questions