CSE225 Lecture 02 Dynamic Memroy Allocation
CSE225 Lecture 02 Dynamic Memroy Allocation
and Pointers
#include <stdio.h>
int main(void)
{
char str[5] = {'H', 'E', 'L', 'L', 'O'};
char *ptr = &str[0];
printf(“ptr = %08x\n”, ptr);
Arrays and Pointers Arrays and Pointers
#include <stdio.h> The array name is basically the name address content
of a pointer variable which contains 0x00000000
int main(void)
the starting address of the array 0x00000001
(address of the first element) .
.
{
str 0x180A96e7
char str[5] = {'H', 'E', 'L', 'L', 'O'}; str
0x180A96e8
0x180A96f3
0x180A96e9
char *ptr = &str[0];
0x180A96f0
printf(“ptr = %08x\n”, ptr); 0x180A96f1
‘H’ ‘E’ ‘L’ ‘L’ ‘O’
0x180A96f2
printf(“str = %08x\n”, str);
0x180A96f3 ‘H’
return 0; 0x180A96f4 ‘E’
0x180A96f5 ‘L’
}
0x180A96f6 ‘L’
Output: 0x180A96f7 ‘O’
ptr = 0028ff17
.
str = 0028ff17
.
Arrays and Pointers Arrays and Pointers
The array name is basically the name address content
of a pointer variable which contains 0x00000000
str
the starting address of the array 0x00000001
(address of the first element) .
.
str 0x180A96e7
str ‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’
0x180A96e8
0x180A96f3
0x180A96e9
0x180A96f0
0x180A96f1
‘H’ ‘E’ ‘L’ ‘L’ ‘O’
0x180A96f2
0x180A96f3 ‘H’
0x180A96f4 ‘E’ char str[6] = “HELLO";
c = str[2]; is equivalent to
0x180A96f5 ‘L’
c = *(str + 1 × 2); 0x180A96f6 ‘L’
0x180A96f7 ‘O’
Base offset .
.
Arrays and Pointers Arrays and Pointers
str str
‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’
ptr
str str
‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’
ptr ptr
Arrays and Pointers Arrays and Pointers
str str
‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’
ptr ptr
A 0x180A96e8
0x180A96f3
A
0x180A96e9
0x180A96f0
0x180A96f1
0x180A96f2
3 1 8 0x180A96f3 3 1 8
0x180A96f4
3
0x180A96f5
Base offset 0x180A96fB
int *ptr = A;
0x180A96fC
8
0x180A96fD
0x180A96fE
Arrays and Pointers Arrays and Pointers
int A[3] = {3, 1, 8}; int A[3] = {3, 1, 8};
A A
3 1 8 3 1 8
ptr ptr
3 1 8
ptr
int *ptr = A;
ptr = ptr + 2;
Arrays and Pointers Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}}; int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
A A
3 1 8 11 3 1 8 11 Array. Each element is an
int.
4 12 9 10 4 12 9 10
7 2 2 6 7 2 2 6
Arrays and Pointers Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}}; int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
A A
Pointer to a pointer to an int (equivalent to int **)
and points to the first element of an array of int pointers.
3 1 8 11 Array. Each element is an 3 1 8 11 Array. Each element is an
int. int.
4 12 9 10 4 12 9 10
7 2 2 6 7 2 2 6
Arrays and Pointers Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}}; int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A; int **p = A;
A A
p
3 1 8 11 3 1 8 11
4 12 9 10 4 12 9 10
7 2 2 6 7 2 2 6
Arrays and Pointers Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}}; int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A; int **p = A;
P[1][2] = 99; P[1][2] = 99;
A A
p p
3 1 8 11 3 1 8 11
4 12 9 10 4 12 99 10
7 2 2 6 7 2 2 6
Allocation of Memory Allocation of Memory
• Static Allocation: Amount of memory space required is • Memory map
determined in advance (that is, at the compilation time) High-end
Run-time allocated
Stack
and memory space is allocated to the program right
before the program is executed.
memory
For example, STACK - This area is used for
function calls return address,
int s; argument and local variables Heap
Compile-time
static data
HEAP – This area is used for
allocated
The operating system allocates 4 bytes of memory to the
memory
program before its execution. Dynamic Memory Allocation
Program
code
• Dynamic Allocation: Amount of memory space required Static memory – reserved for global
is determined at run time (that is, after the program starts and static variables live
executing). Memory space is allocated to the program at Low-end
run time too.
Dynamic Memory Allocation The new Operator
Dynamic allocation is useful when If memory is available, the new operator allocates
• arrays need to be created whose extent is not known until run memory space for the requested object/array, and
time returns a pointer to (address of) the memory allocated.
• complex structures of unknown size and/or shape need to be
constructed as the program runs Pointer = new DataType;
• objects need to be created and the constructor arguments are
not known until run time Pointer = new DataType [IntExpression];
Pointers are used for dynamic allocation of memory If sufficient memory is not available, the new operator
Use the operator new to dynamically allocate space returns NULL.
Use the operator delete to free this space later
allocated at run‐time by the new operator.
• the freed memory space is returned to Heap int *ptr; ptr FDE0
int *ptr; ptr FDE0 0EC4 int *ptr; ptr FDE0 0EC4
ptr = new int; FDE1 ptr = new int; FDE1
*ptr = 22; FDE2 *ptr = 22; FDE2
cout << *ptr << endl; cout << *ptr << endl;
FDE3 FDE3
delete ptr; delete ptr;
ptr = NULL; ptr = NULL;
0EC4 0EC4 22
Dynamically 0EC5 0EC5
allocated variable 0EC6 0EC6
0EC7 0EC7
Example Example
0EC4 22 0EC4
Output:
0EC5 0EC5
22 0EC6 0EC6
0EC7 0EC7
Example Example
int *grades = NULL;
int numberOfGrades;
Dynamic Allocation of 2D Arrays Dynamic Allocation of 2D Arrays
• A two dimensional array is really an array of arrays (rows). To allocate space for the 2D array with r rows and
• To dynamically declare a two dimensional array of int c columns:
type, you need to declare a pointer to a pointer as: You first allocate the array of pointers which will point to the
int **matrix; arrays (rows)
matrix = new int*[r];
This creates space for r addresses; each being a pointer to an
int.
Then you need to allocate the space for the 1D
arrays themselves, each with a size of c
for(i=0; i<r; i++)
matrix[i] = new int[c];
Example Memory Leak
// create a 2D array dynamically • When you dynamically create objects, you can access
int rows, columns, i, j; them through the pointer which is assigned by the new
int **matrix; operator
cin >> rows >> columns;
• Reassigning a pointer without deleting the memory it
matrix = new int*[rows];
pointed to previously is called a memory leak
for(i=0; i<rows; i++)
matrix[i] = new int[columns];
• It results in loss of available memory space
Memory Leak Memory Leak
Inaccessible memory location
Memory location that was allocated using new
ptr1 There is no pointer that points to this memory space
int *ptr1 = new int;
int *ptr2 = new int; 8 It is a logical error and causes memory leaks
*ptr1 = 8;
ptr2
*ptr2 = 5;
5
ptr2 = ptr1;
ptr1
8
ptr2
5
Dangling Pointer Dangling Pointer
A pointer that points to a memory location that has been
de‐allocated.
The result of dereferencing a dangling pointer is int *ptr1 = new int;
ptr1
unpredictable. 8
int *ptr2;
*ptr1 = 8; ptr2
ptr2 = ptr1;
delete ptr1;
ptr1
ptr2