In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program is created to handle product lists for a company. As new products are added to the list, it expands; as products are subtracted, it contracts. As the amount of data grows, the memory makes room for the new data items. Programmers need to use dynamic memory management strategies in these situations. You will learn how to dynamically allocate memory within a C++ program in this article.
When we think of creation, we start from scratch. However, this is not what happens when a computer creates a variable. Instead, the computer simply assigns a memory cell from one of the many already-existing memory cells to the variable. It's like selecting a hotel room to stay from a large number of open or vacant previously occupied rooms.
Ways of Memory Allocation in C++
There are two ways to allocate memory for data storage. Which are:
Static Memory Allocation
Memory allocation is done statically or at compile time when the compiler sets aside space for named variables. At the time of compilation, the precise size and storage must be known; for array declaration, the size must be constant.
Dynamic memory allocation
Runtime allocation, also known as dynamic memory allocation, refers to the process of allocating memory while a program is running. The free store or a heap are two names for the memory section. In this scenario, the compiler does not need to know in advance the precise space or number of the item.
While the program is running, programmers can dynamically allocate storage space. However, programmers are unable to invent new variable names "on the fly," so dynamic allocation needs to meet the following two requirements:
- There must be a dynamic space in the memory.
- preserving the address needed to retrieve the variable from memory
We can use the unary operator "new" followed by the type to dynamically allot space.
Deleting the dynamic memory space
The pointer's address is being saved (so that space can be accessed)
This idea also includes memory de-allocation, which involves "cleaning up" space from variables or other data storage. The task of de-allocating dynamically created space falls to the programmer. We employ the delete operator to release dynamic memory.
The C++ program divides memory into two categories:
Stack: The memory of the stack is occupied by all variables declared inside of any function.
Heap: This is the program's unused memory, and it can be used to dynamically allocate memory at runtime. Heap Memory is allocated dynamically when it is dynamically accessed. In order to locate memory in heap section, we typically allocate arrays rather than just one character, integer, float, or other types of data.
Example for dynamic memory allocation :
Here is an example of using new code:
using namespace std;
char* p = NULL; // Pointer initialized with NULL value
p = new char; // Request memory for the variable
cout<<" no memory is allocated";
cout<<"dynamic memory allocation done ";
dynamic memory allocation done
The term "new" denotes heap memory allocation and stack creation for arrays that are only declared. Any variables we declare inside the program or main function will receive memory inside the stack, regardless of where they are located. When we write something with new, the memory will be in a heap, so the address needs to be stored in an address variable. We can also write it like this because this is created in a heap. Another difference between heap and stack memory is that while heap memory does not automatically delete itself, the array is automatically created inside the stack and deleted once it exits the scope. Since it will remain there while your program is running, you should de-allocate it if you don't need it for the program or only need it temporarily. Heap memory must be de-allocated. This is crucial. When allocating resources, we write new. Later, if we no longer need them, we write delete p. Since p is an array, it should use the subscript symbol .