欢迎来到天天文库
浏览记录
ID:41293461
大小:334.31 KB
页数:18页
时间:2019-08-21
《DynamicMemoryAllocation(DMA)-AmazonS3动态内存分配(DMA)-亚马逊S》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Lecture18Accessing2-DimensionalArray+DynamicMemoryAllocation(DMA)DynamicMemoryAllocationUsedwhenspacerequirementsareunknownatcompiletime.Mostofthetimetheamountofspacerequiredisunknownatcompiletime.Forexampleconsiderthelibrarydatabaseexample,whatabout201thbookinformation.Usingstaticmemoryall
2、ocationitisimpossible.DynamicMemoryAllocation(DMA):-WithDynamicmemoryallocationwecanallocate/deletesmemory(elementsofanarray)atruntimeorexecutiontime.ThreeFunctionsforDMAoperationsinClanguage.IncludeorfilesbeforeusingmallocreallocfreeDMAusingmalloc(1/3)Syntaxvoid*malloc(s
3、ize_tsize)Returnvalue:Onsuccess,mallocreturnsapointertothenewlyallocatedblockofmemory.Onerror(ifnotenoughspaceexistsforthenewblock),mallocreturnsnull.Iftheargumentsize=0,mallocreturnsnull.Returnstheaddressofnewlyallocatedmemoryofanydatatype(int/float/long/double/char)Inbytes,malloc(3)meansy
4、ouareallocationonly3bytesDMAusingmalloc(2/3)int*ip;ip=(int*)malloc(20);if(ip==NULL){printf(“PointerisNULL”);}elseprintf(“PointerisnotNULL”);0123456789*ipDMAusingmalloc(3/3)Example1:-(Allocatingfloattypememory)ip=(float*)malloc(40);//allocates10floattypeslots.Example2:-(Allocatingdoublet
5、ypememory)ip=(double*)malloc(80);//allocates10doubletypeslots.Example3:-(Allocatinglongtypememory)ip=(long*)malloc(40);//allocates10longtypeslots.Example4:-(Allocatingchartypememory)ip=(char*)malloc(10);//allocates10chartypeslots.Assigning/AccessingdatainDMA(1/3)voidmain(void){float*DMA;DMA
6、=(float*)malloc(40);//10slotsareallocatedfloat*pointer=DMA;//assignfirstelementaddress*pointer=29.53;pointer+=7;*pointer=67.23;printf(“Thevalueat7thlocationis%d“,*pointer);}012345678929.5367.23Assigning/AccessingdatainDMA(Alternativeway)(2/3)voidmain(void){float*DMA;DMA=(float*)malloc(40);/
7、/10slotsareallocatedDMA[0]=29.53;DMA[7]=67.23;printf(“Thevalueat7thlocationis%d“,DMA[7]);}012345678929.5367.23Assigning/AccessingdatainDMA(Alternativeway)(3/3)voidmain(void){char*DMA;DMA=(char*)malloc(10);//10slotsareallocatedstrcpy(DMA,“Pakistan”);print
此文档下载收益归作者所有