0% found this document useful (0 votes)
17 views15 pages

4 Dynamic Memory

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

4 Dynamic Memory

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Dynamic Memory

Allocation

1
Dynamic Memory

2
3
4
Memory Allocation

5
Object (variable) creation:
New Operator
The "new" operator in C++
The new operator in C++ is used to dynamically allocate a
block of memory and store its address in a pointer variable
during the execution of a C++ program if enough memory is
available in the system.

Syntax for "new" operator:


data_type * ptr_var = new data_type;
where
ptr is a pointer of type data_Type

Uninitialized int variable


Example
int* p = new int; 6
Allocation Error Handling

int* ptr = new(nothrow) int; if


(ptr == NULL)
{
cout << "Allocation Failed!\n";
}

7
Object (variable) destruction:

Delete Operator : The delete operator is used


to de-allocate the block of memory, which is
dynamically allocated using the new operator.
Syntax
delete p;
storage pointed to by p is returned to free
store and p is now undefined (Dangle
Pointer)
Example
int* p = new int;
*p = 10; 8
Memory leak is a serious bug!

9
Problem with Arrays
 Sometimes
 Amount of data cannot be predicted beforehand
 Number of data items keeps changing during program execution
 Example: Seach for an element in an array of N elements
 One solution: find the maximum possible value of N and allocate
an array of N elements
 Wasteful of memory space, as N may be much smaller in some
executions
 Example: maximum value of N may be 10,000, but a particular
run may need to search only among 100 elements
 Using array of size 10,000 always wastes memory in most

cases

10
Better Solution
 Dynamic memory allocation
 Know how much memory is needed after the program
is run
 Example: ask the user to enter from keyboard
 Dynamically allocate only the amount of memory
needed
 C++ provides functions to dynamically allocate
memory

11
Array Declaration Using the "new" Operator
The new operator can also be used to
allocate a block of memory (an array) of
any data_type.
Syntax:
data_type* ptr_var = new
data_type[size_of_the_array];

Example:
int* arr = new int[5];
12
Dynamically deallocating an array

dynamically deallocating the array using


delete operator
delete [] arr;

Garbage values will be displayed as the pointer will


point to some deallocated locations.
Arr pointer will be a dangle pointer.

13
Dynamic Memory Allocation in C++ for Objects
 We can dynamically allocate memory for objects also. When
we create an object of a class,
 a constructor function is invoked. Constructor is a member
function of a class that is used to initialize the object.
 Also, when the object goes out of scope or gets de-
allocated, a destructor function is invoked. Destructor is also
a class member function that helps in knowing when the
object's memory gets deleted.

14
Dynamic Memory Allocation in C++ for Objects

class Animal
{
public:
Animal() {
cout << "Animal class constructor invoked!" <<endl; }
~Animal() {
cout << "Animal class destructor invoked!" <<endl; }
};

int main() {
// memory allocated for dog object of type Animal
// constructor will be invoked
Animal* dog = new Animal;
// memory deallocated for dog object of type Animal
// destructor will be invoked
delete dog;
return 0; } 15

You might also like