2. [DStructure - Slides] Memory Management
2. [DStructure - Slides] Memory Management
memory management
Lecture 2
Introduction to Memory Management
• Memory management oversees how a program consumes computer memory.
During execution, every computer program uses main memory (i.e. RAM) to
store temporary variables, data structures, and so on. Managing memory
consumption involves both memory allocation and memory deallocation.
Memory allocation is when a portion of main memory is allocated by the
program’s request. Memory deallocation frees the memory that’s no longer
needed by the program.
• A programming language may provide one of two approaches to memory
management:
1. Automatic memory management (e.g. Java, Python, C#)
2. Dynamic memory management (e.g. C++, C)
Why Do We Need Memory Management,
and How Does It Work?
• Memory management is required to ensure that there is no wastage of memory and
that allocation takes place efficiently. The memory that a C++ program uses is divided
into different parts. Here, In C++, we can divide a program’s memory into three parts:
1. Static region, wherein static variables are stored. Static variables are variables that
remain in use throughout the execution of a program. The size of the static region does not
change during the C++ program’s execution.
2. Stack, wherein stack frames are stored. A new stack frame is created for every function
call. A stack frame is a frame of data that contains the corresponding function’s local variables
and is destroyed (popped) when that function returns.
3. Heap, wherein dynamically allocated memory is stored. To optimize memory utilization,
heap and stack typically grow towards each other, as illustrated in the following figure
• During the declaration of an array, there are times when the correct
memory is not determined until runtime. To avoid such scenarios, we
usually declare an array of the maximum size. However, because of this
full size, some memory remains unused. For example, let us suppose we
have declared an array of size 30, and after declaring the array, it turns out
that we only need space of 10 size, so the rest of the space is of no use, or
we can say it will get wasted.
• To avoid such cases, we use memory allocation. We can allocate the
memory at runtime from the heap using an operator.
• Memory management is a process of managing computer memory,
assigning the memory space to the programs to improve the overall
system performance.
Memory Management Operators
• The following are the advantages of the new operator over malloc() function:
Ø It does not use the sizeof() operator as it automatically computes the size of
the data object.
Ø It automatically returns the correct data type pointer, so it does not need to
use the typecasting.
Ø Like other operators, the new and delete operator can also be overloaded.
Ø It also allows you to initialize the data object while creating the memory
space for the object.
Now, let’s look at an example where we will be using both of these operators, and find
the average of three numbers entered by the user.
Shown below is the output.
New and Delete Operator for Arrays
• New and delete operators can also be used for dynamic memory allocation in
arrays. The syntax of new and delete operators for arrays is similar to that of
the variables.
• Syntax:
In this example, we will take the size of the array as an input from the user; the number of
elements inside the array is equal to the size of the array. Then, we will declare the pointer
variable, and allocate the memory for the complete array.
Once the memory allocation is done, we will take the array elements as input from the user.
After that, we will display those elements using the for loop, i.e., iterating from 0 to num.
Once the elements are printed, we will deallocate the memory using the delete operator.