《The C++ Programming Language》课程教学资源(PPT课件讲稿)Lecture 02 Procedure-Based Programming

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 东南大学:《数据结构》课程教学资源(PPT课件讲稿)第七章 图.ppt
- 北京大学:《高级软件工程》课程教学资源(PPT课件讲稿)第一讲 软件与软件开发.ppt
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第二章 流密码(主讲:董庆宽).pptx
- 《Photoshop基础教程与上机指导》教学资源(PPT讲稿)第18章 扫描和修饰图像.ppt
- 中国水利水电出版社:《单片机原理及应用》课程PPT教学课件(C语言版)第8章 单片机系统扩展(主编:周国运).ppt
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 04 Memory Management.ppt
- 《网页设计》课程教学资源:课程教学大纲.doc
- 《人工智能技术导论》课程教学资源(PPT课件讲稿)第3章 图搜索与问题求解.ppt
- 清华大学:TCP and Congestion Control(1).pptx
- 清华大学:域内路由选择(PPT课件讲稿)Intra-domain routing.pptx
- 山东大学:IPv6试商用的进展和挑战(PPT讲稿,网络与信息中心:秦丰林).pptx
- 克里特大学:The Application of Artificial Neural Networks in Engineering and Finance.ppt
- 关键词抽取、社会标签推荐及其在社会计算中的应用.pptx
- 《数据库系统原理》课程PPT教学课件(SQLServer)第12章 并发控制.ppt
- 《计算机组成原理》课程教学资源(PPT课件讲稿)第2章 运算方法和运算器.ppt
- 《数据科学》课程教学资源(PPT课件讲稿)第2章 数据预处理.ppt
- 西安理工大学:面向主题的服务(PPT讲稿)综合集成支撑平台业务化——互联网信息化(平台、内容、服务).ppt
- 中国科学技术大学:《数据结构》课程教学资源(PPT课件讲稿)第三章 线性表.pps
- 《计算机网络》课程PPT教学课件(Windows)第09讲 DNS服务.ppt
- 《软件工程》课程教学资源(PPT课件讲稿)第12章 软件开发工具StarUML及其应用.ppt
- 《数据库原理与应用》课程PPT教学课件(SQL Server)第9章 存储过程和触发器.ppt
- 合肥学院:《数据库原理与应用》课程教学资源(PPT课件)第1章 数据库系统概述(主讲:叶潮流).ppt
- 北京大学软件研究所:高级软件工程(PPT讲稿)云计算与平台即服务.ppt
- 香港科技大学:深度学习导论(PPT讲稿)Introduction to Deep Learning.pptx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)量子计算 Quantum computing.pptx
- 《数字图像处理》课程PPT教学课件(讲稿)第二章 图像获取、显示和表示.ppt
- 《Web编程实用技术教程》课程教学资源(PPT课件讲稿)第5章 MFC WinSock类的编程.ppt
- 电子工业出版社:《计算机网络》课程教学资源(第五版,PPT课件讲稿)第五章 运输层.ppt
- 《神经网络 Neural Networks》课程教学资源(PPT课件讲稿)Ch 8 Artificial Neural networks.pptx
- PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING(PPT讲稿)C Programming Review.ppt
- 计算机网络技术基础(PPT课件讲稿).ppt
- 《网络搜索和挖掘关键技术 Web Search and Mining》课程教学资源(PPT讲稿)Lecture 13 Matrix Factorization and Latent Semantic Indexing.ppt
- 多媒体技术及应用(PPT讲稿)多媒体音频技术.ppt
- 山东大学:《微机原理及单片机接口技术》课程教学资源(PPT课件讲稿)第四章 指令系统及汇编语言程序设计(4.1-4.4).ppt
- 东南大学:《C++语言程序设计》课程教学资源(PPT课件讲稿)Chapter 13 Object-Oriented Programming - Polymorphism.ppt
- 《C++语言程序设计》课程教学资源(PPT课件)第14讲 运算符重载.ppt
- 淮阴工学院:《数据库原理》课程教学资源(PPT课件讲稿)第4章 结构化查询语言SQL.ppt
- 《计算机网络 COMPUTER NETWORKS》课程教学资源(PPT课件讲稿)Chapter 18 互联网协议 Internet Protocols(IP).ppt
- 计算机应用专业《计算机网络》教学大纲.doc
- 《计算机网络安全》课程教学资源(PPT课件讲稿)第四章 数据加密技术.ppt