-7

Consider the code snippet:

ClassName* p;
p = new ClassName;

As I understand it, we are allocating memory from the heap to store *p.

But now consider:

ClassName C;

Question: If not from the heap, where does the memory for C come from?

George
  • 6,927
  • 4
  • 34
  • 67

2 Answers2

6

As I understand it, we are allocating memory from the heap to store *p.

More correctly worded, the object created by new has dynamic storage duration.

If not from the heap, where does the memory for C come from?

It has automatic storage duration.

See C++ standard §3.7/1.

Talking about "stack" or "heap" takes you on the compiler-implementation level. You are generally not interested in how the C++ compiler makes the different kinds of storage duration work but are only interested in their semantics.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
4

Question: If not from the heap, where does the memory for C come from?

From the stack (or in the case of a global variable or function 'static' variable, from a statically allocated region that exists independently of both the heap and the stack).

Strictly speaking, C++ has no concept of heap or stack (other than as data structures in the standard library, which are fundamentally different to "the" heap and stack that are the prevalent mechanisms used for allocation of memory by running programs); the exact mechanism for allocation and management of memory is not specified. In practice, there are two ways for a program to allocate memory at run time on most systems: from the heap (which is itself built on memory chunks obtained from the operating system), or from the stack (the same stack that is used to store the return address and save temporary values when functions are called).

Memory for the stack is usually allocated when (or just before) the program begins execution, and it usually needs to be contiguous. Memory for the heap is usually obtained dynamically and does not normally need to be contiguous (though in practice heap memory is often allocated in contiguous chunks).

Note that your first example:

ClassName* p;
p = new ClassName;

... actually embodies two allocations: one for the ClassName object (dynamic storage, allocated on the heap) and one for the pointer variable p (automatic storage, allocated on the heap).

In practice, local variables will not always require stack allocation - their values can sometimes be kept in registers (or in some cases, the storage can be optimised away altogether, especially if the value is unused or is a compile-time constant). And theoretically, heap allocations can also potentially be optimised away.

davmac
  • 20,150
  • 1
  • 40
  • 68
  • Does C++ get memory for the stack from the heap behind the scenes? – George Feb 05 '16 at 19:20
  • @George That's the thing for you to find out ;) – Ivan Aksamentov - Drop Feb 05 '16 at 19:20
  • @George No, not really. Stack is a fixed size limited resource for a program running on a CPU. – πάντα ῥεῖ Feb 05 '16 at 19:22
  • 1
    @George the stack is generally not considered to be part of the heap, no. Ultimately, however, all the memory for a program is usually provided by the operating system, and sometimes the way that stack memory is obtained is similar to the way that heap memory is obtained. There are some important differences though; stack must be contiguous. – davmac Feb 05 '16 at 19:23
  • @davmac What if you don't have an operating system but operate on bare metal? It's not specified from the c++ standard, and doesn't need to be. – πάντα ῥεῖ Feb 05 '16 at 19:25
  • @πάνταῥεῖ indeed it's not and it doesn't, but then, I never claimed it was. – davmac Feb 05 '16 at 19:27
  • A variable could be placed in the global area, depending on where it is defined. A variable defined outside of a function will most likely not be on the stack. – Thomas Matthews Feb 05 '16 at 19:38
  • Might be worth a small side-note to point out that the system may be implemented without stacks and heaps, but it's highly unlikely you'll ever see one. – user4581301 Feb 05 '16 at 19:54