0% found this document useful (0 votes)
56 views23 pages

Lecture 5

1) Pointers are used to store memory addresses and allow dynamic memory allocation. They can cause issues like dangling pointers if not properly handled. 2) Arrays are implemented using pointers behind the scenes. They are passed by reference. Multidimensional arrays are mapped to linear memory using row-major or column-major ordering. 3) Dynamic memory allocation uses operators like new and delete to allocate and free memory for arrays when needed at runtime.

Uploaded by

Ayesha Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views23 pages

Lecture 5

1) Pointers are used to store memory addresses and allow dynamic memory allocation. They can cause issues like dangling pointers if not properly handled. 2) Arrays are implemented using pointers behind the scenes. They are passed by reference. Multidimensional arrays are mapped to linear memory using row-major or column-major ordering. 3) Dynamic memory allocation uses operators like new and delete to allocate and free memory for arrays when needed at runtime.

Uploaded by

Ayesha Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

CS-212

OBJECT ORIENTED
PROGRAMMING
MEHREEN TAHIR
Pointers
• Variables
– Value
– Location
• Pointers
– Auxiliary Variables
– Store Location
Pointers
Pointers
• Dynamic Memory
– Memory needs of a program can only be
determined during runtime
– Programs need to dynamically allocate memory,
for which the C++ language integrates the
operators new and delete.
Pointers
• Dangling pointer
int *p=new int; p 8
int *q=new int; q 9
*p=8;
*q=9; p 8

p=q; q NULL
delete q; // dangling pointer
q=NULL;
Pointers
• Call by reference
void func(int * py){
++(*py);
cout<<*py;
} //end func
void main(){
x=5;
cout<< x;
func(&x);
}
Pointers
ARRAYS
• ARRAYS
– A 1-D array is a finite, ordered set of
homogeneous elements
– E.g. int a[100] specifies an array of 100 integers
– Two basic operations:
• Extraction e.g. a[i]
• Storage e.g. a[i]=x;
– No. of elements in an array=upper bound – lower
bound + 1
Example: Using 1-D Arrays
void main()
{
int arr[5]={4,3,6,7,1};
int sum=0;
for (int i=0; i<5; i++) {
sum=sum+arr[i];
}
cout<<"Sum="<<sum<<endl;
}
Implementing 1-D Array
• By implementation of a data type we mean what the
hardware or system software (e.g. compiler) does to
support that data type.
• The declaration int b[100] reserves 100 successive
memory locations, each large enough to contain a
single integer.
• The address of the first location is the base address of
array b, and let us denote it as base(b)
• Suppose size of each individual element is esize
• Then b[0] is at base(b), b[1] is at base(b)+1*esize, b[2]
is at base(b)+2*esize, and element i is at base(b)
+i*esize
Example: 1-D Array Element Access
• Address of A[i]=base(A)+i*esize
• Here base(A)=100 and esize=4

100 A[0]
104 A[1]
108 A[2]
112 A[3]
116 A[4]
120 A[5]

Addresses of Individual Elements in int A[6]


Implementing 1-D Array
• An array variable is implemented as a pointer
variable.
• E.g. in int b[100], the type of b is “pointer to
int” or “int *”.
Arrays as Parameters
• Arrays are passed by reference (WHY?)
float avg(float b[], int size) /*no range is specified for the
array a */
{
float sum;
sum=0;
for (int i=0; i<size; i++)
sum+=b[i];
return (sum/size);
}
Arrays as Parameters
void main()
{
float arr[5]={4.1,3.6,6.2,7.3,1.9};
cout<<fun(arr,5)<<endl;
}
1-D Array
arr
1000

*arr *(arr+1) *(arr+2) *(arr+3) *(arr+4)


arr[0] arr[1] arr[2] arr[3] arr[4]

4.1 3.6 6.2 7.3 1.9


1000 1004 1008 1012 1016
arr (arr+1) (arr+2) (arr+3) (arr+4)
Dynamic Array
• You would like to use an array data structure but you
do not know the size of the array at compile time.
• You find out when the program executes that you
need an integer array of size n=5.
• Allocate an array using the new operator
• We must free the memory we got using the new
operator once we are done with that array.

delete[ ] arr;
Dynamic Array
int size=5;
int input=0;
int* arr=new int[size];
for(int i=0;i<size;i++)
{
cin>>input;
arr[i]=input;
}
for(int i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
delete [] arr;
arr=NULL;
2-D Arrays
• Rows as well as columns e.g. int a[3][5]
• The number of rows or columns is called
range of that dimension
• A 2-D array only a logical data structure,
because physical hardware does not have
such facility (i.e. memory is linear)
2-D Arrays
• Thinking in 2 dimensions is convenient to
programmers in many situations, e.g.
– A map
– A checkerboard
– Any set of values that are dependent on two
inputs; a departmental store that has 20 branches
each selling 30 items
int sales[20][30];
Implementing a 2-D array
• Mapping from 2-D logical space to 1-D
physical space
• Two approaches
– Row major order
– Column major order
Row-Major Order
• The first row of the array occupies the first set
of memory locations reserved for the array,
the second row occupies the second set, and
so on.
A[0][0]
A[0][1]
Row 0
A[0][2]
A[1][0]
Row 1 A[1][1]
A[1][2]

Representation of A[2][3] in Row-major Order


Example: Element Access in Row-
Major Order
• Address of A[i1][i2]=base(A)+(i1*r2+i2)*esize
• Here base(A)=100, r1=2, r2=3, esize=4

100 A[0][0]
104 A[0][1]
Row 0
108 A[0][2]
112 A[1][0]
Row 1 116 A[1][1]
120 A[1][2]

Representation of A[2][3] in Row-major Order


Passing 2D arrays
• Always mention column size while declaring 2D
array as compiler needs to know the off set to
jump to the next row.
• Or you can make constant global variable to
declare column size
• int arr[][3]
• No matter how many rows are present,
compiler knows next row comes after 3
consecutive locations

You might also like