Posts

Showing posts from February, 2020

Dynamic Memory Allocation in C++

Image
Understanding of Dynamic memory allocation is very important for a programmer. It enables the programmer to make optimal use of the memory available. Let’s understand memory allocation from scratch.   A typical memory representation of a C program consists of following sections: text segment, initialized data segment, uninitialized data segment, stack and heap. We will not go into the details of the first three sections. However, we will have a look at stack and heap in detail. In C/C++ memory can be allocated based on either stack or heap. Memory from the stack is occupied by variables declared inside the function. The heap is the unused part of the memory of a program which can be used to allocate memory dynamically during runtime. The differences between them have been enlisted below. Stack Heap Memory is allocated in contiguous block. Memory is allocated in a random order Allocation and De-allocation is done automatically by ...