C library function - malloc() - Tutorialspoint

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

C library function - malloc(), The C library function void *malloc(size_t size) allocates the requested memory and returns a pointer to it. Home CodingGround Jobs Whiteboard Tools Business Teachwithus TheCStandardLibrary CLibrary-Home CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CLibrary- CStandardLibraryResources CLibrary-QuickGuide CLibrary-UsefulResources CLibrary-Discussion CProgrammingResources CProgramming-Tutorial C-UsefulResources SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Clibraryfunction-malloc() Advertisements PreviousPage NextPage  Description TheClibraryfunctionvoid*malloc(size_tsize)allocatestherequestedmemoryandreturnsapointertoit. Declaration Followingisthedeclarationformalloc()function. void*malloc(size_tsize) Parameters size−Thisisthesizeofthememoryblock,inbytes. ReturnValue Thisfunctionreturnsapointertotheallocatedmemory,orNULLiftherequestfails. Example Thefollowingexampleshowstheusageofmalloc()function. LiveDemo #include #include intmain(){ char*str; /*Initialmemoryallocation*/ str=(char*)malloc(15); strcpy(str,"tutorialspoint"); printf("String=%s,Address=%u\n",str,str); /*Reallocatingmemory*/ str=(char*)realloc(str,25); strcat(str,".com"); printf("String=%s,Address=%u\n",str,str); free(str); return(0); } Letuscompileandruntheaboveprogramthatwillproducethefollowingresult− String=tutorialspoint,Address=355090448 String=tutorialspoint.com,Address=355090448 stdlib_h.htm PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?