NU-Lec 2 - Dynamic Memory Allocation
NU-Lec 2 - Dynamic Memory Allocation
Lecture No. 2
Accessing Arrays
and
Dynamic Memory Allocation (DMA)
Relationship Between Pointers and Arrays
• Arrays and pointers are closely related
– Array name is like constant pointer
– All arrays elements are placed in the consecutive
locations. (This is only valid in static memory
allocation)
• Example:- int List [10];
• Other slots of the Array (List [50]) can be accessed 990 Element 5
Address Data
int List [ 50 ]; 980 Element 0
982 Element 1
int *Pointer;
984 Element 2
Pointer = List; // Address of first Element 986 Element 3
293
988 Element 4
990 Element 5
int *ptr; 992 Element 6
994 Element 7
ptr = Pointer + 3; // Address of 4th Element
996 Element 8
*ptr = 293; // 293 value store at 4th element …
address
…
998 Element 50
Accessing 1-Demensional Array
We can access all element of List [50] using
Pointers and for loop combinations. Address Data
int List [ 50 ]; 980 Element 0
982 Element 1
int *Pointer; Element 2
984
Pointer = List; 986 Element 3
988 Element 4
for ( int i = 0; i < 50; i++ )
990 Element 5
{ 992 Element 6
cout << *Pointer; 994 Element 7
996 Element 8
Pointer++; // Address of next element
…
} …
This is Equivalent to 998 Element 50
– int *ptr; 0 1 2 3 4 5
2nd column we can increment 3 336 338 340 342 344 346
Row
4 348 350 352 354 356 358
the value of (ptr). 5 360 362 364 366 368 370
– ptr++; // address of 4th row 2nd 6 372 374 376 378 380 382
7 384 386 388 390 392 394
column
8 396 398 400 402 404 406
– Equivalent to List [3][1] ;
Memory address
Accessing 2-Demensional Array
• We know computer can perform only one
Column
operation at any time (remember fetch-
decode-execute cycle). 0 1 2 3 4 5
Row
4 348 350 352 354 356 358
5 360 362 364 366 368 370
• But using pointer we can reach the element of 6 372 374 376 378 380 382
4th row 2nd column (directly) by increment our
7 384 386 388 390 392 394
pointer value (which is a single operation).
– ptr++; // 4th row 2nd column 8 396 398 400 402 404 406
– ptr+1; // 4th row 3rd column
– ptr+2; // 4th row 4th column
Memory address
Dynamic Memory Allocation
• Static Memory Allocation (SMA):-
• Used when space requirements are unknown at compile time.
• Most of the time the amount of space required is unknown at
compile time.
– For example consider the library database example having 200 data
set declared, what about 201th book information?
• a = new int[3];
– *a = 300;
– *(a+1) = 301;
– *(a+2) = 302;
Returning Memory to the Heap
• How Big is the Heap?
– Most applications request memory from the heap when
they are running
– It is possible to run out of memory (you may even have
gotten a message like "Running Low On Virtual
Memory")
– So, it is important to return memory to the heap when
you no longer need it
Another example
• Suppose you have the following declaration:
int *p;
This statement declares p to be a pointer variable
of type int. Next, consider the
following statements:
p = new int; //Line 1
*p = 54; //Line 2
p = new int; //Line 3
*p = 73;
Another example
Returning Memory to the Heap
• The Opposite of new:
– In C++: The delete operator in C++
• delete ptr;
Dangling Pointers
• The delete operator does not delete the pointer, it takes the
memory being pointed to and returns it to the heap
• Remember:
– Return memory to the heap before undangling the pointer
• e.g.
• delete ptr;
• Ptr=NULL;
Returning Memory to the Heap
• For Arrays
– delete[ ] a;
– a = NULL;
Memory Leaks
• Memory leaks when it is allocated from the heap
using the new operator but not returned to the
heap using the delete operator
• int *otherptr;
• otherptr = new int;
• *otherptr = 4;
• otherptr = new int;
Casting pointers (1/2)
int*
However, there is an exception to this. There is a
Pointer to Pointer
and
What's Wrong with the Following:
ptr = NULL;
delete ptr;