C++ Memory Management
C++ Memory
management
The computer
system has a finite memory available to allocate to multiple processes. When memory
is inefficiently distributed then the number of processes that can be run simultaneous
decreases, also programs are cleared from or loaded to memory more often
depending on their priority leading to the system feeling sluggish to the user.
To tackle this problem, we follow memory management principles and practices. Memory
management controls and coordinated the allocation of memory blocks to the
various running programs in order to optimize the system.
- Memory management can be implemented in the hardware, OS and application levels.At hardware level, it involves the primary and secondary memory modules such as RAM, SSD, HDD, etc.
- At OS level, it involves distribution of memory for various processes while reserving some memory for OS processes.
- At Application level, it involves the request and management of memory blocks required by the program itself.
To solve
this issue, memory management operators are used.
The output for example was -
- In C, malloc() and calloc() functions are used to allocate memory dynamically at run-time. And free() function is used to de-allocate the dynamically allocated memory.
- malloc() – only allocated memory.
- calloc() – allocated memory and initializes it to zero.
- In C++, new and delete operators are used to allocate and de-allocate memory respectively. By using the cstdlib.h library, malloc() and calloc() can also be used in C++.
new operator denotes a request for
memory allocation on the Heap. If the request is granted depending on the
availability of memory, then it returns the address of allocated memory.
- Syntax – pointer-variable = new data-type;
- Initialize memory while allocation – pointer-variable = new data-type(value);
- Allocation of block of memory – pointer-variable = new data-type[size];
Delete operator is used to de-allocate dynamically
allocated memory.
- Syntax – delete pointer-variable;
- De-allocate dynamically allocated memory block – delete[] pointer-variable;
The output for example was -
-Aadhiraj More
TY - K51
Explained in easy way, good job
ReplyDeleteThank you
Delete