Lecture 6
Lecture 6
CS/CE 224/272
Nadia Nasir
int x = 10;
int* p =&x;
int** pp = &p;
Example
int main()
{
int x{ 5 };
return 0;
}
Dynamic Memory allocation
Memory Management
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.
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.
•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.
•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.
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 :
• 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 :
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** 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.