-2

I am recently learning C/C++,but I feel confused about the difference between dynamic memory allocation and heap.so please tell me the difference between dynamic memory allocation and heap in c and c++

aMooly
  • 551
  • 5
  • 13

3 Answers3

3

Dynamic memory allocation differentiates itself with static memory allocation, so if you're comparing it with heap, it's a wrong comparison.

  • Dynamic and static allocation is "how" and "when" memory is allocated.
  • heap, stack, etc. is "where" the memory resides and affects how memory are managed in those areas

Static memory is allocated at program start-up, usually in the form of local or global variables. For example:

int globalVariable = 5;
int globalArray[ 2 ] = { 0, 1 };

void main()
{
    int localVariable = 2;
    int localArray[ 3 ] = { 1, 2, 3 };
}

These memory are allocated on the stack and program space.

Dynamic allocation occurs when the program execute functions like malloc or operators like "new". For example:

void main()
{
    int* pointerToInt = new int;
    int* pointerToArrayOfInts = new int[2];
    //do something
    delete pointerToInt;
    delete [] pointerToArrayOfInts;
}

It is important to note, the variable "pointerToInt" is statically allocated on the stack. However, the memory it points to, which stores an "int" is dynamically allocated when that line of code runs, which allocates the memory on the heap. Similarly for "pointerToArrayOfInt".

phandinhlan
  • 583
  • 1
  • 5
  • 16
2

In C and C++ there are 4 types of allocation. The term used to describe the allocation type of a variable is storage duration. The types are:

  • automatic (e.g. { int x; }
  • thread ( e.g. int thread_local x;)
  • static (e.g. static int x;)
  • (C++ only) dynamic (e.g. new int;)
  • (C only) allocated (e.g. malloc(4);)

The term heap is a general programming term, not a C++ one. It corresponds to dynamic storage duration in C++. It is called heap for historical reasons. C uses the term allocated instead of dynamic.

Dynamic storage lasts until you manually free it. Automatic storage lasts until the next } after the declaration. Static storage lasts until the end of the program. Thread storage lasts until the end of the thread.

M.M
  • 138,810
  • 21
  • 208
  • 365
0
  • Static memory allocation: the lifetime of the variable is automatic, i.e. the compiler is responsible to create and destroy the variable at very specific points in the program (when the variable gets created / goes out of scope)

  • Dynamic memory allocation: the creation and destruction of the object is handled by the programmer and can happen at any point in the execution of the program

  • stack: usual implementation on usual systems by usual compilers of the static memory allocation (and other things)

  • heap: usual implementation on usual systems by usual compilers for dynamic memory allocation

stack and heap are compiler handled memory zones in the address space of the program.

bolov
  • 72,283
  • 15
  • 145
  • 224