DynamicMemoryAllocationppt动态内存allocationppt.ppt_第1页
DynamicMemoryAllocationppt动态内存allocationppt.ppt_第2页
DynamicMemoryAllocationppt动态内存allocationppt.ppt_第3页
DynamicMemoryAllocationppt动态内存allocationppt.ppt_第4页
DynamicMemoryAllocationppt动态内存allocationppt.ppt_第5页
已阅读5页,还剩16页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Dynamic Memory Allocation,tMyn,1,Dynamic Memory Allocation,All of the code we have written up to now allocates space for data at compile time. We specify the variables and the array sizes that we need in the source code, and thats what will be allocated when the program executes, whether we need it

2、or not. Working with a fixed set of variables in a program can be very restrictive, and it is often wasteful.,Dynamic Memory Allocation,tMyn,2,The solution is to use dynamic memory allocation, which means that you allocate the memory you need to store the data you are working with when your program

3、executes (runtime), rather than when you compile it (compile time). Dynamically allocated variables cant be declared at compile time, and so they cant be named in your source program. When you allocate memory dynamically, the space that is made available by your request is identified by its address.

4、 The obvious place to store this address is in a pointer.,Dynamic Memory Allocation,tMyn,3,Memory Allocated Statically,Memory Allocated Dynamically,Fixed Data Area,Inflexible Wasteful Fixed capacity,Program Code,Program Code,Fixed Data Area,Data area allocated dynamically at runtime,Flexible Efficie

5、nt Variable capacity,Dynamic Memory Allocation,tMyn,4,In most instances, there is unused memory in your computer when your program is executed. In C+, this unused memory is called the free store, or sometimes the heap. You can allocate space within the free store for a new variable of a given type b

6、y using a special C+ operator that returns the address of the space allocated. This operator is new, and it is complemented by the operator delete, which de-allocates memory that you have previously allocated with new.,Dynamic Memory Allocation,tMyn,5,You can allocate space in the free store for som

7、e variables in one part of a program, and then release the allocated space and return it to the free store once you have finished with the variables concerned. The memory then becomes available for reuse by other dynamically allocated variables later in the same program. When you allocate space for

8、a variable using new, you are creating the variable in the free store. The variable continues to exist until the memory it occupies is released by the operator delete.,Dynamic Memory Allocation,tMyn,6,Suppose that we need space for a variable of type double. We can define a pointer to type double, a

9、nd then request that the memory is allocated at execution time: double* pValue=0; pValue=new double; All pointers should be initialized! The new operator in the second line of code above will return the address of the memory in the free store allocated to a double variable, and this address will be

10、stored in the pointer pValue.,Dynamic Memory Allocation,tMyn,7,It is possible to initialize a variable created with new at the time of creation: pValue=new double(999.0); When the program no longer needs a dynamically allocated variable, you can free up the memory that it occupied in the free store

11、with the delete operator. This ensures that the memory can be used subsequently by another variable. If you dont use delete, and you subsequently store a different address in the pointer pValue, it will be impossible to free up the original memory location or to use the variable that it contains, si

12、nce access to the address will have been lost.,Dynamic Memory Allocation,tMyn,8,Its important to realize that the delete operator frees the memory but does not change the pointer. Following the above statement, pValue still contains the address of memory that was allocated, but the memory is now fre

13、e and may immediately be allocated to something else possibly by another program. To avoid that risk: delete pValue; pvalue=0;,Dynamic Memory Allocation,tMyn,9,#include stdafx.h #include using namespace System; using namespace std; int main(array args) int* pToRam=0; int answer=0; coutanswer; if(ans

14、wer=1) pToRam=new int; cout*pToRam; ,Dynamic Memory Allocation,tMyn,10,if(pToRam) coutThe integer which was stored dynamically: *pToRam.endl; else coutNothing was stored dynamically!endl; if(pToRam) coutLets deallocate the memory area allocated dynamically!endl; delete pToRam; pToRam=0; return 0; ,D

15、ynamic Memory Allocation,tMyn,11,Dynamic Memory Allocation,tMyn,12,Allocating memory for an array dynamically is straightforward. Assuming that we have already declared pstring of type pointer to char, we could allocate an array of type char in the free store by writing: char* pstring=0; pstring=new

16、 char20; This allocates space for a char array of 20 characters and stores its address in pstring. To remove the array that we have just created in the free store, we must use the delete operator: delete pstring; pstring=0;,Dynamic Memory Allocation,tMyn,13,#include stdafx.h #include using namespace

17、 System; using namespace std; int main(array args) int* whenNeeded=0; int answer; coutanswer; whenNeeded=new intanswer; for(int i=0; iwhenNeededi;,Dynamic Memory Allocation,tMyn,14,cin*(whenNeeded+i); coutWe were able to store dynamically integers! endl; for(int i=0; ianswer; i+) coutInteger entered

18、 was /whenNeededi *(whenNeeded+i) and the address was / ,Dynamic Memory Allocation,tMyn,15,Dynamic Memory Allocation,tMyn,16,Next example stores dynamically an array of type string:,Dynamic Memory Allocation,tMyn,17,#include stdafx.h #include #include #include using namespace System; using namespace

19、 std; int main(array args) int howMany=0; couthowMany; cin.get(); int* grades=new inthowMany; string* names=new stringhowMany;,Dynamic Memory Allocation,tMyn,18,for(int i=0; i*(grades+i); /cingradesi; cin.get(); for(int i=0; ihowMany; i+) cout.setf(ios:left); cout.width(20); coutendl*(names+i)*(grad

20、es+i); /coutendlnamesigradesi; ,Dynamic Memory Allocation,tMyn,19,coutendl; delete names; delete grades; return 0; ,Dynamic Memory Allocation,tMyn,20,There are two kinds of problem that can arise when you allocate memory dynamically. The first is called a memory leak, and it is caused by errors in your code (this can happen often!). A memory leak occurs when you allocate memory using new, and fail to release it when you are done with it. In this situation, its common to lose the address of the memory block by overwriting the address in the pointer you were using to access it. The

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论