100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Inherited and polymorphism, class and object

Rating
-
Sold
-
Pages
113
Uploaded on
18-07-2025
Written in
2024/2025

I have written these all notes by me only and teach in the class. I have written theory and Syntax followed by examples. Please try to read at least one time. I am sure you can easily understand because I have written in very simple language. Thank you.

Show more Read less
Institution
Course











Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
July 18, 2025
Number of pages
113
Written in
2024/2025
Type
Class notes
Professor(s)
Self
Contains
All classes

Subjects

Content preview

Dynamic Memory Allocation (DMA) in C++

C++ allows us to allocate the memory dynamically in run time. This is known as dynamic memory
allocation.

In other programming languages such as Java and Python, the compiler automatically manages
the memories allocated to variables. But this is not the case in C++.
In C++, we need to deallocate the dynamically allocated memory manually after we have no use
for the variable.

We can allocate and then deallocate memory dynamically using the new and delete operators
respectively.

For storing the data, memory allocation can be done in two ways -

o Static allocation or compile-time allocation - Static memory allocation means providing space for the
variable. The size and data type of the variable is known, and it remains constant throughout the
program.
o Dynamic allocation or run-time allocation - The allocation in which memory is allocated dynamically.
In this type of allocation, the exact size of the variable is not known in advance. Pointers play a major
role in dynamic memory allocation.
Dynamically we can allocate storage while the program is in a running state, but variables cannot be created "on the
fly". Thus, there are two criteria for dynamic memory allocation -

o A dynamic space in the memory is needed.
o Storing the address to access the variable from the memory


Dynamic memory allocation using the new operator

o To allocate the space dynamically, the operator new is used. It means creating a request for memory
allocation on the free store. If memory is available, memory is initialized, and the address of that space is
returned to a pointer variable.

Syntax

Pointer_variable = new data_type;

The pointer_varible is of pointer data_type. The data type can be int, float, string, char, etc.

Example

int *m = NULL // Initially we have a NULL pointer

m = new int // memory is requested to the variable


Naseem Mirza
Assistant Professor
BCA Department Page 1

,It can be directly declared by putting the following statement in a line -

int *m = new int

Initialize memory

We can also initialize memory using new operator.

For example

int *m = new int(20);

Float *d = new float(21.01);

Allocate a block of memory

We can also use a new operator to allocate a block(array) of a particular data type.

For example

int *arr = new int[10]

Here we have dynamically allocated memory for ten integers which also returns a pointer to the first element of the
array. Hence, arr[0] is the first element and so on.

Note

o The difference between creating a normal array and allocating a block using new normal arrays is
deallocated by the compiler. Whereas the block is created dynamically until the programmer deletes it or
if the program terminates.


#include <iostream>
#include <memory>
using namespace std;
int main()
{
// pointer to store the address returned by the new
int* ptr;
// allocating memory for integer
ptr = new int;
// assigning value using dereference operator
*ptr = 10;
// printing value and address
cout << "Address: " << ptr << endl;
cout << "Value: " << *ptr;
return 0;
}

Address: 0x162bc20
Value: 10

Naseem Mirza
Assistant Professor
BCA Department Page 2

,delete Expression
Once we no longer need to use a variable that we have declared dynamically, we can deallocate
the memory occupied by the variable.

For this, we can use the delete expression. It returns the memory back to the operating system.
This is known as memory deallocation.
The syntax for delete expression is:

delete pointer_variable;



#include <iostream>
using namespace std;

int main() {

// dynamically allocate memory
int* point_int = new int{45};
float* point_float = new float{45.45f};

cout << *point_int << endl;
cout << *point_float << endl;

// deallocate the memory
// set pointers to nullptr
delete point_int;

delete point_float;

return 0;
}

45
45.45




Naseem Mirza
Assistant Professor
BCA Department Page 3

, // C++ program to store GPA of n number of students and display it
// where n is the number of students entered by the user
#include <iostream>
using namespace std;
int main() {

int num;
cout << "Enter total number of students: ";
cin >> num;
float* ptr;

// memory allocation of num number of floats
ptr = new float[num];

cout << "Enter GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}

cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": " << *(ptr + i) << endl;
}

// ptr memory is released
delete[] ptr;
ptr = nullptr;

return 0;
}
Run Code


Output

Enter total number of students: 4
Enter GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9

Displaying GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9



Naseem Mirza
Assistant Professor
BCA Department Page 4
$5.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
NaseeMirza

Also available in package deal

Get to know the seller

Seller avatar
NaseeMirza Freelancer
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
4 months
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions