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

Chapter 4 - Pointers2

The document explains the concept of arrays in C++, including how array names act as constant pointers and how to manipulate arrays using pointers. It covers dynamic memory allocation using 'new' and 'delete', the internal structure of a process, and how to pass arrays as arguments to functions. Additionally, it provides examples of working with single and multi-dimensional arrays, including initializing and displaying their values.

Uploaded by

hadytarabay12
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)
11 views

Chapter 4 - Pointers2

The document explains the concept of arrays in C++, including how array names act as constant pointers and how to manipulate arrays using pointers. It covers dynamic memory allocation using 'new' and 'delete', the internal structure of a process, and how to pass arrays as arguments to functions. Additionally, it provides examples of working with single and multi-dimensional arrays, including initializing and displaying their values.

Uploaded by

hadytarabay12
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/ 18

The name of an array is a constant pointer

int t[10]
 t is equivalent to &t[0]
 t is a constant pointer to integers
 The following notations are the same
t+1 same as &t[1]
t+i same as &t[i]
t[i] same as *(t+i)
 These are two ways to store the value 1 to each of 10 elements of the array t
using pointers

int i ; int i ;
for (i=0;i<10;i++) int *p; //variable pointer
*(t+i) = 1 ; for (p = t, i=0 ; i<10 ; i++, p++)
*p = 1 ;
Remark: we cannot do t++
because t is a constant pointer cout<<p[2]; //correct 1
Example

int A[5] = {5, 8, 2, 1, 0};

cout<< A; →&A[0]

cout<< *A; →A[0] →5

cout<< A + 1; →&A[1]

cout<< *A + 1; →6 = A[0] + 1

cout << *(A+1) →8 = A[1]

cout<<*(A+i) →A[i]

2
Exp

int A[5] = {5, 8, 2, 1, 0};

for(i=0; i<5; i++)


Method1
cout << A[i];

Method2 for(i=0; i<5; i++)


cout << *(A+i);

int *p;
Method3 for(i=0, p=A; i<5; i++,p++)
cout << *p;

int *p;
Method4 for(p=A; p< A+5; p++)
cout << *p; 3
TYU
Write a function that asks the user to input an array of n integers
The array is by default passed by address. The name of the array is a pointer to the
1st element = The name is the address of the 1st element
void inputArray (int A[], int n)
{
int i;
for(i=0;i<n;i++)
cin>>A[i]; //cin >> *(A+i);
}

void inputArray (int *A, int n)


{
int i;
for(i=0;i<n;i++)
cin>>A[i]; //cin >> *(A+i);
}
4
Arrays with many indices (multiple dimensions)

int t[3][4] ;
 t is an array with 3 « elements »,
▪ each of these elements is itself an array of 4 integers

 t has not a type int*, but a type int[4]* = array of pointers


 t+1 corresponds to the address of t increased by 4 integers (2nd line in the array)
 t still corresponds to &t[0][0] = address of the 1st = address of the 1st row

5
Arrays with many indices (cont.)
t[3][4]
Adr Val
1 2 3 4
t 3000 1 &t[0][0] or &t[0]
5 6 7 8
3004 2
9 10 11 12
3008 3
3012 4
Remarks t+1 3016 5 &t[1][0] or &t[1]
• t[i][j] = *(*(t+i)+j)
3020 6
• t[1] is a constant pointer 3024 7
• not lvalue 3028 8
• t[1]++ → Error
t+2 3032 9 &t[2][0] or &t[2]
3036 10
3040 11
3044 12

6
Null Pointer

 A null pointer
▪ points on nothing
▪ has a value 0
▪ is a constant pointer

 int *ptr; ptr=0; // or ptr=NULL;


▪ NULL is a predifined constant in files cstdio and cstddef

 It is very important to insure that the pointer points somewhere known before
using it (or it is null)

7
Dynamic allocation: operators new and delete

 We distinguish between static and automatic data:


1. static data exists during the lifetime of the program and lasts until it ends
2. automatic data exists only during the lifetime of a bloc or a function

 A new category of data is allocated dynamically


 Dynamic allocation of memory is done in a specific region in memory called
heap (or free store).
 The allocation (i.e. reservation) is done using the operator new
 The deallocation (i.e. releasing) of this memory is only done with the operator
delete
 Even after leaving a function where the allocation is done, the allocation persist
if we don’t use delete

8
Internal structure of a process

• A process is a running program stack

• It is constituted with 4 parts in memory:


– code: the binary code of the program
– data: contain global variables
– stack: contains temporary data (e.g. to manage the
usage of functions: local variables, arguments,
returning point address from funtion) heap
– heap: for dynamic allocation using the new
data
operator
code

9
Syntax and role of new
x
 Syntax of new:
▪ Variable ?
▪ type *var = new type;
- int *x;
12
- x = new int; int *x = new int;
- *x = 12;
▪ Allocate the necessary memory for one element of type int, and
▪ Assign to x the corresponding address

▪ 1D Array
▪ type *var = new type[n];
- n can be a variable or a constant A
int n;
cin>>n;
? ? … ?
int * A = new int [n];
▪ Allocate the necessary memory for an array of n integers, and
▪ Put the starting address in A
10
Syntax and role of new (cont.)

▪ 2D Array n x m
type **var = new type *[n];
for(int i=0; i<n; i++)
var[i] = new type[m];
▪ Exp:
int **a=new int*[n];
for(int i=0;i<n;i++)
a[i]=new int[m];

 Remarks
▪ Parenthesis in declaration are important
- int(*ptab)[4]; //a pointer on an array of 4 int
- int *ptr[3]; // an array of pointers to integers

11
Operator delete

 Deallocate allocated memory


 Is only applied to pointers with values obtained from the operator new.
 Does not eliminate the pointer itself but the value pointed by this pointer
 Syntax: delete address

 Example
delete x ; //in case of simple variable
delete A or delete [] A; //in case of an array

12
Example illustrating the manipulation of dynamic allocation memory
#include <iostream>
using namespace std ;
int main()
{ int *adi, *adibis ; Screen:
int nb ; How many values: 7
float * adf ; allocation of 7 int in : 8861976
These are the squares of numbers 1 to 7 :
cout << "How many values : " ; 1 4 9 16 25 36 49
cin >> nb ;

adi = new int [nb] ;


cout<<"allocation of "<<nb<<" int in: " << adi << "\n" ;
for (int i=0 ; i<nb ; i++)
*(adi+i) = (i+1)*(i+1) ;

cout<<"These are the square of numbers from 1 to "<<nb<<":\n";


for (adibis = adi ; adibis < adi+nb ; adibis++)
cout << *adibis << " " ;
cout << "\n" ; 13
Example illustrating the manipulation of dynamic allocation memory
adf = new float [30] ;
cout << « Allocation of 30 float in : " << adf << "\n" ;

delete adi ;
cout << "Deallocation of "<<nb<<" int in : "<< adi << "\n";

//here it is dangerous to use the spaces pointed by adibis, and also those pointed
by adi
adi = new int [50] ;
cout << "Allocation of 50 int in: " << adi << "\n" ;

delete adf ;
cout << "Deallocation of 30 float in: " << adf <<"\n" ;

adf = new float [10] ;


cout << "Allocation of 10 float in : " << adf << "\n" ;
}
Allocation of 30 float in : 8862008
Deallocation of 7 int in : 8861976
Allocation of 50 int in : 8862132
Deallocation of 30 float in : 8862008
14
Alocation of 10 float in : 8862336
Passing arrays as argument: Array with one index
 Function definition  Example
functionType functionName (arryType t[], int n) int sum (int t[],int nb)
{ { int s = 0, i ;
… for (i=0 ; i<nb ; i++)
} s += t[i] ;
return (s) ;
}
functionType functionName (arryType *t, int n)
{
… int main()
} { int t1[30], t2[15], t3[10] ;
int s1, s2;
.....
s1 = sum(t1, 30) ;
 Function call s2 = sum(t2, 15) + sum(t3, 10) ;
.....
functionName(arrayName,size); }

15
Passing arrays as argument: Array with multiple indices
 Function definition

functionType functionName (arryType t[][nbColumns], int n, int m)


{

}

functionType functionName (arryType **t, int n, int m)


{

}

 Function call
functionName(arrayName, nbUsedRows, nbUsedColumns);

16
Passing arrays as argument: Array with multiple indices
 Example

void initialize (int t[][15], int n, int m)


{ int i, j ;
for (i=0 ; i<n ; i++)
for (j=0 ; j<m ; j++)
t[i][j] = 1 ;
}

int main()
{ int A[3][4];
.....
initialize (A, 3, 4);
.....
}

17
Array with multiple indices

 example: array with variable size


#include<iostream.h> int main(){
int n,m;
int Display(int **t, int n, int m){
cout<<« Give the value of n and m "<<endl;
for(int i=0;i<n;i++){ cin>>n>>m;
int **a=new int*[n];
for(int j=0;j<m;j++)
for(int i=0;i<n;i++)
cout<<t[i][j]<<" "; a[i]=new int[m];
cout<<endl; for(int i=0;i<n;i++)
} for(int j=0;j<m;j++)
a[i][j]=2;
}
Display(a,n,m);

for(int i=0;i<n;i++)
delete a[i];

return 0;
}
18

You might also like