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<int>* fubon seq (int iLength if(X==0‖ⅸ==1) if (iLength <=0 iLength >= 1024) Elems[ix=1 cer<<“ ength”<< iLength < not supported, reset to 8 else < end Length = 8 Elems[iX]=Eems[ⅸX-1]+ ElemsⅨX2] vector<int> 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<int>* fibon_seq(int iLength) { if (iLength <= 0 || iLength >= 1024) { cerr << “Length ” << iLength << “not supported, reset to 8” << endl; iLength = 8; } vector<int> 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