Presentation is loading. Please wait.

Presentation is loading. Please wait.

C/C++ Basics (IV) Array, Pointers and Dynamic Arrays Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter.

Similar presentations


Presentation on theme: "C/C++ Basics (IV) Array, Pointers and Dynamic Arrays Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter."— Presentation transcript:

1 C/C++ Basics (IV) Array, Pointers and Dynamic Arrays Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem Solving With C++,” Addison Wesley, 2003 歐亞代理

2 2 1. Arrays

3 3 Learning Objectives Introduction to Arrays –Declaring and referencing arrays –For-loops and arrays –Arrays in memory Arrays in Functions –Arrays as function arguments, return values Programming with Arrays –Partially filled arrays, searching, sorting Multidimensional Arrays

4 4 Introduction to Arrays Array definition: –A collection of data of same type First ‘aggregate’ data type –Means ‘grouping’ – int, float, double, char are simple data types Used for lists of like items –Test scores, temperatures, names, etc. –Avoids declaring multiple simple variables –Can manipulate

5 5 Declaring Arrays Declare the array  allocates memory int score[5]; –Declares array of 5 integers named ‘score’ –Similar to declaring five variables: int score[0], score[1], score[2], score[3], score[4] Individual parts called many things: –Called indexed or subscripted variables –‘Elements’ of the array –Value in brackets called index or subscript Numbered from 0 to “size-1” A list of variables with a uniform naming mechanism

6 6 Accessing Arrays Access using index/subscript cout << score[3]; Note two uses of brackets: –In declaration, specifies SIZE of array (here: “3”) –Anywhere else, specifies a subscript, tell which indexed variable is meant Size, subscript need not be literal int score[MAX_SCORES]; score[n+1] = 99; –If n is 2, identical to: score[3]

7 7 Array Usage Powerful storage mechanism Can issue command like: –“Do this to ith indexed variable” where i is computed by program –“Display all elements of array score” –“Fill elements of array score from user input” –“Find highest value in array score” –“Find lowest value in array score”

8 8 與最大者之差 Array Program Example //Reads in 5 scores and shows how much each //score differs from the highest score. #include using namespace std; int main( ) { int i, score[5], max; cout << "Enter 5 scores:\n"; cin >> score[0]; max = score[0]; for (i = 1; i < 5; i++) { cin >> score[i]; if (score[i] > max) max = score[i]; //max is the largest of the values score[0],..., score[i]. } cout << "The highest score is " << max << endl << "The scores and their\n" << "differences from the highest are:\n"; for (i = 0; i < 5; i++) cout << score[i] << " off by " << (max - score[i]) << endl; return 0; } 找出最大者

9 9 for-loops with Arrays Natural counting loop Naturally works well ‘counting thru’ elements of an array Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << “off by “ << max – score[idx] << endl; } Loop control variable (idx) counts from 0 – 5 First index is 0 當 idx 為 0 到 4 之間數值時,會執行 loop body

10 10 Major Array Pitfall Array indexes always start with zero! Zero is ‘first’ number to computer scientists C++ will ‘let’ you go beyond range –Unpredictable results –Compiler will not detect these errors! Up to programmer to ‘stay in range’ 寫程式時要注意不要使用超過 array 所定義的 memory 範圍 cout << score[5] ? cin >> score[6] ? …

11 11 Major Array Pitfall Example Indexes range from 0 to (array_size – 1) –Example: double temperature[24]; // 24 is array size // Declares array of 24 double values called temperature They are indexed as: temperature[0], temperature[1] … temperature[23] –Common mistake: temperature[24] = 5; Index 24 is ‘out of range’! No warning, possibly disastrous results

12 12 Defined Constant as Array Size Always use defined/named constant for array size –Example: const int NUMBER_OF_STUDENTS = 5; int score[NUMBER_OF_STUDENTS]; Improves readability Versatility ( 多用途 ) Improves maintainability You can not use a variable for the array size in declaration. 不可以用 variable 來代表 array 宣告時的 size 但是可以用 constant variable

13 13 Defined Constant as Array Size You cannot use a variable for the array size in declaration int number; cout << “Enter the number of sutdent:\n”; cin >>number; int score[number]; //Illegal on many compiler! The size should be determined before the program is run.

14 14 Uses of Defined Constant Use everywhere size of array is needed –In for-loop for traversal: for (idx = 0; idx < NUMBER_OF_STUDENTS; idx++) { // Manipulate array } –In calculations involving size: lastIndex = (NUMBER_OF_STUDENTS – 1); –When passing array to functions (to be explained later) If size changes  requires only ONE change in program!

15 15 Arrays in Memory Recall: simple variable declarations : –Allocate memory in an ‘address’ Array declarations –Allocate memory for entire array –Sequentially-allocated –Means addresses allocated back-to-back (one after the other) in memory –Allows indexing calculations –Simple ‘addition’ from array beginning (index 0) 一個接著一個 The computer only remember the type of the array and the address of the first array element

16 16 An Array in Memory What if a[7]=234?

17 17 Initializing Arrays As simple variables can be initialized at declaration: int price = 0;// 0 is initial value Arrays can as well: int children[3] = {2, 12, 1}; – Equivalent to following: int children[3]; children[0] = 2; children[1] = 12; children[2] = 1;

18 18 Auto-Initializing Arrays If fewer values than size supplied: –Fills from beginning –Fills ‘rest’ with zero of array base type If array-size is left out –Declares array with size required based on number of initialization values –Example: int b[] = {5, 12, 11}; –Allocates array b to size 3 #include using namespace std; void main( ) { int myTest[5]={2,3}; for (int i = 0; i < 5; i++) cout<<myTest[i]<<endl; }

19 19 Arrays in Functions As arguments to functions –Indexed variables An individual ‘element’ of an array can be function parameter ( e.g., score[0], score[1], score[2], …) –Entire arrays All array elements can be passed as ‘one entity’ As return value from function –Will be discussed later

20 20 Indexed Variables as Arguments Indexed variable handled same as simple variable of array base type –Can be a call-by-value or call-by-reference argument Given this function declaration: void myFunction(double par1); And these declarations: int i; double n, a[10]; Can make these function calls: myFunction(i);// i is converted to double myFunction(a[3]);// a[3] is double myFunction(n);// n is double call-by-value

21 21 Subtlety of Indexing Consider: myFunction(a[i]); –Value of i is determined first –It determines which indexed variable is sent myFunction(a[i*5]); –Perfectly legal, from compiler’s view –Programmer responsible for staying ‘in-bounds’ of array

22 22 Entire Arrays as Arguments Formal parameter can be entire array –Argument then passed in function call is array name –Called ‘array parameter’ or ‘array argument’ Send size of array as well –Typically done as second parameter –Simple int type formal parameter 如此在 function 中使用 array 時才知其 size

23 23 Entire Array as Argument Example

24 24 Entire Array as Argument Example Given previous example: –In some main() function definition, consider this calls: constant int numberOfScores = 5; int score[numberOfScores]; …………. fillup(score, numberOfScores); –1 st argument is entire array –2 nd argument is integer value –Note no brackets in array argument! A function call with an array argument

25 25 Array as Argument: How? What’s really passed? Think of array as 3 ‘pieces’ –Address of first indexed variable (arrName[0]) –Array base type –Size of array Only 1 st piece is passed! –Just the beginning address of array –Very similar to ‘pass-by-reference’

26 26 Array Parameters May seem strange –No brackets in array argument –Must send size separately One nice property: –Can use SAME function to fill any size array! –Exemplifies ‘re-use’ properties of functions –Example: int score[5], time[10]; fillUp(score, 5); fillUp(time, 10);

27 27 The const Parameter Modifier Recall: array parameter actually passes address of 1st element –Similar to pass-by-reference Function can then modify array! –Often desirable, sometimes not! Protect array contents from modification –Use ‘const’ modifier before array parameter Called ‘constant array parameter’ Tells compiler to ‘not allow’ modifications See pages 186-187!

28 28 Functions that Return an Array Functions cannot return arrays same way simple types are returned Requires use of a ‘ pointer ’ –Will be discussed later

29 29 Programming with Arrays Plenty of uses –Partially-filled arrays Must be declared some ‘max size’ –Sorting –Searching

30 30 Partially-filled Arrays Difficult to know exact array size needed Must declare to be largest possible size –Must then keep ‘track’ of valid data in array –Additional ‘tracking’ variable needed int numberUsed; –Tracks current number of elements in array Ensure that the program will not reference an illegal array index

31 31 Partially-filled Arrays Example See pages 195-196!

32 32 Partially-filled Arrays Example

33 33 Partially-filled Arrays Example

34 34 Global Constants vs. Parameters Constants typically made ‘global’ –Declared above main() Functions then have scope to array size constant –No need to send as parameter then? Technically yes –Why should we anyway? Function definition might be in separate file Function might be used by other programs! 最好也不要宣告 global (array) variables !

35 35 Searching an Array Very typical use of arrays –Search an array for a given value See pages 198-199!

36 36 Searching an Array Sequential Search 如果找到回傳其在 array 的 index 位置,否 則回傳 -1

37 37 Sorting an Array Sorting –Sort a list of values, such that the elements in array will be stored form lowest to highest or highest to lowest (or elements stored in alphabetical order) Selection Sort Algorithm Algorithm: for (int i=0; i<numberUsed-1; i++) Place the i-th smallest elements in a[i]; 每次 loop 會將找到的一個最小 的數值放在 array 的左邊 值互換

38 38 Sorting an Array Example Function Declarations See pages 202-203!

39 39 Sorting an Array Example a[0]≤ a[1]≤…. ≤ a[numberUsed-1] main Function: function call

40 40 Sorting an Array Example Why? Function Definition 執行 array 中兩位置之值的互換 為 call-by-reference 的呼叫 尋找下一個最小的值 在 array 中的位置 array 中兩位置之值的互換

41 41 Sorting an Array Example Function Definition 尋找下一個最小的值在 array 中的位置

42 42 Sorting an Array Example

43 43 Multidimensional Arrays Arrays with more than one index char page[30][100]; –Two indexes: An ‘array of arrays’ –Visualize as: (indexed variables) page[0][0], page[0][1], …, page[0][99] page[1][0], page[1][1], …, page[1][99] … page[29][0], page[29][1], …, page[29][99] C++ allows any number of indexes –Typically no more than two Row Column First index gives the row while the second index gives the column. One-dimensional array of size 30 whose base-type is a one dimensional array of characters of size 100

44 44 Multidimensional Arrays Since the students and quizzes are numbered starting with 1 rather than 0, we must subtract 1 from the student number and subtract 1 from the quiz number to obtain the Index variable that stores a particular quiz score.

45 45 Multidimensional Arrays 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1020 1021 1022 1023 grade[0][0] grade[0][1] grade[0][2] grade[1][0] grade[1][1] grade[3][1] grade[3][2] short grade[4][3]; 此 statement 宣告 grade 為 有四個 elements 的 array , 每個 element 是含 3 個 整數 (short) 的 array grade[0] grade[1] grade[3] A row-major style for data storage !

46 46 Multidimensional Arrays

47 47 Multidimensional Array Parameters Similar to one-dimensional array –1st dimension size not given Provided as second parameter –2nd dimension size IS given –Example: void DisplayPage(const char p[][100], int sizeDimension1) { for (int index1=0; index1<sizeDimension1; index1++) { for (int index2=0; index2 < 100; index2++) cout << p[index1][index2]; cout << endl; } }

48 48 Example: Two-Dimensional Array See pages 207-209! 宣告三個 array 沒有 show 出 輸入資料的程式碼

49 49 Example: Two-Dimensional Array

50 50 Example: Two-Dimensional Array

51 51 Summary 1 Array is collection of ‘same type’ data Indexed variables of array used just like any other simple variables for-loop ‘natural’ way to traverse arrays Programmer responsible for staying ‘in bounds’ of array Array parameter is ‘new’ kind –Similar to call-by-reference

52 52 Summary 2 Array elements stored sequentially –‘Contiguous’ portion of memory –Only address of 1 st element is passed to functions Partially-filled arrays  more tracking Constant array parameters – Prevent modification of array contents Multidimensional arrays –Create ‘array of arrays’

53 53 2. Pointers and Dynamic Arrays

54 54 Learning Objectives Pointers –Pointer variables –Memory management Dynamic Arrays –Creating and using –Pointer arithmetic

55 55 Pointer Introduction Pointer definition: –Memory address of a variable Recall: memory divided –Numbered memory locations –Sometimes, addresses used as name for variable You’ve used pointers already! –Call-by-reference parameters In function call, the address of actual argument was passed

56 56 Pointer Variables Pointers are ‘typed’ –Can store pointer in variable –Not int, double, etc. Instead: A POINTER to int, double, etc.! Example: double *p; // p is “point type” –p is declared a ‘pointer to double’ variable Variable p may contain a pointer to a variable of type double –p can hold pointers to variables of type double Not other types!

57 57 Declaring Pointer Variables Pointers declared like other types –Add ‘*’ (asterisk) before variable name –Produces ‘pointer to’ that type ‘*’ must be before each variable int *p1, *p2, v1, v2; –p1, p2 hold pointers to variables of type int –v1, v2 are ordinary variables of type int

58 58 Addresses and Numbers Pointer is an address Address is an integer Pointer is NOT an integer! –Not crazy  abstraction! C++ forces pointers be used as addresses –Cannot be used as numbers –Even though it ‘is a’ number int *p1, *p2, *p3; ….. p3=p1+p2; //incorrect! p3=p1+100; * p3= * p1+ * p2;

59 59 Pointing Terminology, view –Talk of ‘pointing’, not ‘addresses’ –Pointer variable ‘points to’ ordinary variable –Leave ‘address’ talk out Makes visualization clearer –‘See’ memory references Arrows saying: “point to a variable!”

60 60 Pointing to … int *p1, *p2, v1, v2; p1 = &v1; –Sets pointer variable p1 to ‘point to’ int variable v1 –*p1 means the variable (here is v1) that the pointer variable p1 points to –Operator, & Determines ‘address of’ variable –Read like: “p1 equals address of v1” Or “p1 points to v1”

61 61 Pointing to … Recall: int *p1, *p2, v1, v2; p1 = &v1; Two ways to refer to v1 now: –Variable v1 itself: cout << v1; –Via pointer p1: cout << *p1; Dereferencing operator, * –The pointer variable is ‘dereferenced ’ ( 解參考 ) –Means: “Get data that p1 points to” cout << *p1; p1 為指標型 態變數 此位置所儲 存的值 兩者差異為何 變數宣告時

62 62 ‘Pointing to’ Example Consider: v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl; –Produces output: 42 42 –p1 and v1 refer to same variable & is used to obtain the address of a variable

63 63 & Operator The ‘address of’ operator –It was also used to specify call-by-reference parameter previously No coincidence (不是巧合) ! Recall: call-by-reference parameters pass ‘address of’ the actual argument Operator’s two uses are closely related

64 64 Pointer Assignments Pointer variables can be ‘assigned’: int *p1, *p2; p2 = p1; –Assigns one pointer to another –“Make p2 point to where p1 points” Do not confuse with *p1 = *p2; –Assigns ‘value pointed to’ by p1, to ‘value pointed to’ by p2 p2 與 p1 指到同一個記憶體位置 p2 與 p1 可能各自指到不同記憶體 位置,但 p2 所指的記憶體位置內的 值與 p1 所指的記憶體位置內值相同

65 65 Pointer Assignments Graphic

66 66 Pointer Assignments Graphic 1100 1101 3001 3002 3003 3004. Memory Address Variable name Memory Content 1100 95 score ptrS short score; 產生 score 的整數型態變數, 在變數中儲存值 95 short *ptrS; 產生 ptrS 的指標變數, 在變數中儲存 score 的位置

67 67 The new Operator Since pointers can refer to variables… –No ‘real’ need to have a standard identifier Can dynamically allocate variables –Operator new creates variables No identifiers to refer to them Just a pointer! p1 = new int; Creates new ‘nameless’ variable, and assigns p1 to ‘point to’ it (the memory address of it) Can access with *p1 –Use just like an ordinary variable The variable can have no identifier to name it p1

68 68 The new Operator int *p1; p1 = new int; cin>>*p1; *p1=*p1+7; cout<<*p1; Variables created using the new operator are called dynamically allocated variables or simply dynamic variable, because they can be created and destroyed while program running.

69 69 Basic Pointer Manipulations Example a b c d e f g

70 70 Basic Pointer Manipulations Example

71 71 More on new Operator Creates new dynamic variable Returns pointer to the new variable Initialize dynamic variables of basic types int *n; n = new int(17);//Initializes *n to 17

72 72 Pointers and Functions Pointers are full-fledged ( 發展成熟 ) types –Can be used just like other types Can be function parameters Can be returned from functions Example: int* findOtherPointer(int* p); This function declaration: –Has ‘pointer to an integer parameter –Returns ‘pointer to an integer variable

73 73 Memory Management Heap –Also called ‘freestore’ –Reserved for dynamically-allocated variables –All new dynamic variables consume memory in freestore If too many  could use all freestore memory Future ‘new’ operations will fail if freestore is ‘full’

74 74 Checking new Success Older compilers: –Test if NULL returned by call to new int *p; p = new int; if (p == NULL) { cout << “Error: Insufficient memory.\n”; exit(1); } –If new succeeded, program continues NULL is an identifier and is actually the number 0. #include or

75 75 Checking new Success Newer compilers: –If new operation fails Program terminates automatically Produces error message Still good practice to use NULL check

76 76 Freestore Size Varies with implementations Typically large –Most programs won’t use all memory Memory management –Still good practice –Solid software engineering principle –Memory IS finite Regardless of how much there is!

77 77 delete Operator De-allocate dynamic memory –When no longer needed –Returns memory to freestore –Example: int *p; p = new int(5); … //Some processing… delete p; –De-allocates dynamic memory “pointed to by pointer p” Literally ‘destroys’ memory The delete operator eliminates a dynamic variable and returns the memory location occupied by it to the system

78 78 Dangling Pointers delete p; –Destroys dynamic memory –But p still points there! Called ‘dangling pointer’ (the value of the point variable is undefined) –If p is then dereferenced ( *p ) Unpredicatable results! Often disastrous ( 導致很大災害的 ) ! –Avoid dangling pointers Assign pointer to NULL after delete: delete p; p = NULL; This phenomena can be observed for dynamic variables pointing to the same memory location as dynamic variable p. For safety, we can set any dangling point variables equal to NULL

79 79 Dynamic and Automatic Variables Dynamic variables –Created with new operator –Created and destroyed while program runs Local variables –Declared within function definition –Not dynamic Created when function is called Destroyed when function call completes –Often called ‘automatic’ variables The dynamic properties automatically controlled for you Global variables are variables declared outside any function or classes, including main.

80 80 Define Pointer Types Can ‘name’ pointer types To be able to declare pointers like other variables –Eliminate need for ‘*’ in pointer declaration typedef int* IntPtr; –Defines a ‘new type’ alias –Consider these declarations: IntPtr p; int *p; –The two are equivalent

81 81 Pitfall: Call-by-value Pointers Behavior subtle and troublesome If function changes pointer parameter itself  only change is to local copy Best illustrated with example…

82 82 Call-by-value Pointers Example 77 → 99 p temp

83 83 Call-by-value Pointers Example

84 84 Dynamic Arrays Array variables –Really pointer variables! Standard array –Fixed size Dynamic array –Size not specified at programming time –Determined while program running Constant pointer variables

85 85 Array Variables Recall: arrays stored in memory addresses, sequentially –Array variable ‘refers to’ first indexed variable –So array variable is a kind of pointer variable! Example: int a[10]; int * p; a and p are both pointer variables!

86 86 Array Variables  Pointers Recall previous example: int a[10]; typedef int* IntPtr; IntPtr p; a and p are pointer variables –Can perform assignments: p = a;// Legal p now points where a points To first indexed variable of array a a = p;// ILLEGAL! –Array pointer is CONSTANT version of int* pointer! You cannot change the pointer value in an array variable. // 定義一個整數指標型態 // 宣告一個大小為 10 的整數陣列變數 // 宣告一個整數指標變數

87 87 Array Variables  Pointers Array variable int a[10]; MORE than a pointer variable –‘const int*’ type –Array was allocated in memory already –Variable a MUST point there…always! –Cannot be changed! In contrast to ordinary pointers –Which can ( and typically do) change a a[0] a[1] a[9] *a *(a+1) *(a+9)

88 88 Array Variables  Pointers Example a a[0] a[1] a[4] *a *(a+1) *(a+4) a[3] a[2] *(a+3) *(a+2) 1 2 3 5 4 a 是陣列變數,雖也是一種 指標變數 ( 內存有陣列起 始位置 ) ,但它所指到的記憶 體位址在程式執行時 是不可以被改變的! 另外一種使用陣列的方式

89 89 Dynamic Arrays Array limitations –Must specify size first –May not know until program runs! –Must ‘estimate’ maximum size needed Sometimes OK, sometimes not ‘Wastes’ memory Dynamic arrays –Can grow and shrink ( 縮小 ) as needed ( 靜態 ) 陣列使用時的限制

90 90 Creating/Deleting Dynamic Arrays Very simple! Use new operator –Dynamically allocate with pointer variable –Treat like standard arrays Example: typedef double* DoublePtr; DoublePtr d; d = new double[10]; //Size in brackets Creates dynamically allocated array variable d, with ten elements, base type double The pointer type for an array of elements of the type double is the same as the pointer type you would used for a simple variable of type double. 動態地宣告一個有十個 double 數值的陣列 並利用指標變數 d 指到這個陣列

91 91 Creating/Deleting Dynamic Arrays Allocated dynamically at run-time So should be destroyed at run-time Simple again. Recall Example: d = new double[10]; … //Processing delete [] d; De-allocates all memory for dynamic array Brackets indicate ‘array’ is there Recall: d still points there! Should set d = NULL; If you omit bracket then use : delete d; you will not eliminate the entire array. a d

92 92 Creating/Deleting Dynamic Arrays 動態宣告一個陣列大小為擁有十個整數資料的空間

93 93 Creating/Deleting Dynamic Arrays

94 94 Creating/Deleting Dynamic Arrays When the program is finished using a dynamically allocated array, you should return the array memory to the freestore manager with a call to delete 如果程式執行過程中有動態宣告陣列的話, 當程式不再需要使用此陣列時,記得清除所 宣告的陣列,把記憶體空間還給系統 !

95 95 Function that Returns an Array Array type NOT allowed as return-type of function Example: int [] someFunction(); // ILLEGAL! Instead return pointer to array base type: int* someFunction(); // LEGAL!

96 96 Function that Returns an Array Sample dialogue Array a: 1, 2, 3, 4, 5 Array b: 2, 4, 6, 8, 10

97 97 Pointer Arithmetic Can perform arithmetic on pointers –‘Address’ arithmetic Example: typedef double* DoublePtr; DoublePtr d; d = new double[10]; –d contains address of d[0] –d + 1 evaluates to address of d[1] –d + 2 evaluates to address of d[2] –Equates to ‘address’ at these locations

98 98 Alternative Array Manipulation Use pointer arithmetic! ‘Step thru’ array without indexing: for (int i = 0; i < arraySize; i++) cout << *(d + i) << “ “ ; Equivalent to: for (int i = 0; i < arraySize; i++) cout << d[i] << “ “ ; Only addition/subtraction on pointers –No multiplication, division Can use ++ and -- on pointers d++; or d--; e.g., cout << *(d /i) << “ “ ;

99 99 Multidimensional Dynamic Arrays Yes we can! Recall: ‘arrays of arrays’ Type definitions help ‘see it’: typedef int* IntArrayPtr; IntArrayPtr *m = new IntArrayPtr[4]; Creates array of four pointers Make each allocate array of 4 ints for (int i = 0; i < 4; i++) m[i] = new int[4]; Results in four-by-four dynamic array! The type for a one-dimensional array of ints.

100 100 Multidimensional Dynamic Arrays The dynamic array created on the previous slide could be visualized like this m IntArrayPtr's int's IntArrayPtr * 這四個陣列並不一定 接續地存在記憶體中, 而可能各自位在記憶 體的某一個連續區間

101 101 Multidimensional Dynamic Arrays See P432

102 102 Multidimensional Dynamic Arrays

103 103 Summary 1 Pointer is memory address –Provides indirect reference to variable Dynamic variables –Created and destroyed while program runs Freestore –Memory storage for dynamic variables Dynamically allocated arrays –Size determined as program runs


Download ppt "C/C++ Basics (IV) Array, Pointers and Dynamic Arrays Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter."

Similar presentations


Ads by Google