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