0% found this document useful (0 votes)
5 views

2. [DStructure - Slides] Memory Management

Uploaded by

alieslam20014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2. [DStructure - Slides] Memory Management

Uploaded by

alieslam20014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Getting started with C++

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

• In C++, a block of memory refers to a contiguous array of bytes,


where each byte has a unique address.
• We can perform memory management in C++ with the use of two
operators:
• new operator to allocate a block of memory on heap
• delete operator to deallocate a block of memory from heap
C++ New Operator

• Using the C++ new operator, we can


allocate memory at the runtime. The
new operator in C++ is used for the
dynamic memory allocation; It is used
to allocate the memory at runtime on
heap memory.
• Syntax:
C++ Delete Operator
• Once the memory is allocated, we can delete the memory when it is
not required anymore. The delete operator in C++ is used for the
deallocation of memory.
• When we no longer need to use the variable, that means when the
memory is no longer required, we have to deallocate or release the
memory using the delete operator.
• Syntax:
Differences between delete and free()
• The following are the differences between delete and free() in C++ are:
Ø The delete is an operator that de-allocates the memory dynamically while the free() is a
function that destroys the memory at the runtime.
Ø The delete operator is used to delete the pointer, which is either allocated using new
operator or a NULL pointer, whereas the free() function is used to delete the pointer
that is either allocated using malloc(), calloc() or realloc() function or NULL pointer.
Ø When the delete operator destroys the allocated memory, then it calls the destructor of
the class in C++, whereas the free() function does not call the destructor; it only frees
the memory from the heap.
Ø The delete() operator is faster than the free() function
Advantages of the new operator

• 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.

Shown below is the output.


tasks
• Write C++ Program to store GPA of n number of students and display it
where n is the number of students entered by the user?

You might also like