1 Computer Programming and Basic Software Engineering 6
1 Computer Programming and Basic Software Engineering 6. Pointers and Arrays 6. 2 Arrays and Strings
2 Computer Programming and Basic Software Engineering 6. Pointers and Arrays What Is an Array? In consecutive memory • An array consists of a collection of data storage locations, each holds the same type of data • An array can be easily declared as follows: short. Array[25]; Variables short Memory short. Array[25]; //declaration 10 0 A 21 3 A short. Array[0] It states that there is a sequence of 25 short integer data. The whole sequence is named as short. Array . . . 20 2 A 4 B 40 000 B 000 C. . . 0037 0038 0039 003 A short. Array[1] short. Array[24]
3 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Array Element • In an array, an array element is referred to by indicating its index • The first element has index 0, and then 1, … • Hence short. Array[0] is the first element short. Array[1] is the second element 25 short integers : short. Array[24] is the last element • No short. Array[25] !!! • Do not try to use short. Array[25], result unpredictable.
4 Computer Programming and Basic Software Engineering 6. Pointers and Arrays short. Array[25]; Variables short Memory Declaration short. Array[25]; 10 0 A 21 3 A . . . 20 2 A 4 B 40 Address 0009 000 A 000 B 000 C. . . 0037 0038 0039 003 A (Hex) • So short. Array[0] = 0 x 100 A; // 4106 in deciaml short. Array[1] = 0 x 213 A; // 8506 in decimal : short. Array[23] = 0 x 202 A; short. Array[24] = 0 x 4 B 40; Assignment statements
5 Computer Programming and Basic Software Engineering 6. Pointers and Arrays No my. Array[5] !!! Do not try to use my. Array[5], result unpredictable #include <iostream> using namespace std; int main() { int my. Array[5], i; for (i=0; i<=5; i++) The kind of mistake my. Array[i] = 20; people often make for (i=0; i<5; i++) cout << my. Array[i] << endl; return 0; }
6 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Initializing Arrays • An array can be initialized during declaration int Integer. Array[5] = {10, 20, 30, 40, 50}; int Another. Array[] = {50, 40, 30, 20, 10}; int Bigger. Array[5] = {10, 20}; Integer. Array declares itself to have 5 integers Another. Array requests the memory space in stack just enough to hold the data defined in the list Bigger. Array declares itself to have 5 integers but only the first 2 of them are int a[5]; NOT the same initialized. The others are 0. as int a[5]={}; It is different from : int Bigger. Array[] = {10, 20}; int Incorrect. Array[2] = {10, 20, 30};
7 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Array of Objects • Any object can be stored in an array • Accessing member data in an array of objects is a two-step process • Identify the member of array by [] • Access the member by. CAT Litter[5]; //Litter[0] - Litter[4] are 5 objects int i; for (i=0; i<5; i++) cout << Litter[i]. Get. Age() << endl; To find out which CAT Call Get. Age() of that CAT
8 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Multidimensional Array • It is possible to have an array of more than 1 dimension 0 1 2 3 4 5 6 7 0 0 1 2 3 4 5 6 7 Two-dimensional array A Chess Board • A 2 -dimensional array can be declared as int Board[8][8]; 64 integers • Each element can be written or read as Board[5][3] = 0; int number = Board[5][3]; // number = 0
9 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Initialize Multidimensional Arrays • Multidimensional Arrays can also be initialized during declaration int Some. Array[5][2] = { {0, 0}, {1, 2}, {4, 6}, {7, 2}, {4, 4}}; int Another. Array[5][2] = {0, 0, 1, 2, 4, 6, 7, 2, 4, 4}; 4 3 2 1 0 4 7 4 1 0 0 4 2 6 2 0 1
10 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Array and Pointer • C++ allows the flexibility for user to use Array and Pointerchangeably • The name of an array is a constant pointer pointing to the first element of the array int a = b = a, b; Some. Array[5] = {10, 20, 30, 40, 50}; *Some. Array; // a = 10 *(Some. Array+1); // b = 20, pointer arithmetic • Compiler does all the calculation • Some. Array+1 does not add 1 to Some. Array but add 4 (1 integer needs 4 bytes for storage) and point to the next element in the array
11 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Some. Array[5] The Stack Address 10 20 30 40 50 0104 0108 010 c 0110 0114 0118 011 c 0120 0124 • There is NOT a memory location to store the pointer Some. Array. Everything is done by an internal conversion x = *Some. Array; will be internally converted to x = Some. Array[0]; // x = 10 px = Some. Array; will be internally converted to px = &(Some. Array[0]); // px = 0104
12 Computer Programming and Basic Software Engineering 6. Pointers and Arrays • A variable declared as a pointer can also be used as an array int Some. Array[5] = {10, 11, 12, 13, 14}; int *p. Some. Pointer = Some. Array; // It is a pointer but will // later be used as array cout << p. Some. Pointer[0] << endl; // number 10 will be shown cout << p. Some. Pointer[1] << endl; // number 11 will be shown cout << p. Some. Pointer[2] << endl; // number 12 will be shown cout << p. Some. Pointer[3] << endl; // number 13 will be shown cout << p. Some. Pointer[4] << endl; // number 14 will be shown Some. Array[5] The stack Address 10 11 12 13 14 0100 0104 0108 010 c 0110 0114 0118 011 c 0120 0124 Some. Array[1] and p. Some. Pointer[1] are p. Some. Pointer = 0104 exactly the same.
13 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Array of Pointers • So far the arrays are declared to store data in stack • Usually the memory space of stack is very small • If a large array is required, it is better to store the elements of arrays in Free Store • In this case, for every element in an array, a pointer is assigned to indicate its location in Free Store • This becomes an array of pointers. The array itself is still in the stack.
14 Computer Programming and Basic Software Engineering Pointers and Arrays #include 6. <iostream> using namespace std; class CAT {public: CAT() {its. Age = 1; } ~CAT() {} Creating 500 CATs int Get. Age() const {return its. Age; } may use up all void Set. Age(int age) {its. Age = age; } memory in the stack private: int its. Age; Hence only an array of 500 pointers }; of CAT are created in the stack int main() { CAT *Family[500]; Each CAT object is located in the int i; Free Store by the keyword new for (i=0; i<500; i++) { Family[i] = new CAT; } To access member of a particular Family[255]->Set. Age(1); CAT, use the corresponding pointer for (i=0; i<500; i++) { delete Family[i]; } in the array return 0; }
15 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Array of Pointers CAT 2 (*Family[0]) CAT 0 CAT 1 Free Store or the heap An array of pointers called Family is kept to point to different elements in Free Store CAT 499 . . . The Stack Code Space Global Name Space
16 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Pointer of Array • If one feels that 500 pointers in the previous example are still too many, we can put the whole array into Free Store • Hence one pointer is enough to point to the Array itself but not an individual element • This becomes a pointer of array. Family is the pointer of array CAT *Family = new CAT[500]; and points to Family[0] in Free Store int a = 0, b=0; (Family+255)->Set. Age(10); a = (Family+255)->Get. Age(); // a = 10 b = Family[255]. Get. Age(); // b = 10 Individual element of the array of the CAT objects can be accessed by pointer arithmetic
17 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Pointer of Array Family[0] Family[499] CAT 0 CAT 1 A pointer Family is kept to point to the beginning memory location of an array of CAT objects in Free Store . . . CAT 499 Free Store or the heap The Stack Code Space Global Name Space
18 Computer Programming and Basic Software Engineering and Arrays #include 6. Pointers <iostream> using namespace std; class CAT {public: Arrays created in Free CAT() {its. Age = 1; } Store can also be deleted ~CAT(){; } int Get. Age() const {return its. Age; } void Set. Age(int age) {its. Age = age; } private: int its. Age; }; int main() { CAT *Family = new CAT[10]; for (int i=0; i<10; i++) { Family[i]. Set. Age(2*i+1); cout <<' '<< Family[i]. Get. Age(); } The [] symbol after delete [] Family; lets the system know the whole return 0; array is to be deleted }
19 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Exercise 6. 2 • Based on the program in the last page, add a destructor to CAT such that when it is called, the age of the cat will be shown on the screen. • How many times the destructor will be called when the “delete [] Family; ” statement is executed? Why? • What will happen if we use the statement “delete Family” instead? Can it be executed in Visual C++?
20 Computer Programming and Basic Software Engineering 6. Pointers and Arrays Exercise 6. 2 b • Modify the program in Ex. 6. 2 such that the “Array of Pointers” approach is used to define the 10 objects of CAT in the heap. Make sure your program will not introduce memory leaks.
21 Computer Programming and Basic Software Engineering 6. Pointers and Arrays The null character String - char Arrays represents the end of string; it • A string is an array of characters • A string can be simply initialized as follows: must be added. char Greeting[] = Size of {'H', 'e', 'l', 'o', 'W', 'o', 'r', 'l', 'd', '