How to use "malloc" in C - Educative.io

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

Memory allocation (malloc), is an in-built function in C. This function is used to assign a specified amount of memory for an array to be created. SolutionsEducativeEnterpriseEnablementplatformDevelopersLearnnewtechnologiesProductsCoursesforEnterpriseSuperchargeyourengineeringteamCoursesforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsAnswersTrustedAnswerstoDeveloperQuestionsPricingForEnterpriseTailoredforyourteamForIndividualsStayaheadofthecurveCoursesLogInJoinforfreeTrustedanswerstodeveloperquestionsTrustedAnswerstoDeveloperQuestionsRELATEDTAGSHowtouse"malloc"inCEducativeAnswersTeamMemoryallocation(malloc),isanin-builtfunctioninC.Thisfunctionisusedtoassignaspecifiedamountofmemoryforanarraytobecreated.Italsoreturnsapointertothespaceallocatedinmemoryusingthisfunction. Theneedformalloc Intheworldofprogrammingwhereeveryspacecounts,therearenumeroustimeswhenweonlywantanarraytohaveaspecificamountofspaceatruntime.Thatis,wewanttocreateanarrayoccupyingaparticularamountofspace,dynamically.Wedothisusingmalloc. Syntax Weknowwhatmallocreturnsandweknowwhatitrequiresasaninput,buthowdoesthesyntaxofthefunctionwork.Theillustrationbelowshowsthat: Note:mallocwillreturnNULLifthememoryspecifiedisnotavailableandhence,theallocationhasfailed Examples Nowthatweknowhowmallocisusedandwhyitisneeded,let’slookatafewcodeexamplestoseehowitisusedinthecode. #include #include intmain(){ int*ptr1; //Wewantptr1tostorethespaceof3integers ptr1=(int*)malloc(3*sizeof(int)); if(ptr1==NULL){ printf("Memorynotallocated.\n"); } else{printf("Memoryallocatedsuccesfully.\n"); //Thisstatementshowswherememoryisallocated printf("Theaddressofthepointeris:%u\n",ptr1); //Hereweassignvaluestotheptr1created for(inti=0;i<3;i++){ ptr1[i]=i; } //Printingthevlauesofptr1toshowmemoryallocationisdone for(inti=0;i<3;i++){ printf("%d\n",ptr1[i]); } } }RunRELATEDTAGSCopyright©2022Educative,Inc.AllrightsreservedRELATEDCOURSESViewallCoursesKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsRESOURCESEducativeBlogEducativeAnswersCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceABOUTUSOurTeamCareersHiringMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServicePRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringRESOURCESEducativeBlogEducativeAnswersMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsRESOURCESEducativeBlogEducativeAnswersCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsCopyright©2022Educative,Inc.Allrightsreserved.



請為這篇文章評分?