Types of Allocation
Study types of allocation.
We'll cover the following
Introduction
In C++, we can allocate memory in two ways:
- Static allocation
- Dynamic allocation
Static allocation
In static allocation, a fixed amount of memory is allocated to the variables or arrays before the execution of the program (during compile-time), and we cannot request more memory while the program runs.
In static allocation:
- We must know the size of an array or variable during the compile time.
- Memory is allocated and deallocated to the variables by the compiler.
Dynamic allocation
Sometimes you will encounter a situation where you don’t know in advance how much memory is needed to store the data. Thus, dynamic allocation is needed.
Example program
Suppose you want to input a sentence from a user, but you don’t know the exact characters needed in an array.
Get hands-on with 1400+ tech skills courses.