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

Lecture 6

OOP Lecture 6

Uploaded by

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

Lecture 6

OOP Lecture 6

Uploaded by

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

Object Oriented Programming –

CS/CE 224/272

Nadia Nasir

Habib University, Fall 2024


Pointers to pointers
Pointers to pointers

A normal pointer to an int is


declared using a single asterisk:
Int x = 10;
int* p = &x;

A pointer to a pointer to an int is


declared using two asterisks

int x = 10;
int* p =&x;
int** pp = &p;
Example

int main()
{

int x{ 5 };

int* ptr { &x };


std::cout << *ptr << '\n'; // Dereference pointer to int to get
value of variable of x

int** ptrptr { &ptr };


std::cout << **ptrptr << '\n'; // dereference to get pointer to
int, dereference again to get value of variable x

return 0;
}
Dynamic Memory allocation
Memory Management

Memory management is the process of controlling and coordinating computer memory,


assigning portions called blocks to various running programs to optimize overall system
performance.

Memory management is the functionality of an operating system which handles or


manages primary memory and moves processes back and forth between main memory
and disk during execution.
Types of memory allocation

C++ supports three basic types of memory allocation, of which you’ve already seen
two.
•Static memory allocation
happens for static and global variables.
Memory for these types of variables is allocated once when your program is run and persists
throughout the life of your program.
•Automatic memory allocation
happens for function parameters and local variables.
Memory for these types of variables is allocated when the relevant block is entered, and freed
when the block is exited, as many times as necessary.

Both static and automatic allocation have two things in common:


The size of the variable / array must be known at compile time.
Memory allocation and deallocation happens automatically (when the variable is instantiated / destroyed).
•Dynamic memory allocation : where the memory is allocated at runtime and the
allocation of memory space is done dynamically within the program run .
In this case, the exact space or number of the item does not have to be known by the compiler in
advance. Pointers play a major role in this case.
Stack is used for static memory allocation and

Heap for dynamic memory allocation, both stored in the computer's RAM .
Stack:

•Variables created on the stack will go out of scope and are automatically
deallocated.

•Much faster to allocate in comparison to variables on the heap.

•Implemented with an actual stack data structure.

•Stores local data, return addresses, used for parameter passing.

•Can have a stack overflow when too much of the stack is used (mostly from infinite
or too deep recursion, very large allocations).

•You would use the stack if you know exactly how much data you need to allocate
before compile time and it is not too big.

•Usually has a maximum size already determined when your program starts.
Heap:

•In C++, variables on the heap must be destroyed manually and never fall out of scope.
The data is freed with delete, delete[], or free.

•Slower to allocate in comparison to variables on the stack.

•Used on demand to allocate a block of data for use by the program.

•Can have fragmentation when there are a lot of allocations and deallocations.

•In C++ or C, data created on the heap will be pointed to by pointers and allocated
with new or malloc respectively.

•You would use the heap if you don't know exactly how much data you will need at run
time or if you need to allocate a lot of data.

•Responsible for memory leaks.


Dynamic Memory Allocation – new operator

In C++, the pointer support dynamic memory allocation (allocation of memory during
runtime).
If we wish to allocate memory as and when required new operator helps in this context.
• The syntax of the new operator is given below :

data_type pointer_variable = new data_type;


• Where the data type is any allowed C++ data type and the pointer_variable is a
pointer of the same data type.
For example,
1. char * cptr = new char;
The above statements allocate 1 byte and assigns the address to cptr.
2. int* iptr = new int;
The above statements allocate 4 bytes and assigns the address to iptr.

• We can also allocate and initialize the memory in the following way :

For example,
char *cptr = new char (‘j’);
int* iptr = new int{3};
One-dimensional dynamically allocated arrays

One dimensional fixed array, which can easily be declared like this:
int a[5] = {1,2,3,4,5};
Dynamically allocating a one-dimensional array use pointer and new operator:
int* a = new int[5] ;
Examples:

1. The following statement allocates 21 bytes of memory and assigns the starting
address to cptr :
char * cptr;
cptr = new char [21];

2. The following statement allocates 20 bytes of memory and assigns the starting
address to iptr :

int* iptr = new int;


iptr = new int[5];
Deallocating the memory- delete operator

When we are done with a dynamically allocated variable, we need to explicitly tell C++
to free the memory for reuse.
delete operator is used to deallocate the memory.
The syntax of delete operator is :
delete_pointer_variable;
For example,
delete[] cptr;
cptr= nullptr;

delete[] iptr ;
iptr = nullptr;
The delete operator does not actually delete anything. It simply returns the memory
being pointed to back to the operating system.

The operating system is then free to reassign that memory to another application (or to
this application again later).
Memory Leaks . Example code discussed in class.
Dangling pointers. Example code discussed in class.
Arrays of pointers

Pointers to pointers have a few uses. The most common use is to dynamically allocate
an array of pointers:

int** array { new int*[10] }; // allocate an array of 10 int pointers


Two-dimensional dynamically allocated arrays

First, we allocate an array of pointers (as per above).

int** p{ new int*[4] }; // allocate an array of 4 int pointers — these are our rows

Then we iterate through the array of pointers and allocate a dynamic array for each
array element.

for (int count { 0 }; count < 4; ++count)


p[count] = new int[3]; // these are our columns

Our dynamic two-dimensional array is a dynamic one-dimensional array of dynamic


one-dimensional arrays!

You might also like