array and pointer · caution! caution! caution! • it is the programmer’s responsibility to...
TRANSCRIPT
![Page 1: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/1.jpg)
CS101 Introduction to computingCS101 Introduction to computing
Array and PointerArray and Pointer
A. Sahu and S. V .RaoDept of Comp. Sc. & Engg.Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati
![Page 2: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/2.jpg)
Outline• Array Definition, Declaration, Use• Array Examples• PointerPointer
–Memory access –Access using pointer
• Basic Pointer Arithmetic• Basic Pointer Arithmetic
![Page 3: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/3.jpg)
ObjectivesObjectives• Be able to use
–arrays, pointers, and strings in C programsprograms
• Be able to explain the p–Representation of these data types at the machine level including theirthe machine level including their similarities and differences
![Page 4: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/4.jpg)
Definition – ArrayDefinition Array
• A collection of objects of the same typej ypstored contiguously in memory under one namename–May be type of any kind of variable–May even be collection of arrays!
• For ease of access to any member of arrayy y• Can be think as a group
![Page 5: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/5.jpg)
Examples• int A[10]
• An array of ten integers• An array of ten integers• A[0], A[1], …, A[9]
• double B[20]• An array of twenty long floating point y y g g pnumbers
• B[0] B[1] B[19]B[0], B[1], …, B[19]
• Array indexes always start at zero in C
![Page 6: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/6.jpg)
ExamplesExamples• int D[10][20]
• An array of ten rows, each of which is an array of twenty integers
• D[0][0], D[0][1], …, D[1][0], D[1][1], …, D[9][19]
• Not used so often as arrays of pointers
![Page 7: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/7.jpg)
Array ElementArray Element
• May be used wherever a variable of the• May be used wherever a variable of the same type may be used
I i (i l di )• In an expression (including arguments)• On left side of assignment
• Examples:–
A[3] = x + y;x = y – A[3];z = sin(A[i]) + cos(B[j]);z = sin(A[i]) + cos(B[j]);
![Page 8: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/8.jpg)
Array ElementsArray Elements
• Generic form:––ArrayName[integer‐expression]ArrayName[integer expression] [integer–ArrayName[integer‐expression] [integer‐expression]
h d l i f h– Same type as the underlying type of the array
• Definition:– Array Index – the expression between the square bracketsq
![Page 9: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/9.jpg)
Array Elements• Array elements are commonly used in loops• E.g., for(i=0; i < max; i++)
A[i] = i*i;A[i] i i;
sum = 0; for(j=0;j<max;j++) sum += B[j];
sum = 0;sum 0; for (count=0; count<30; count++){
scanf("%f", &A[count]);Sum += A[count];
}
![Page 10: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/10.jpg)
Array: Initialization and AccessingArray: Initialization and Accessing
int A[5] i; //definingint A[5], i; //defining//initializingfor (i 0; i<5; i++)for (i=0; i<5; i++)
A[i]=i; ////accessing the array for (i=0; i<5; i++)
printf("%d, ", A[i]);
![Page 11: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/11.jpg)
Memory Organization···
2n‐10 1 2 3 4 5 6 7 8 9 10 11
• All modern processors have memories organized as sequence of numbered bytesorganized as sequence of numbered bytes–Many (but not all) are linear sequences
• Definitions:––Byte: an 8‐bit memory cell capable ofByte: an 8 bit memory cell capable of storing a value in range 0 … 255
b b h h ll–Address: number by which a memory cell is identified
![Page 12: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/12.jpg)
Memory Organization (continued)···
2n‐10 1 2 3 4 5 6 7 8 9 10 11
• Larger data types are sequences of bytes–short int – 2 B, int – 4 B, long –8 B , , g–float – 4 B, double – 8 B
• (Almost) al a s aligned to m ltiple of si e in• (Almost) always aligned to multiple of size in bytes
• Address is “first” byte of sequence–May be low‐order or high‐order byteMay be low order or high order byte–Big endian or Little endian
![Page 13: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/13.jpg)
Array Representation• Homogeneous: Each element same size –s bytes
– An array of m data is a sequence ofm×s bytesAn array of m data is a sequence of m×s bytes– Indexing: 0th data at byte s×0, 1st data at byte s×1, …
• m and s are not part of representation• m and s are not part of representation– s known by compiler – usually irrelevant to programmerprogrammer
– m often known by compiler – if not, must be saved by programmerby programmer
a[1]a[2]
0 10040x1008
i t [3]a[0]a[1]
0x10000x1004 int a[3];
![Page 14: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/14.jpg)
Array Sizesint A[10];
• Size of object/data : returns the size of an object in bytesan object in bytes
• What is sizeof(A[3])? 44• What is sizeof(A)?
int A[10];
40
printf("%d, %d, %d ",sizeof(int), sizeof(A[3]), i f( ))sizeof(A));
//outputs 4, 4, 40
![Page 15: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/15.jpg)
Array: Address, Element AddressArray: Address, Element Address• Scanf require element address• Reading values from KBD and storing in array element
int A[10];int Sum = 0;int Sum 0; for (count=0;count<10;count++){
scanf("%f" &A[count]);scanf( %f , &A[count]);Sum += A[count];
}}
![Page 16: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/16.jpg)
Array Representationchar c1;int a[3];int a[3];char c2;int i;
i0x1014 Could be optimized by
a[2]0x100C
c20x1010Could be optimized by
making these adjacent, and reducing padding (by
default, not)
1a[0]a[1]
0x1004
0x1008, )
Array aligned byc10x1000 Array aligned bysize of elements
![Page 17: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/17.jpg)
Arrays in C
int A[4];All elements of same type homogenous
array size in declaration
int A[4];int b;
– homogenous
A[0] = 3;A[3] = 4;
First element (index 0)L t l t (i d i 1)A[3] = 4; Last element (index size ‐ 1)
b0x1014
A[2]A[3]b
0x1008
0x100C
0x1010
A[1][ ]
0x1000
0x1004
0x1008
A[0]
![Page 18: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/18.jpg)
Arrays in CNo bounds checking!Allowed – usually causes no int A[4]; Allowed usually causes noobvious error
A[4] may overwrite b
int A[4];int b;
A[4] may overwrite bA[0] = 3;A[3] = 4;A[3] = 4;A[4] = 5;A[ 1] 6; b
0x1014A[-1] = 6;
A[2]A[3]b
0x1008
0x100C
0x1010
A[1][ ]
0x1000
0x1004
0x1008
A[0]
![Page 19: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/19.jpg)
Multi‐Dimensional Arrays
t i [1][1]matrix[1][2]0x1014
int matrix[2][3];
matrix[0][2]0 1008
matrix[1][0]matrix[1][1]
0x100C
0x1010
matrix[1][0] = 17;
matrix[0][0]matrix[0][1]matrix[0][2]
0x1000
0x1004
0x1008
matrix[0][0]0x1000Recall: no bounds checkingWhat happens when you write: “Row Major”matrix[0][3] = 42;
jOrganization
it t t i [1][0] t i 0*3 3 i t i 1*3 0write at matrix[1][0] :matrix+0*3+3 is same as matrix+1*3+0
![Page 20: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/20.jpg)
Array Initialization• int A[5] = {2, 4, 8, 16, 32};• int B[20] = {2, 4, 8, 16, 32};int B[20] {2, 4, 8, 16, 32};
–Unspecified elements are guaranteed to be zerozero
• int C[4] = {2, 4, 8, 16, 32};
– Error — compiler detects too many initial values
![Page 21: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/21.jpg)
2D array Initialization and access2D array Initialization and access
int i, j;int i, j;int A[2][5] = {{20, 11, 22, 33, 44},
{1, 12, 23, 34, 25} };for (i=0; i<2; i++){for (j=0; j<5; j++) {
i f("%d " [i][j])printf("%d, ", A[i][j]);}
}}//prints 20, 11, 22, 33, 44, 1, 12, 23, 34, 25,, ,
![Page 22: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/22.jpg)
Implicit Array Size DeterminationImplicit Array Size Determination• int days[] = {31,28,31,30,31, 30 31 31 30 31 30 31}30, 31,31,30,31,30,31};
–Array is created with as many elements as initial values• In this case, 12 elements
–Values must be compile‐time constants (for static arrays)y )
–Values may be run‐time expressions (for automatic arrays) : will be discussed laterautomatic arrays) : will be discussed later
![Page 23: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/23.jpg)
Array Examples: Largest Element
int i, data[10], Largest;, [ ], g ;printf("Enter 10 elements: ");for(i=0; i < 10; ++i)
scanf(“%d", &data[i]);Largest=data[0];f (i 1 i<10 ++i){for(i=1; i<10; ++i){
if(Largest<data[i]) Largest=data[i];Largest data[i];
}printf(“Largest Element=%d\n“,Largest);
![Page 24: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/24.jpg)
Array Examples: Standard deviation • SD σ = sqrt( ( ∑ (μ‐xi) ) / N )
int i;float data[10], sum=0.0, mean, SD=0.0;printf("Enter 10 elements: ");printf("Enter 10 elements: ");for(i=0; i < 10; ++i)
scanf("%f", &data[i]);scanf( %f , &data[i]);for(i=0; i<10; ++i) sum += data[i];mean = sum/10;for(i=0; i<10; ++i)
SD += (data[i]–mean)*(data[i]–mean);SD t(SD/10)SD = sqrt(SD/10);printf("\nStandard Deviation=%f“,SD);
![Page 25: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/25.jpg)
Caution! Caution! Caution!• It is the programmer’s responsibility to avoid indexing off the end of an arrayg y– Likely to corrupt dataMay cause a segmentation fault–May cause a segmentation fault
–Could expose system to a security hole!
• C does NOT check array bounds– I e whether index points to an elementI.e., whether index points to an element within the arrayMight be high (beyond the end) or negative–Might be high (beyond the end) or negative (before the array starts)
![Page 26: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/26.jpg)
Segmentation fault : GDB• Finding Segmentation Fault using GDB
– gcc ‐g test.c– ./a.out // Segmentaion fault core dump!– gdb ./a.outg /– gdb > run
int i, data[10], Largest=0;int i, data[10], Largest 0; for(i=0; i<50000; ++i){
if(Largest<data[i]) Largest=data[i];//i>=10
}i tf(“L El t %d\ “ L t)printf(“Large Element=%d\n“,Largest);
![Page 27: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/27.jpg)
Pointers• Special case of bounded‐size natural numbersnumbers–Maximum memory limited by processor word‐size
–232 bytes = 4GB, 264 bytes = 16 exabytes
• A pointer is just another kind of valueA basic type in C–A basic type in C
int *ptr;p ;
The variable “ptr” stores a pointer to an “int”.
![Page 28: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/28.jpg)
Recall: Memory Organization···
2n‐10 1 2 3 4 5 6 7 8 9 10 11
• All modern processors have memories organized as sequence of numbered bytesg q y–Many (but not all) are linear sequences
• Definitions:––Byte: an 8‐bit memory cell capable ofByte: an 8 bit memory cell capable of storing a value in range 0 … 255Add b b hi h ll–Address: number by which a memory cell is identified
![Page 29: Array and Pointer · Caution! Caution! Caution! • It is the programmer’s responsibility to avoid indexing off the end of an array –Likely to corrupt data –MayMay cause a segmentation](https://reader034.vdocuments.us/reader034/viewer/2022051604/5ffd465b6b60f2785a671c85/html5/thumbnails/29.jpg)
Thanks