The c ++ Programming Language Lecture 2 Procedure-Based Programming
The C++ Programming Language Lecture 2: Procedure-Based Programming
Procedure-Based Programming u Procedure/ Function based, why Program more readable a Code reusing a Task assignment and teamwork Prior to the oo thoughts, and still on the stage Efficient and effective in simple designs
Procedure-Based Programming ◼ Procedure/Function based, why ◼ Program more readable ◼ Code reusing ◼ Task assignment and teamwork ◼ Prior to the OO thoughts, and still on the stage ◼ Efficient and effective in simple designs
Important Concepts for Safe Programming
Important Concepts for Safe Programming
A simple starting point J Fibonacci number sequence: 11,23, 5, 8, 13, 21 a We build a function to get certain element of the sequence int fubon elem(int iPos) tn2=1,n1=1 int eLem = 1; for(intⅸ=3;ⅸ<=iPos;i++) Elem =n2+n1 n2=n1; n1= eLem return elem
A simple starting point ◼ Fibonacci number sequence: 1, 1, 2, 3, 5, 8, 13, 21, … ◼ We build a function to get certain element of the sequence int fibon_elem(int iPos) { int n2 = 1, n1 = 1; int iElem = 1; for (int iX = 3; iX <= iPos; iX++) { iElem = n2 + n1; n2 = n1; n1 = iElem; } return iElem; }
Could we always trust others' a better version J Users may not be trustworthy, assumption is dangerous Be doubtful bool fubon elem (int iPoS, int &eLem) int n2=1, n1=1 if (iPos = 1024) iElem =0: return false iElem= 1: for(intⅸ=3;ⅸ<=iPos;i++) iElem n2+n1 n1= eLem return truer
Could we always trust others? – A better version ◼ Users may not be trustworthy, assumption is dangerous ◼ Be doubtful bool fibon_elem(int iPos, int &iElem) { int n2 = 1, n1 = 1; if (iPos = 1024) { iElem = 0; return false; } iElem = 1; for (int iX = 3; iX <= iPos; iX++) { iElem = n2 + n1; n2 = n1; n1 = iElem; } return true; }
How about this u Calculating elements in every calling is not so efficient u We need only calculate them once, then pick the corresponding one vector* fubon seq (int iLength if(X==0‖ⅸ==1) if (iLength = 1024) Elems[ix=1 cer Elems( iLength ) for(intⅸ=0;ⅸ< iLength;ⅸ++) return &Elems; Problem
How about this? ◼ Calculating elements in every calling is not so efficient ◼ We need only calculate them once, then pick the corresponding one vector* fibon_seq(int iLength) { if (iLength = 1024) { cerr Elems( iLength ); for (int iX = 0; iX < iLength; iX++) { if (iX == 0 || iX == 1) { Elems[iX] = 1; } else { Elems[iX] = Elems[iX-1] + Elems[iX-2]; } } return &Elems; } Problem
Extent u Elems has only Local Extent Its memory was allocated on the program stack Discarded after the function completed Can t be seen from outside of the function Pointer to a died object is dangerous Result of dereferencing adied object is not defined Declared it outside of the function will have file Extent Its scope is from the declaration point to the end of the file Its memory was allocated before entering the main function, and never discarded until the program ends Internal(Built-in type was initialized as0 automatically
Extent ◼ Elems has only “Local Extent” ◼ Its memory was allocated on the program stack ◼ Discarded after the function completed ◼ Can’t be seen from outside of the function ◼ Pointer to a ‘died’ object is dangerous ◼ Result of dereferencing a ‘died’ object is not defined ◼ Declared it outside of the function will have “File Extent” ◼ Its scope is from the declaration point to the end of the file ◼ Its memory was allocated before entering the main function, and never discarded until the program ends ◼ Internal(Built-in) type was initialized as 0 automatically
Extent(cont) u Also We could leverage the Dynamic Extent Use the new and delete operators in any functions a Its memory was allocated from the free store Manually memory management by the programmer Example int* funcAO void funcB(int* p) nt*p=new int[ 1024] cout <<pl l<<endl return p deletep
Extent (cont.) ◼ Also we could leverage the “Dynamic Extent” ◼ Use the new and delete operators in any functions ◼ Its memory was allocated from the free store ◼ Manually memory management by the programmer ◼ Example int* funcA() void funcB(int* p) { { int* p = new int[1024]; cout << p[ 1 ] << endl; return p; delete [] p; } }
Mem Management of Extent objects u For those objects having local and file extent Automatic Memory Management The system allocate and de-allocate the memory automatically For those objects having dynamic extent Dynamic Memory Management Adaptive and convenient System will never free them automatically while the program running Explicit delete is necessary, or there will be "Memory Leak
Mem Management of Extent objects ◼ For those objects having local and file extent – Automatic Memory Management ◼ The system allocate and de-allocate the memory automatically ◼ For those objects having dynamic extent – Dynamic Memory Management ◼ Adaptive and convenient ◼ System will never free them automatically while the program running ◼ Explicit delete is necessary, or there will be “Memory Leak
Memory Areas in C++ 5 areas store variables and objects Const data area For those literal const data, only internal types Global or static data area u Once the program starts, the memory allocated for global or static objects, but may not be initialized at once(eg static objects in function Support user-defined data types Stack a For local objects a Objects destroyed at the end of their scope ■ Free store For the objects constructed and destroyed using new delete and their family Heap u For the objects constructed and destroyed using malloc free and their family
Memory Areas in C++ ◼ 5 areas store variables and objects ◼ Const data area ◼ For those literal const data, only internal types ◼ Global or static data area ◼ Once the program starts, the memory allocated for global or static objects, but may not be initialized at once (eg. static objects in function) ◼ Support user-defined data types ◼ Stack ◼ For local objects ◼ Objects destroyed at the end of their scope ◼ Free store ◼ For the objects constructed and destroyed using new & delete and their family ◼ Heap ◼ For the objects constructed and destroyed using malloc & free and their family