What is a malloc function in C language? - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

The malloc() function stands for memory allocation, that allocate a block of memory dynamically. It reserves the memory space for a ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus TrendingCategories DataStructure Networking RDBMS OperatingSystem Java iOS HTML CSS Android Python CProgramming C++ C# MongoDB MySQL Javascript PHP SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho WhatisamallocfunctioninClanguage? CServerSideProgrammingProgramming Themalloc()functionstandsformemoryallocation,thatallocateablockofmemorydynamically.Itreservesthememoryspaceforaspecifiedsizeandreturnsthenullpointer,whichpointstothememorylocation.malloc()functioncarriesgarbagevalue.Thepointerreturnedisoftypevoid.Thesyntaxformalloc()functionisasfollows−ptr=(castType*)malloc(size);ExampleThefollowingexampleshowstheusageofmalloc()function. LiveDemo#include #include #include intmain(){   char*MemoryAlloc;   /*memoryallocateddynamically*/   MemoryAlloc=malloc(15*sizeof(char));   if(MemoryAlloc==NULL){    printf("Couldn'tabletoallocaterequestedmemory\n");   }else{    strcpy(MemoryAlloc,"TutorialsPoint");   }   printf("Dynamicallyallocatedmemorycontent:%s\n",MemoryAlloc);   free(MemoryAlloc); }OutputWhentheaboveprogramisexecuted,itproducesthefollowingresult−Dynamicallyallocatedmemorycontent:TutorialsPoint Mandalika Updatedon20-Feb-202110:27:42 RelatedQuestions&AnswersWhatismallocinClanguage? ExplainmallocfunctioninCprogramming Whatisstrcpy()FunctioninClanguage? Whatisstrcat()FunctioninClanguage? WhatisstrlenfunctioninClanguage? Whatisstrcoll()FunctioninClanguage? Whatisstrspn()FunctioninClanguage? Whatisstrstr()FunctioninClanguage? Whatisstrncat()FunctioninClanguage? Whatisstrcmp()FunctioninClanguage? Whatisstrncmp()FunctioninClanguage? Whatisstrncpy()FunctioninClanguage? Whatisstrrev()FunctioninClanguage? WhatisfunctionprototypeinClanguage Whatisexit()functioninClanguage? PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?