You are on page 1of 18

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-1


Bachelor of Engineering (Computer Science & Engineering)
Subject Name and Code: Object-oriented Programming using
C++
CSP-157
Prepared by Ms. Jasleen Kaur
Dynamic Memory allocation DISCOVER . LEARN . EMPOWER
CHAPTER -8
Course Outcome
CO Title Level
Number

CO1 provide the environment that allows students to Remember


understand object-oriented programming Concepts.   Will be covered in this
CO2 demonstrate basic experimental skills for Understand lecture
differentiating between object-oriented and  
procedural programming paradigms and the
advantages of object-oriented programs.

CO3 Ability to demonstrate their coding skill on complex Understand


programming concepts and use it for generating
solutions for engineering and mathematical problems.
CO4 Students will develop skills to design the application of Understand
classes, objects, constructors, destructors, inheritance,  
operator overloading and polymorphism, pointers,
virtual functions, templates, exception handling, file
operations and handling
2
• New and delete operator

CONTENTS

Dynamic memory allocation in C/C++ refers to


performing memory allocation manually by
programmer.

3
new OPERATOR
• The new operator denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator
initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.
• Syntax to use new operator: To allocate memory of any data type, the syntax is:
• pointer-variable = new data-type;
• pointer-variable = new data-type(value);
• Example:
• int *p = new int(25);
• float *q = new float(75.25);

4
delete OPERATOR
• Since it is programmer’s responsibility to deallocate dynamically allocated memory, programmers are provided delete
operator by C++ language.
• Syntax:
• // Release memory pointed by pointer-variable
• delete pointer-variable;
• Here, pointer-variable is the pointer that points to the data object created by new.
• Examples:
• delete p;
• delete q;

5
delete OPERATOR
• To free the dynamically allocated array pointed by pointer-variable, use following form of delete:
• // Release block of memory
• // pointed by pointer-variable
• delete[] pointer-variable;

• Example:
• // It will free the entire array
• // pointed by p.
• delete[] p;

6
PROGRAM
• #include <iostream> • int main()
• using namespace std; • {
• class Random • Random* a = new Random[3];
• { • delete [] a; // Delete array
• public: • return 0;
• Random() { • }
• cout << "Constructor" << endl;
• }
• ~Random() {
• cout << "Destructor" << endl;
• }
• };

7
WHAT IF ENOUGH MEMORY IS NOT
AVAILABLE DURING RUNTIME?
• If enough memory is not available in the heap to allocate, the new request indicates failure by throwing an exception of type
std::bad_alloc, unless “nothrow” is used with the new operator, in which case it returns a NULL pointer.
• Therefore, it may be good idea to check for the pointer variable produced by new before using it program.

Figure 8.1 Program made on Dev-C++

8
New CALLS CONSTRUCTOR & delete
CALLS DESTRUCTOR

Figure 8.3 Program made on Dev-C++ 9


EXAMPLE

Figure 8.3 Program made on Dev-C++ 10


EXAMPLE

Figure 8.3 Program made on Dev-C++ 11


EXAMPLE

Output

Figure 8.4 Program made on Dev-C++ 12


DYNAMIC OBJECT CALLS PRIVATE
CONSTRUCTOR
• #include <iostream> • int main()
• using namespace std; • {
• class Random • Random* a = new Random[3];
• { • delete [] a; // Delete array
• public: • return 0;
• Random() { • }
• cout << "Constructor" << endl;
• }
• ~Random() {
• cout << "Destructor" << endl;
• }
• };

Figure 8.5 Program made on Dev-C++ 13


DYNAMIC OBJECT CALLS PRIVATE
CONSTRUCTOR
• #include <iostream> • int main()
• using namespace std; • {
• class Random • Random* a = new Random[3];
• { • delete [] a; // Delete array
• public: • return 0;
• Random() { • }
• cout << "Constructor" << endl;
• }
• ~Random() {
• cout << "Destructor" << endl;
• }
• };

Figure 8.6 Program made on Dev-C++ 14


Assessment Pattern
Section-A
1. (a) Define dynamic allocation
(b) What is new operator?
(c) Differentiate between new and malloc.
Section-B
2. Write a C++ program to demonstrate the concept of new and delete.
3. WAP in C++ to demonstrate the difference between dynamic and non dynamic object.
4. Write a C++ program to use array of dynamic objects.

15
APPLICATIONS

• One use of dynamically allocated memory is to allocate memory of variable size which is not possible with compiler
allocated memory except variable length arrays.
• The most important use is flexibility provided to programmers. We are free to allocate and deallocate memory whenever we
need and whenever we don’t need anymore. There are many cases where this flexibility helps. Examples of such cases are
Linked List, Tree, etc.

16
REFERENCES

• Reference Website
[1] https://www.geeksforgeeks.org/pointers-c-examples/
[2] https://techdifferences.com/difference-between-static-and-dynamic-binding.html
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
[4] https://www.softwaretestinghelp.com/runtime-polymorphism-in-cpp/

17
THANK YOU

For queries
Email: CST157_2019@gmail.com

You might also like