DynamicMemoryAllocation动态内存allocation

上传人:e****s 文档编号:252565568 上传时间:2024-11-17 格式:PPT 页数:21 大小:69KB
返回 下载 相关 举报
DynamicMemoryAllocation动态内存allocation_第1页
第1页 / 共21页
DynamicMemoryAllocation动态内存allocation_第2页
第2页 / 共21页
DynamicMemoryAllocation动态内存allocation_第3页
第3页 / 共21页
点击查看更多>>
资源描述
Muokkaa otsikon perustyyli napsauttamalla,Muokkaa tekstin perustyylej napsauttamalla,toinen taso,kolmas taso,neljs taso,viides taso,Dynamic Memory Allocation,*,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 or not.,Working with a fixed set of variables in a program can be very restrictive,and it is often wasteful.,Dynamic Memory Allocation,1,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 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.,The obvious place to store this address is in a pointer.,Dynamic Memory Allocation,2,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,Efficient,Variable capacity,Dynamic Memory Allocation,3,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 by 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,4,You can allocate space in the free store for some 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 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,5,Suppose that we need space for a variable of type double.,We can define a pointer to type double,and 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 stored in the pointer pValue.,Dynamic Memory Allocation,6,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 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,since access to the address will have been lost.,Dynamic Memory Allocation,7,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 free and may immediately be allocated to something else possibly by another program.,To avoid that risk:,delete pValue;,pvalue=0;,Dynamic Memory Allocation,8,#include,stdafx.h,#include,using namespace System;,using namespace std;,int main(array args),int*pToRam=0;,int answer=0;,cout,Allocate memory dynamically?,answer;,if(answer=1),pToRam=new int;,cout*pToRam;,Dynamic Memory Allocation,9,if(pToRam),cout,The integer which was stored dynamically:,*pToRam.endl;,else,cout,Nothing was stored dynamically!endl;,if(pToRam),cout,Lets deallocate the memory area allocated,dynamically!endl;,delete pToRam;,pToRam=0;,return 0;,Dynamic Memory Allocation,10,Dynamic Memory Allocation,11,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 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,12,#include,stdafx.h,#include,using namespace System;,using namespace std;,int main(array args),int*whenNeeded=0;,int answer;,coutanswer;,whenNeeded=,new intanswer;,for(int i=0
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业管理 > 商业计划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!