How to Create an Array Using Malloc() in C Programming
文章推薦指數: 80 %
In c programming, the array is used to store a range of values of the same data type and it occupies some space in memory which can be either static or ...
Incprogramming,thearrayisusedtostorearangeofvaluesofthesamedatatypeanditoccupiessomespaceinmemorywhichcanbeeitherstaticordynamic.Themallocisafunctionusedinthecprogrammingfordynamicmemoryallocation.
Inthisarticle,wewilllearnaboutthemallocfunctiontocreateanarrayincprogramming.
Whatisamalloc()inCprogramming
Thedynamicmemoryisallocatedtotheprogramduringitsexecutionaccordingtothespaceneededbyit.Instaticmemory,thefixedmemoryisallocatedtotheprogrambeforeexecutionoftheprogramwhichhasthefollowingdisadvantages:
Thearraydeclaredwiththefixedsizewilloccupythefixedsizeonthesystemmemory
Ifthearrayhasvalueslessthanthesizedeclared,thefreespacewillbewastageandcannotbeusedbyanotherprogram
Ifthearrayhasvaluesmorethanthesizedeclared,theprogrammaygiveerrors
Toavoidthesedisadvantages,wewillusethedynamicmemoryallocationschemeasthisschemewillassignthememoryoftheblockneededbytheprogramduringitsexecution.Dynamicmemoryhasdifferentfunctionsusedasapointertowardstheprogram.
Themalloc()functionstandsfor“memoryallocation”andisusedfordynamicmemoryallocationwhileexecutionoftheprogram.Whenthemalloc()functioniscalled,itsendsarequestofamemoryblocktotheheap(itisamemorysegmentwherethememoryisallocatedrandomly).Iftheheaphasmemoryequivalenttothatmemoryblock,itwillaccepttherequestandassignthatsizetothemalloc()functionagainstitsrequest,andifithasnomemorythenitwillreturnthenullvalue.Whenwearedonewiththememoryblock,wecanclearitbyusingthefree()functionsothatthememoryblockcangetfreeandbeusedbytheotherprograminstructions.Fordynamicmemoryallocation,wehavetoincludethe“stdlib.h”inheaderfilesandthegeneralsyntaxofusingthemallocfunctionis:
1$pointer=(castType*)malloc(size);
Wecanuseanyvariableinsteadof“pointer”thenwecanreplacethe“castType”withthedatatypewhosevaluesaregoingtostoreinthearray.Thenusethemalloc()functionandmentionthesizeofthememoryweneeded.
HowtousemallocfunctioninCprogrammingtocreateanarray
Forabetterunderstandingofthecreationofanarrayusingthemalloc()function,wewillcreateaprogram.TousethecprogramminginLinux,wehavetoinstalltheGCCcompilerusingthecommand:
1$sudoaptinstallgcc
Createatextfileusingthenanoeditor:
1$nanomyfile.c
Typethefollowingcode:
123456789101112131415161718192021222324252627#include
延伸文章資訊
- 1How to Create an Array Using Malloc() in C Programming
In c programming, the array is used to store a range of values of the same data type and it occup...
- 2C dynamic memory allocation - Wikipedia
- 3Dynamic Memory Allocation in C using malloc(), calloc(), free ...
An array is a collection of items stored at contiguous memory locations. arrays ... ptr = (int*) ...
- 4Can malloc() be used to define the size of an array? - Stack Overflow
- 5and two-dimensional arrays in C - Dive Into Systems
We can allocate an array with malloc(sizeof(int) * (3*. Figure 3. The results of allocating a 2D ...