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

CSE225 Lecture 02 Dynamic Memroy Allocation

Uploaded by

Fariha Mehzabin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

CSE225 Lecture 02 Dynamic Memroy Allocation

Uploaded by

Fariha Mehzabin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture 02

Dynamic Memory Allocation


CSE225: Data Structures
Arrays and Pointers
#include <stdio.h>

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 <

printf(“str = %08x\n”, str); // 0 means replaced blank space with 0

return 0;
}

// string sv char array

// string end with null char (‘\0’)

// char doesn’t end with null char


Arrays 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);
printf(“str = %08x\n”, str);
return 0;
}
Output:
ptr = 0028ff17
str = 0028ff17
Arrays and Pointers
The array name is basically the name address content

of a pointer variable which contains 0x00000000

the starting address of the array 0x00000001

(address of the first element) .


.
str
str 0x180A96e7
0x180A96e8
0x180A96f3
0x180A96e9
0x180A96f0

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ 0x180A96f1


0x180A96f2
0x180A96f3 ‘H’
0x180A96f4 ‘E’
0x180A96f5 ‘L’
0x180A96f6 ‘L’
0x180A96f7 ‘O’
.
.
Arrays and Pointers
The array name is basically the name address content

of a pointer variable which contains 0x00000000

the starting address of the array 0x00000001

(address of the first element) .


.
str
str 0x180A96e7
0x180A96e8
0x180A96f3
0x180A96e9
0x180A96f0

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ 0x180A96f1


0x180A96f2
0x180A96f3 ‘H’
c = str[2]; is equivalent to 0x180A96f4 ‘E’
c = *(str + 1 × 2); 0x180A96f5 ‘L’
0x180A96f6 ‘L’
0x180A96f7 ‘O’
Base offset .
.
Arrays and Pointers

str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

char str[6] = “HELLO";


Arrays and Pointers

str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

char str[6] = “HELLO";


char *ptr = str;
Arrays and Pointers

str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = “HELLO";


char *ptr = str;
Arrays and Pointers

str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = “HELLO";


char *ptr = str;
ptr[2] = ‘l’;
Arrays and Pointers

str

‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = “HELLO";


char *ptr = str;
ptr[2] = ‘l’;
Arrays and Pointers

str

‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = “HELLO";


char *ptr = str;
ptr[2] = ‘l’;
ptr = ptr + 2;
Arrays and Pointers

str

‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = “HELLO";


char *ptr = str;
NOTE:
ptr[2] = ‘l’; ● array & pointer are the same thing
● pointer ptr atcs as it is an array
ptr = ptr + 2; ● array index can be negative
Arrays and Pointers address
0x00000000
content

0x00000001

int A[3] = {3, 1, 8}; .

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}};

3 1 8 11 Array. Each element is an


int.
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}};

3 1 8 11 Array. Each element is an


int.
4 12 9 10

7 2 2 6

Array. Each element is a pointer to an int (equivalent to int *)


and points to the first element of an int array.
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 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.

3 1 8 11 Array. Each element is an


int.
4 12 9 10

7 2 2 6

Array. Each element is a pointer to an int (equivalent to int *)


and points to the first element of an int array.
Arrays and Pointers
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;

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.

• Dynamic Allocation: Allocation of memory space at run


time.
Dynamic Memory Allocation
▪ Dynamic allocation is useful when
• arrays need to be created whose extent is not known until run
time
• complex structures of unknown size and/or shape need to be
constructed as the program runs
• objects need to be created and the constructor arguments are
not known until run time
▪ Pointers need to be used for dynamic allocation of
memory
▪ Use the operator new to dynamically allocate space
▪ Use the operator delete to free this space later
The new Operator
▪ If memory is available, the new operator allocates
memory space for the requested object/array, and returns
a pointer to (address of) the memory allocated.
▪ If sufficient memory is not available, the new operator
returns NULL.
▪ The dynamically allocated object/array exists until the
delete operator destroys it.
The delete Operator
▪ The delete operator de-allocates the object or array
currently pointed to by the pointer which was previously
allocated at run-time by the new operator.
• the freed memory space is returned to Heap
• the pointer is then considered unassigned
▪ If the value of the pointer is NULL there is no effect.
Example

int *ptr; ptr FDE0


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

0EC4
0EC5
0EC6
0EC7
Example

int *ptr; ptr FDE0 0EC4


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

0EC4
0EC5
0EC6
0EC7
Example

int *ptr; ptr FDE0 0EC4


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

0EC4 22
0EC5
0EC6
0EC7
Example

int *ptr; ptr FDE0 0EC4


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

0EC4 22
Output:
0EC5
22 0EC6
0EC7
Example

int *ptr; ptr FDE0 ?


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

0EC4
0EC5
0EC6
0EC7
Example

int *ptr; ptr FDE0 0


ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl; FDE3
delete ptr;
ptr = NULL;

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;

cout << "Enter the number of grades: ";


cin >> numberOfGrades;
grades = new int[numberOfGrades];

for (int i = 0; i < numberOfGrades; i++)


cin >> grades[i];

for (int j = 0; j < numberOfGrades; j++)


cout << grades[j] << " ";

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

// deallocate the array


for(i=0; i<rows; i++)
delete [] matrix[i];
delete [] matrix;
Memory Leak
• When you dynamically create objects, you can access
them through the pointer which is assigned by the new
operator
• Reassigning a pointer without deleting the memory it
pointed to previously is called a memory leak
• It results in loss of available memory space
Memory Leak

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

You might also like