Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views14 pages

PPS Unit-4 Notes-2

The document discusses dynamic memory allocation techniques in programming, highlighting the differences between static and dynamic memory allocation. It explains how dynamic data structures allow for flexible memory management, optimizing storage space and enabling the allocation of memory at runtime. Various functions for memory allocation, such as malloc and calloc, are described, along with examples of their usage in C programming.

Uploaded by

vanithatellakula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

PPS Unit-4 Notes-2

The document discusses dynamic memory allocation techniques in programming, highlighting the differences between static and dynamic memory allocation. It explains how dynamic data structures allow for flexible memory management, optimizing storage space and enabling the allocation of memory at runtime. Various functions for memory allocation, such as malloc and calloc, are described, along with examples of their usage in C programming.

Uploaded by

vanithatellakula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
Bynamfo namoxy aniocation ' — Most often data is dynamic in nature, toy the numbos on} data ttoms keop Changing Quoi execution tho peagtor. Exompio 4 e oF Progiam fos Poocessi rnp playoo “The list Qtous tohen employee rames are acted Ard Shvinks when employpa Tames aso deleted Peoblero — tohen list gaous woo need to attecato Mose rmernoay “Space to acceremech to the colditional rormes. Solution — such sftuations can be hardled! mos Casily by ayramic data Stsuctuses ts} Ayramic rmemesy rraragement -ochniquos, Dyramic data Skouctusos — provide spoxibi lity rm ocdirg , Aelcting ev seassangirg data ttoms ak sun Hme. Byramfo mernesy rraragerment bechniques posroft Us to Allocate Addittoral roe 3 weleasa Unwanted & io os to eo at won Limo) thus optimising tho Use of Stosoge Space. . Dypamic Stosage _traragement functions tex) Bynamic Memooy AtlocaHon + “The Psccass af Allocating mernosy at suntime 1S known as ayramio momesy attecation. Dipffesorce kotwoon Static merosy V's Eysemfe, meray Static — Mmemesy ). Mramosy Attocation fa cone baifees “8 (fo. using Compile time) arn execution 2, Memosy artocation fs fixed. jo., Once moa is allocated two Cannot WNcscase os Aacsecee| tha Sige Oo tho Memosy. 3, TE usos the data Stsuctusn Catied Stack tow immplomontirg Static allocation of romouy A. \tosiables xornain Posreanentty allocated in—E Q=toy 6. Eq: int ofs}: int af2]= 2 to, 204, hos romdig= " Wot come": 7 Byramic Perron promesy attocation is done usirg psuytars exocution Ge, Aesing bun roe ) “Tha tornosy sige fs Not constant. So me can be Irascasad 038 Cacscased based on cur sequisamont. LE uses heap foo Maragirg he dyrarnia Atloca tion of me my Me con ko Allocated ak al Herne ard can ko wateasad ak ony time 3 Int eka; a=(int *) Catloa C to, Sigcod Cink). wae Le Seah ert) Atloca’ . to Ayramia rmemosy Those ose 4 libsoay soutiras Known as "mo: ormank nations! that con ba used sox ONecalirg ord 4frecirg merosy dusirg psegyon exocutton , Mem Aniocattion furctions ‘J rratioe — Atiecatas saquested sige o-} bytes ard retusns a pointes to die iss byte to the : allocated Space. 2) cattoe — Atlocates Space foo an asoy of Clements , jnitiaizes thom to geo ard then sotusns a Pontos to the. Memory 3) -foco — -fooes pacviously Allocated pace 4) weative — Mectijios “tho “siga of poo vious! QNlecated Space q C Newey Anocation povcess: Corce ptuat Niew of Stosage oO 2 BRO in mombdsy 1 “dg | P 7 Loca) Niosiablos | Stack F Fece rmereay |] Heap Givba) Nosiablas Posrmanent Stovoge ABI a m instouctions Paxgrarn in | * poogtam Ingtouetions — Global & static Ntasiables asa Stosed fn _postmanont Stosage Avx0n & Loa N1dsiablos ase Stosod fn Stack *& Pnormosy Space jae, tocated between posrranent Stpsago aso and 2) / ts ANailable fos Aynamio allocation using execution of lhe poogrorn . This Jvoe rmerrosy sagion is callod heap ok The Sige ©} tho heap keeps Chargos wohen tho Psegror is eCxecutod. -'. Tt is possible to Oncountos memo \ oves-}tow! Alusing Ayramic aNecaton psocess. -' TE wotusns A Nutt pointos —cohen ail to locate enough momeay voguestad DARN 2 Beck et meres. (mouea) —A block a} momosy — ailoaated using’ the function — ration — rollec dunction sosontes a btock 6, mernosy a] 2 iftod SIZe and setusns a Poin tos ed po Noid . / Syntax: pts = (cast_typor) rnatioa Chyto— sige): pto fs a pointes a} typo Cast._typo matloc Setusns A pointes vith So as byto_sito . Fe @. [R= Cint ¥) rratioe C too * Sige} Cint)), | Lshese X18 A pointes , it atlocates “loo Has stho Sige of an int" (ie. too x4.=400 byloy And tha addsoss 6-4} tha fist byte of tho memesy is assignad to tho pointos x of Eypo int. @. [x= Cink +) matioe Csigee| Cint)):| TE atlocates a Sing Jo Mernovy beck (ler Ing = 4 bytes) © CPt = (chase matloe Cte); TE atlocates lo bytos © Space dos dhe Pointes cps of typo Chag Cos) [cpl= Cokes) rratiee (toe Sige | Coho); @. Peatloc ts aso used to atloacate Space clos Complox data typos Such as Stauctens [S& sea = (stsuct Stoo +) mratioe (Seo (atsuct Stoxl tohese St_vay tea pointes of typo gksuck stove Pratloa atlocatos a bleak od Contiguous | “The attocation can fii) if tho Space in hoap is pot Serftioront, Suppose if it foils, FL sotusns a Nutr ii), Atiocatieg routtfplo blocks of mmoreony : Canttoc) Catloc fa anothos tmomesy antocatten function hex oorrally used -fos sequosting mamesy Space ak bun timo tos Stosing dosived Gata typas Such as Omays ard Stsuctusos, Syotax [Pere (cost type *) catioe (7, lemsize) | Tt atlocatos Contiquous Space -foo M blocks cach Of Sijo Clam sige bytos Au Ave initiatigac) to Zoro ard pointer to the ftsst byte o4} the allocated sEgion is sctusned. Stouct Studen 2 chos rame[2s7; oe Oo long int idenur) » 7 Eypocla} Sksuct Student accosc!! paces] ASt_pts > Int class_se =30! Gk_pts = Ceccosd *) cattoa (ctass_spe 3 = Siheof C secod )) socord 8 of typo Stouat Stuclent » aottoc attocatos Mmermooy to heid data Jos 40 s0cc0cs , Di}ffosonce hetwoen matloc ard calloc Ditfesonce between Te canoe _ Matloc ). LE Attocates aSirgle | Tt atlocates multiple bigcks block of Stoxoge | Steroge Cmemosy) Of tho . Anorng she sae of 2 beck: Covatee) beta. can charge sthe. memosy Sige a Autecated bith the hatp of the cforction neatiec. This psocess is called tho seallocation o} mermosy, “Sypicn [fle= veates (pte, roseste);| Assume FY the esigina} attocation is done dhe staternent, pev= rrantoc (SiZe) 5 Example psegrams; based on Anocating ond poefrq Momo O. sam of Gremants_using_mator: sHnclude 2atdio. hs ‘ . Atiralude 2stdtb-h> int maint) nt 7,1, RPes, sum=0; Pointe} (" Entosx cumbes of Claments); Sean} ("¥d" en); pes= Lint -k) matioc Cok Si. Cink); if (pes ==Nut) pT print} (" unable to alt exitto)? oman momeny)! Point+ (“Entos claments fos Ci= 0; fen +41) lay veo is Sean} ("Zd", ptsti) ; Sum += % Cpts+i) ; 26 Sum = 60 Paine} ("cum = xd" Sum): dove Cpts)> se kusn o @ sum of TY etoments using collec: tFinelude 4Stdio.h> tinelude 2 Stadlib:h> nt rain) int 7,1, pts, Sum= o> point} ("Enter nurnbes! of elarnonts : % cond (“¥d" xn): Pto= Cint #) canoe Cn, Sigoo} Cink )): i (ptr= = Nutt) . Point} (“unable to Allocate mei go rey’) point} (" Entos a} A rp (ino; tensiasy oY b 3 Bot Scan} C\xd"! Pto-ti) > Ertos numbos o} Olorrerks Sum += & Cptsti) + 38 Entos Clormonts o- ane Psints (Nicum= %d",Sum), to -}ooo (pts); ay 30 wetug , een Sum = bo @ Steing hardting Junction (Stsaat, stecpy) usirg poalloc tHrctuded.stdio- hs Setpes sHirotude 2stdlib-h> Poogsarnrmi Hinclude 4stoirg.h»s de "J Int maint) 6 Pargoremirg Inc a chaa apes; pts = (chos *) matioo 16); Stocpy (pew,"psogrararni "ys Point-f ( "xa" Pts)! Pto= (chos*) soar Boke ; aHiratude Stouct omp 2 > . Int eno; choy aname [20J; ttoat esat; 7 Void troinc) é Stouct emp pts, int Vn : paint} (" Ertes numbos of socoedls"); Scan} ("%d"\ sen) Pts = (stsuct emp ) rratloc (1m % size} if (pts == Nute) CStouct 2mp))" " print} (" rmomesy Not oNailable"); 2xitto); also Paint} (\ omtos employee ctotails In"), Jos 0; tens I+) Sean} (“4d Ao A4 ", & Cptati) 9 Ono, 4 Cpts+ids ename, x (pes +) esa): Pint} a \n Bisploying infooreation ) -fooCi=oy fens Vet) Point} (In xd xs %f ll Cpts-ti)— ano, (pts +i) Sename, Cpes+) —esa}); Proetpts); gatchty, “output Entos numbes af wocosds 2 Ertes employe details 1 ‘Tom too 2 Jessy 700 Displayirg inefoorna Hon \ ‘Tom . loo R Tessy 200. Poogeam to Aoronstsate matloa and catloc, } maint int rain t) . ) y ie Int ¥%a,1; jnt *a,) a= mattioc (see} Cink) sto); |O= catloc (sigea} Cnt) Aox (i=0 ; 14 tor i++) dos Cio 12 test) paint} (aL xdj=zd ln", Lay] pant} (Yo Led = -}oe0 ta), 2 (oi) zalny atiy)y wotuan 0; -froe lay; 5 wetwsano} output eae alo) =) +0. oafoy}= o ati) - © attj= o af) 0 afsjJ= o afs)= 12678 ae S afqj= © afey= 0 afe]= oO afej= © Lej= btaqle aft= 0 CH= © afaj= 0 Alej= o afaJ=o afaj= 6 Dispiay the_numbass Ip Ascording | Pasc ordirg oacler Using Mmatioc }catloc Jnt reainc) 2 x Tnt a, 1) A= tnatloe (S140 Of Cint) 10); oF A= calloc (si,eo4 Cit) , lo); =o; 14top I+ : fe ca = at, } Boscercling oscar atij = te} ‘Ff ASrdieg oodor . -fox Ci=o, fetoy i++) paint} (Natzd}= Zale", 1, ai), tooo (a); ° os Cat) ye © Ascarding osder pene oe ato) = put afes= lo arya | out aftij= 9 oe 2 afej= §& aA(sj= 3 afaJ= 7 afe]= + afaj= §& atsjJ=z afsj= = afej= 6 atejJ= 4 alt= 7 atij= 3 afey=& ofay= 2 afaj= q | ey =

You might also like