0% found this document useful (0 votes)
23 views12 pages

CSE225 Lecture 02 Dynamic Memroy Allocation

The document discusses arrays and pointers in C. It contains the following key points: 1) An array name is equivalent to a pointer to the first element of the array. Setting a pointer to the array name makes it point to the start of the array. 2) Array subscript notation is equivalent to pointer arithmetic. Accessing an element through its index is the same as accessing it through a pointer offset from the base address. 3) Pointers can be used to modify array elements by treating the pointer as an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

CSE225 Lecture 02 Dynamic Memroy Allocation

The document discusses arrays and pointers in C. It contains the following key points: 1) An array name is equivalent to a pointer to the first element of the array. Setting a pointer to the array name makes it point to the start of the array. 2) Array subscript notation is equivalent to pointer arithmetic. Accessing an element through its index is the same as accessing it through a pointer offset from the base address. 3) Pointers can be used to modify array elements by treating the pointer as an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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

Lecture 02 printf(“str = %08x\n”, str);


return 0;
Dynamic Memory Allocation }
CSE225: Data Structures

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

char str[6] = “HELLO"; char str[6] = “HELLO";


char *ptr = str; char *ptr = str;
Arrays and Pointers Arrays and Pointers

str str

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

ptr ptr

char str[6] = “HELLO"; char str[6] = “HELLO";


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

Arrays and Pointers Arrays and Pointers

str str

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

ptr ptr

char str[6] = “HELLO"; char str[6] = “HELLO";


char *ptr = str; char *ptr = str;
ptr[2] = ‘l’; ptr[2] = ‘l’;
ptr = ptr + 2; ptr = ptr + 2;
Arrays and Pointers address
0x00000000
content
Arrays and Pointers
0x00000001
.
int A[3] = {3, 1, 8}; . int A[3] = {3, 1, 8};
A 0x180A96e7

A 0x180A96e8
0x180A96f3
A
0x180A96e9
0x180A96f0
0x180A96f1
0x180A96f2

3 1 8 0x180A96f3 3 1 8
0x180A96f4
3
0x180A96f5

i = A[2]; is equivalent to 0x180A96f6


0x180A96f7
i = *(A + 4 × 2); 0x180A96f8
1
0x180A96f9
0x180A96fA

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

int *ptr = A; 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] = {3, 1, 8};
A

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

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


and points to the first element of an int array. and points to the first element of an int array.

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

The delete Operator Example


 The delete operator de‐allocates the object or array  Statically allocated 
currently pointed to by the pointer which was previously  variable

allocated at run‐time by the new operator.
• the freed memory space is returned to Heap int *ptr; ptr FDE0

• the pointer is then considered unassigned ptr = new int; FDE1


*ptr = 22; FDE2
delete    Pointer; cout << *ptr << endl;
FDE3
delete ptr;
delete  []   Pointer; ptr = NULL;

 If the value of the pointer is NULL there is no effect. 0EC4


0EC5
0EC6
0EC7
Example Example

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

int *ptr; ptr FDE0 0EC4 int *ptr; ptr FDE0 ?


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 22 0EC4
Output:
0EC5 0EC5
22 0EC6 0EC6
0EC7 0EC7
Example Example
int *grades = NULL;
int numberOfGrades;

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


int *ptr; ptr FDE0 0 cin >> numberOfGrades;
ptr = new int; FDE1 grades = new int[numberOfGrades];
*ptr = 22; FDE2 for (int i = 0; i < numberOfGrades; i++)
cout << *ptr << endl; cin >> grades[i];
FDE3
delete ptr;
for (int j = 0; j < numberOfGrades; j++)
ptr = NULL; cout << grades[j] << " ";

0EC4 delete [] grades;


grades = NULL;
0EC5
0EC6
0EC7

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

// deallocate the array


for(i=0; i<rows; i++)
delete [] matrix[i];
delete [] matrix;

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy