CSE225 Lecture 02 Dynamic Memroy Allocation
CSE225 Lecture 02 Dynamic Memroy Allocation
int main(void)
{
char str[5] = {'H', 'E', 'L', 'L', 'O'};
char *ptr = &str[0];
8
printf(“ptr = %08x\n”, ptr); // 8 means 8 digit, blank space if the nub is <
return 0;
}
int main(void)
{
char str[5] = {'H', 'E', 'L', 'L', 'O'};
char *ptr = &str[0];
printf(“ptr = %08x\n”, ptr);
printf(“str = %08x\n”, str);
return 0;
}
Output:
ptr = 0028ff17
str = 0028ff17
Arrays and Pointers
The array name is basically the name address content
str
str
str
ptr
str
ptr
str
ptr
str
ptr
str
ptr
0x00000001
A .
A 0x180A96e7
0x180A96e8
0x180A96f3
0x180A96e9
0x180A96f0
3 1 8 0x180A96f1
0x180A96f2
0x180A96f3
i = A[2]; is equivalent to 0x180A96f4
3
0x180A96f5
i = *(A + 4 × 2); 0x180A96f6
0x180A96f7
0x180A96f8
Base offset 0x180A96f9
1
0x180A96fA
0x180A96fB
0x180A96fC
8
0x180A96fD
0x180A96fE
Arrays and Pointers
int A[3] = {3, 1, 8};
A
3 1 8
int *ptr = A;
Arrays and Pointers
int A[3] = {3, 1, 8};
A
3 1 8
ptr
int *ptr = A;
Arrays and Pointers
int A[3] = {3, 1, 8};
A
3 1 8
ptr
int *ptr = A;
ptr = ptr + 2;
Arrays and Pointers
int A[3] = {3, 1, 8};
A
3 1 8
ptr
int *ptr = A;
ptr = ptr + 2;
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
3 1 8 11
4 12 9 10
7 2 2 6
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
7 2 2 6
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
7 2 2 6
A
Pointer to a pointer to an int (equivalent to int **)
and points to the first element of an array of int pointers.
7 2 2 6
3 1 8 11
4 12 9 10
7 2 2 6
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
p
3 1 8 11
4 12 9 10
7 2 2 6
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
P[1][2] = 99;
p
3 1 8 11
4 12 9 10
7 2 2 6
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
P[1][2] = 99;
p
3 1 8 11
4 12 99 10
7 2 2 6
Allocation of Memory
• Static Allocation: Allocation of memory space at compile
time.
0EC4
0EC5
0EC6
0EC7
Example
0EC4
0EC5
0EC6
0EC7
Example
0EC4 22
0EC5
0EC6
0EC7
Example
0EC4 22
Output:
0EC5
22 0EC6
0EC7
Example
0EC4
0EC5
0EC6
0EC7
Example
0EC4
0EC5
0EC6
0EC7
Dynamic Allocation and De-allocation of
Arrays
• Use the new[integer_expression] statement to
create an array of elements instead of a single element.
• Use the delete [] statement to indicate that an array
of elements is to be deallocated.
Example
int *grades = NULL;
int numberOfGrades;
delete [] grades;
grades = NULL;
Dynamic Allocation of 2D Arrays
• A two dimensional array is really an array of arrays (rows).
• To dynamically declare a two dimensional array of int
type, you need to declare a pointer to a pointer as:
int **matrix;
Dynamic Allocation of 2D Arrays
● To allocate space for the 2D array with r rows and
c columns:
● You first allocate the array of pointers which will point to the
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
// create a 2D array dynamically
int rows, columns, i, j;
int **matrix;
cin >> rows >> columns;
matrix = new int*[rows];
for(i=0; i<rows; i++)
matrix[i] = new int[columns];//uneven array size is possible,if we write that line manually without using loop
ptr
int *ptr1 = new int;
1 8
int *ptr2 = new int;
*ptr1 = 8;
ptr
*ptr2 = 5; 2 5
ptr2 = ptr1;
ptr
1 8
ptr
2 5
Memory Leak
▪ Inaccessible memory location
▪ Memory location that was allocated using new
▪ There is no pointer that points to this memory space
▪ It is a logical error and causes memory leaks
Dangling Pointer
▪ A pointer that points to a memory location that has been
de-allocated.
▪ The result of dereferencing a dangling pointer is
unpredictable.
Dangling Pointer
ptr
int *ptr1 = new int; 1 8
int *ptr2;
*ptr1 = 8; ptr
ptr2 = ptr1; 2
delete ptr1;
ptr
1
ptr
2