FAQ

Published on May 2019 | Categories: Documents | Downloads: 85 | Comments: 0 | Views: 427
of 42
Download PDF   Embed   Report

Comments

Content

FAQ's of C Programming Q1. main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. . Runtime error. !ccess iolation. c. #ompile error. $lleal snta' d. one of te aoe  !ns* d, printf( printf( ) prints prints addressar addressarae ae of i, scanf() dont a & sin, so scans address for i +1, -1 dont a an effect on code Q. main(int arc, car ar/) { (main && arc) 0 main(arc-1, 22) * return 3; } a. Runtime error. . #ompile error. $lleal snta' c. 4ets into $nfinite loop d. one of te aoe  !ns* ) illeal illeal snta' snta' for usin usin return return Q5. main() { int i; float pf; pf 6 (float )&i; pf 6 133.33; printf("7n %d", i); } a. Runtime error. . 133 c. 8ome $nteer not 133 d. one of te aoe  !ns* d) 3 Q9. main()

{ int i 6 3'ff ; printf("7n%d", i::); } a. 9 . 1 c. 133 d. 139  !ns* c) 133 133 Q. <define 8QR(') '  ' main() { printf("%d", 8QR(1)); } a. 1 .  c. 1 d. none of te aoe  !ns* )   Q=. union u { struct st { int i * 9; int > * 9; int ? * 9; int l; }st; int i; }u; main() { u.i 6 133; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 9, 9, 3 . 3, 3, 3 c. 133, 9, 3 d. 93, 9, 3  !ns* c) 133, 133, 9, 3 Q@. union u

{ union u { int i; int >; }a13/; int 13/; }u; main() { printf("7n%d", siAeof(u)); printf(" %d", siAeof(u.a));  printf("%d", siAeof(u.a9/.i)); } a. 9, 9, 9 . 93, 9, 9 c. 1, 133, 1 d. 93 933 9  !ns* 3, 33, error error for 5rd printf  QB. main() { int (functale/)(car format, ...) 6{printf, scanf}; int i 6 133; (functale3/)("%d", i); (functale1/)("%d", i); (functale1/)("%d", i); (functale3/)("%d", &i); } a. 133, Runtime error. . 133, Random numer, Random numer, Random numer. c. #ompile error  d. 133, Random numer  QC. main() { int i, >, p; i 6 ;  > 6 133; p 6 &i;  !ddress of i is assined to pointer p printf("%f", i(p) );  i is diided  pointer p } a. Runtime error. . 1.33333 c. #ompile error  d. 3.33333

 !ns* c) Drror Drror ecoA ecoA i(p) is  i.e 1 Eic Eic is int & printed as a float, float, 8o anormal proram termination, runs if (float) i(p) -----F Gpe #astin Q13. main() { int i, >; scanf("%d %d"+scanf("%d %d", &i, &>)); printf("%d %d", i, >); } a. Runtime error. . 3, 3 c. #ompile error  d. te first tEo alues entered  te user   !ns* d) tEo tEo alues alues entered, entered, 5rd Eill Eill e null null pointer assinment assinment Q11. main() { car p 6 "ello Eorld"; p3/ 6 HIH; printf("%s", p); } a. Runtime error. . "Iello Eorld" c. #ompile error  d. "ello Eorld"  !ns* ) Iello Iello Eorld Eorld Q1. main() { car  str!; car  strJ 6 $ am KL; memcp( str!, strJ, =); } a. Runtime error. . $ am KL c. #ompile error  d. $ am K  !ns* c) $ am KL is not in " " Q15. IoE Eill ou print % caracter0 a. printf("7%") . printf("77%") c. printf("%%") d. printf("7%%")

 !ns* c) printf(" %% "); Q19. const int perple'ed 6 ; <define perple'ed 5 main() { <ifdef perple'ed <undef perple'ed <define perple'ed 9 <endif  printf("%d",perple'ed); } a. 3 .  c. 9 d. none of te aoe  !ns* c) Q1. struct Moo { car pame; }; main() { struct Moo o> 6 malloc(siAeof(struct Moo)); clrscr(); strcp(o>-Fpame,"Nour ame"); printf("%s", o>-Fpame); } a. Nour ame . compile error  c. ame d. Runtime error   !ns a) Q1=. struct Moo { car pame; car p!ddress; }; main() { struct Moo o> 6 malloc(siAeof(struct Moo)); clrscr(); o>-Fpame 6 malloc(133);

o>-Fp!ddress 6 malloc(133); strcp(o>-Fpame,"Nour ame"); strcp(o>-Fp!ddress, "Nour !ddress"); free(o>); printf("%s", o>-Fpame); printf("%s", o>-Fp!ddress); } a. Nour ame, Nour !ddress . Nour !ddress, Nour !ddress c. Nour ame Nour ame d. one of te aoe  !ns* d) printd otin, as after free(o>), no memor is tere containin o>-Fpame & p>-Fp!ddress Q1@. main() { car a 6 "Iello "; car  6 "Oorld"; clrscr(); printf("%s", strcat(a,)); } a. Iello . Iello Oorld c. IelloOorld d. one of te aoe  !ns* ) Q1B. main() { car a 6 "Iello "; car  6 "Oorld"; clrscr(); printf("%s", strcp(a,)); } a. "Iello" . "Iello Oorld" c. "IelloOorld" d. one of te aoe  !ns* d) Oorld, copies Oorld on a, oerErites Iello in a. Q1C. oid func1(int (a)13/) { printf("K? it Eor?s"); } oid func(int a/13/)

{ printf("Oill tis Eor?0"); } main() { int a13/13/; func1(a); func(a); } a. K? it Eor?s . Oill tis Eor?0 c. K? it Eor?sOill tis Eor?0 d. one of te aoe  !ns* c) Q3. main() { printf("%d, %d", siAeof(HcH), siAeof(133)); } a. ,  . , 133 c. 9, 133 d. 9, 9  !ns* a) ,  Q1. main() { int i 6 133; clrscr(); printf("%d", siAeof(siAeof(i))); } a.  . 133 c. 9 d. none of te aoe  !ns* a)  Q. main() { int c 6 ; printf("%d", mainPPc); } a. 1

.  c. 3 d. none of te aoe  !ns* a) 1, if Ee use mainPc ten error, illeal use of pointer  Q5. main() { car c; int i 6 9=; clrscr(); c 6 i; printf("%d", c); } a. 9= . -9= c. random numer  d. none of te aoe  !ns* d) -= Q9. oid main () { int ' 6 13; printf ("' 6 %d,  6 %d", ',--'++); } a. 13, 13 . 13, C c. 13, 11 d. none of te aoe  !ns* d) 2alue reuired Q. main() { int i 613, > 6 3; clrscr(); printf("%d, %d, ", >-- , --i); printf("%d, %d ", >++ , ++i); } a. 3, 13, 3, 13 . 3, C, 3, 13 c. 3, C, 1C, 13 d. 1C, C, 3, 13  !ns* c) Q=. main() { int '6;

clrscr(); for(;'663;'--) { printf("'6%d7n"", '--); } } a. 9, 5, , 1, 3 . 1, , 5, 9,  c. 3, 1, , 5, 9 d. none of te aoe  !ns* d) prints notin, as condition '663 is Malse Q@ main() { int '6; for(;'63;'--) { printf("'6%d7n", '--); } } a. , 9, 5, ,1 . 9, 5, , 1, 3 c. , 5, 1 d. none of te aoe  !ns* d) $nfinite loop as ' is decremented tEice, it neer e 3 and loop is oin on & on QB main() { int '6; clrscr(); for(;':6 3;'--) { printf("'6%d ", '--); } } a. , 5, 1 . , , 1, c. , 5, 1, -1, 5 d. -5, -1, 1, 5,   !ns* prints notin, as condition in loop is false. QC. main() { {

unsined int it6=; printf("%d", it); } { unsined int it61; printf("%d", it); } } a. =, = . 1, 1 c. =, 1 d. #ompile error   !ns* =, 1, ecoA tese r different loc?s, so declaration alloEed Q53. main() { int i; clrscr(); for(i63;i:;i++) { printf("%d7n", 12 :: i); } } a. , 9, 5, , 1 . 3, 1, , 5, 9 c. 3, 1, , 9, B d. 1, , 9, B, 1=  !ns* d) 2 doesHt ma?e an diff. Q51. main() { sined int it61, i6; for(;i;i--) { printf("%d7n", it 6 (it FF (i - (i -1)))); } } a. 1, =, 1B, =9, 5 . =, 1B, =9, 5, 1= c. 1B, =9, 5, 1=, B d. =9, 5, 1=, B, 9  !ns* ) Q5. main() {

sined int it61, i6; for(;i;i--) { printf("%d7n", it FF (i - (i -1))); } } a. 1, =, 3, 3, 3 . =, =, 3, 3, 3 c. 1, 1, 1, 1, 1 d. =, =, =, =, =  !ns* d) itHs alue is not caned Q55. main() { if ((1&&3)) { printf("KL $ am done."); } else { printf("KL $ am one."); } } a. KL $ am done . KL $ am one c. compile error  d. none of te aoe  !ns* a) Q59 main() { if ((1PP3) && (3PP1)) { printf("KL $ am done."); } else { printf("KL $ am one."); } } a. KL $ am done . KL $ am one c. compile error  d. none of te aoe

 !ns* a) Q5 main() { sined int it61, mJit; { mJit 6 Sit; it 6 it & Sit ; printf("%d %d", it, mJit); } } a. 3, 3 . 3, 15 c. 1, 3 d. 3, -15  !ns* d)

C++

FAQ's of C++, OOPS and Programming IoE do ou Erite a proram Eic produces its oEn source code as its output0 IoE can $ find te da of te Eee? ien te date0 O doesnHt # ae nested functions0 Oat is te most efficient Ea to count te numer of its Eic are set in a alue0 IoE can $ conert inteers to inar or e'adecimal0 IoE can $ call a function, ien its name as a strin0 IoE do $ access command-line aruments0 IoE can $ return multiple alues from a function0 IoE can $ ino?e anoter proram from Eitin a # proram0

IoE can $ access memor located at a certain address0 IoE can $ allocate arras or structures ier tan =9L0 IoE can $ find out oE muc memor is aailale0 IoE can $ read a director in a # proram0 IoE can $ increase te alloEale numer of simultaneousl open files0 OatHs Eron Eit te call "fopen("c*7neEdir7file.dat", "r")"0 Oat is te output of printf("%d") Oat Eill appen if $ sa delete tis Tifference etEeen "# structure" and "#++ structure". Tiffrence etEeen a "assinment operator" and a "cop constructor" Oat is te difference etEeen "oerloadin" and "oerriddin"0 D'plain te need for "Uirtual Testructor". #an Ee ae "Uirtual #onstructors"0 Oat are te different tpes of polmorpism0 Oat are Uirtual Munctions0 IoE to implement irtual functions in "#" Oat are te different tpes of 8torae classes0 Oat is amespace0 Oat are te tpes of 8G2 containers0. Tifference etEeen "ector" and "arra"0 IoE to Erite a proram suc tat it Eill delete itself after e'ectution0 #an Ee enerate a #++ source code from te inar file0 Oat are inline functions0 Gal? sometimin aout profilin0

IoE man lines of code ou ae Eritten for a sinle proram0 Oat is "strstream" 0 IoE to Erite Vultitreaded applications usin #++0 D'plain "passin  alue", "passin  pointer" and "passin  reference" Orite an small proram tat Eill compile in "#" ut not in "#++" Iae ou eard of "mutale" ?eEord0 Oat is a "RGG$"0 $s tere sometin tat $ can do in # and not in #++0 O preincrement operator is faster tan postincrement0 Oat is te difference etEeen "calloc" and "malloc"0 Oat Eill appen if $ allocate memor usin "neE" and free it usin "free" or allocate sin "calloc" and free it usin "delete"0 Oat is Vemor !linment0 D'plain Eor?in of printf. Tifference etEeen "printf" and "sprintf". Oat is "map" in 8G20 Oen sall $ use Vultiple $neritance0 Oat are te tecniues ou use for deuin0 IoE to reduce a final siAe of e'ecutale0 4ie  e'amples of a code optimiAation. Oat is ineritance0 Tifference etEeen #omposition and !reation. Tifference* 8euence Tiarams, #ollaoration Tiarams.

Tifference* HusesH, He'tendsH, HincludesH Oat sall $ o for Wac?ae Tiaram0 Oat is Wolmorpism0 $s class an K>ect0 $s o>ect a class0 #omment* #++ "includes" eaior and >aa "imports" Oat do ou mean  "RealiAation"0 Oat is a Wresistent, Gransient K>ect0 Oat is te use of Kperator Kerloadin0 Toes V2 uarantee pro>ect success0 Tifference* !ctiit Tiaram and 8euence Tiaram. Oat is association0 IoE to resole man to man relationsip0 IoE do ou represent static memers and astract classes in #lass Tiaram0 Oat does static ariale mean0 Oat is a pointer0 Oat is a structure0 Oat are te differences etEeen structures and arras0 $n eader files Eeter functions are declared or defined0 Oat are te differences etEeen malloc() and calloc()0 Oat are macros0 Eat are its adantaes and disadantaes0 Tifference etEeen pass  reference and pass  alue0 Oat is static identifier0 Oere are te auto ariales stored0

Oere does loal, static, local, reister ariales, free memor and # Wroram instructions et stored0 Tifference etEeen arras and lin?ed list0 Oat are enumerations0 Tescrie aout storae allocation and scope of loal, e'tern, static, local and reister ariales0 Oat are reister ariales0 Oat are te adantae of usin reister  ariales0 Oat is te use of tpedef0 #an Ee specif ariale field Eidt in a scanf() format strin0 $f  possile oE0 Kut of fets() and ets() Eic function is safe to use and E0 Tifference etEeen strdup and strcp0 Oat is recursion0 Tifferentiate etEeen a for loop and a Eile loop0 Oat are it uses0 Oat are te different storae classes in #0 Orite doEn te euialent pointer e'pression for referrin te same element ai/>/?/l/0 Oat is difference etEeen 8tructure and nions0 Oat te adantaes of usin nions0 Oat are te adantaes of usin pointers in a proram0 Oat is te difference etEeen 8trins and !rras0 $n a eader file Eeter functions are declared or defined0 Oat is a far pointer0 Eere Ee use it0 IoE Eill ou declare an arra of tree function pointers Eere eac function receies tEo ints and returns a float0

Eat is a 22 Wointer0 Oeter it is same as an uninitialiAed pointer0 Oat is a 22 Vacro0 Oat is te difference etEeen a 22 Wointer  and a 22 Vacro0 Oat does te error Hull Wointer !ssinmentH mean and Eat causes tis error0 Oat is near, far and ue pointers0 IoE man tes are occupied  tem0 IoE Eould ou otain sement and offset addresses from a far address of a memor location0  !re te e'pressions arr and &arr same for an arra of inteers0 Toes mentionin te arra name ies te ase address in all te conte'ts0 D'plain one metod to process an entire strin as one unit0 Oat is te similarit etEeen a 8tructure, nion and enumeration0 #an a 8tructure contain a Wointer to itself0 IoE can Ee cec? Eeter te contents of tEo structure ariales are same or not0 IoE are 8tructure passin and returnin implemented  te complier0 IoE can Ee readErite 8tructures fromto data files0 Oat is te difference etEeen an enumeration and a set of  pre-processor < defines0 Eat do te HcH and HH in arc and ar stand for0  !re te ariales arc and ar are local to main0 Oat is te ma'imum comined lent of command line aruments includin te space etEeen ad>acent aruments0 $f Ee Eant tat an Eildcard caracters in te command line aruments sould e appropriatel e'panded, are Ee reuired to ma?e an special proision0 $f es, Eic0 Toes tere e'ist an Ea to ma?e te command line aruments aailale to oter functions Eitout passin tem as aruments to te function0

Oat are it fields0 Oat is te use of it fields in a 8tructure declaration0 Go Eic numerin sstem can te inar numer 1131133133111133 e easil conerted to0 Oic it Eise operator is suitale for cec?in Eeter a particular  it is on or off0 Oic it Eise operator is suitale for turnin off a particular it in a numer0 Oic it Eise operator is suitale for puttin on a particular it in a numer0 Oic it Eise operator is suitale for cec?in Eeter a particular  it is on or off0 Eic one is euialent to multiplin  *2eft siftin a numer  1 or 2eft siftin an unsined int or car  10 Orite a proram to compare tEo strins Eitout usin te strcmp() function. Orite a proram to concatenate tEo strins. Orite a proram to intercane  ariales Eitout usin te tird one. Orite prorams for 8trin Reersal & Walindrome cec? Orite a proram to find te Mactorial of a numer  Orite a proram to enerate te Miinocci 8eries Orite a proram Eic emplos Recursion Orite a proram Eic uses #ommand 2ine !ruments Orite a proram Eic uses functions li?e strcmp(), strcp()0 etc Oat are te adantaes of usin tpedef in a proram0 IoE Eould ou dnamicall allocate a one-dimensional and tEo-dimensional arra of inteers0 IoE can ou increase te siAe of a dnamicall allocated arra0

IoE can ou increase te siAe of a staticall allocated arra0 Oen reallocatin memor if an oter pointers point into te same piece of memor do ou ae to read>ust tese oter pointers or do te et read>usted automaticall0 Oic function sould e used to free te memor allocated  calloc()0 IoE muc ma'imum can ou allocate in a sinle call to malloc()0 #an ou dnamicall allocate arras in e'panded memor0 Oat is o>ect file0 IoE can ou access o>ect file0 Oic eader file sould ou include if ou are to deelop a function Eic can accept ariale numer of aruments0 #an ou Erite a function similar to printf()0 IoE can a called function determine te numer of aruments tat ae een passed to it0 #an tere e at least some solution to determine te numer of  aruments passed to a ariale arument list function0 IoE do ou declare te folloEin*  !n arra of tree pointers to cars  !n arra of tree car pointers  ! pointer to arra of tree cars  ! pointer to function Eic receies an int pointer and returns a float pointer   ! pointer to a function Eic receies notin and returns notin Oat do te functions atoi(), itoa() and ct() do0 Toes tere e'ist an oter function Eic can e used to conert an inteer or a float to a strin0 IoE Eould ou use sort() function to sort an arra of structures0

IoE Eould ou use sort() function to sort te name stored in an arra of pointers to strin0 IoE Eould ou use searc() function to searc a name stored in arra of pointers to strin0 IoE Eould ou use te functions sin(), poE(), srt()0 IoE Eould ou use te functions memcp(), memset(), memmoe()0 IoE Eould ou use te functions fsee?(), freed(), fErite() and ftell()0 IoE Eould ou otain te current time and difference etEeen tEo times0 IoE Eould ou use te functions randomiAe() and random()0 IoE Eould ou implement a sustr() function tat e'tracts a su strin from a ien strin0 Oat is te difference etEeen te functions rand(), random(), srand() and randomiAe()0 Oat is te difference etEeen te functions memmoe() and memcp()0 IoE do ou print a strin on te printer0 #an ou use te function fprintf() to displa te output on te screen0 Oat is an o>ect0 Oat is te difference etEeen an o>ect and a class0 Oat is te difference etEeen class and structure0 Oat is pulic, protected, priate0 Oat are irtual functions0 Oat is friend function0 Oat is a scope resolution operator0 Oat do ou mean  ineritance0 Oat is astraction0

Oat is polmorpism0 D'plain Eit an e'ample. Oat is encapsulation0 Oat do ou mean  indin of data and functions0 Oat is function oerloadin and operator oerloadin0 Oat is irtual class and friend class0 Oat do ou mean  inline function0 Oat do ou mean  pulic, priate, protected and friendl0 Oen is an o>ect created and Eat is its lifetime0 Oat do ou mean  multiple ineritance and multileel ineritance0 Tifferentiate etEeen tem. Tifference etEeen realloc() and free0 Oat is a template0 Oat are te main differences etEeen procedure oriented lanuaes and o>ect oriented lanuaes0 Oat is R G G $ 0 Oat are eneric functions and eneric classes0 Oat is namespace0 Oat is te difference etEeen pass  reference and pass  alue0 O do Ee use irtual functions0 Oat do ou mean  pure irtual functions0 Oat are irtual classes0 Toes c++ support multileel and multiple ineritance0 Oat are te adantaes of ineritance0 Oen is a memor allocated to a class0

Oat is te difference etEeen declaration and definition0 Oat is irtual constructorsdestructors0 $n c++ tere is onl irtual destructors, no constructors. O0 Oat is late ound function call and earl ound function call0 Tifferentiate. IoE is e'ception andlin carried out in c++0 Oen Eill a constructor e'ecuted0 Oat is Tnamic Wolmorpism0 Orite a macro for sEappin inteers.

GC FAQ -- draft Common questions What is garbage collection? Garbage collection is a part of a language's runtime system, or an add-on library,  perhaps assisted by the compiler, the hardware, the OS, or any combination of the three, that automatically determines what memory a program is no longer using, and recycles it for other use !t is also "nown as ##automatic storage $or memory% reclamation'' Why is it good? &anual memory management is $programmer-%time consuming, and error prone &ost programs still contain lea"s his is all doubly true with programs using e(ception-handling and)or threads  ! second enefit of arae collection, less oious to people Eo aenHt used it, is tat relin on arae collection to manae memor simplifies te interfaces etEeen components (suroutines, liraries, modules, classes) tat no loner need e'pose memor manaement details ("Eo is responsile for recclin tis memor"). !s garbage collection slow?  *ot necessarily &odern garbage collectors appear to run as uic"ly as manual storage allocators $mallocfree or neEdelete% Garbage collection probably will not run as uic"ly as customied memory allocator de signed for use in a specific

 program On the other hand, the e(tra code reuired to ma"e manual memory management wor" properly $for e(ample, e(plicit reference counting% is often more e(pensie than a garbage collector would be Can ! use garbage collection with C or C++? .robably &odern $well-tested, efficient, non-pausing% garbage collectors are aailable that wor" with all but the most pathological C and C++ programs, including legacy code See GC, C, and C++ for more details /oes garbage collection cause my program's e(ecution to pause?  *ot necessarily 0 ariety of algorithms allow garbage collection to proceed concurrently, incrementally, and $for some definitions of the term% in 1real time1 here are incremental garbage collectors that wor" with C and C++, for instance Where can ! get a C or C++ garbage collector? 2oehm-Weiser collector http3))realitysgicom)employees)boehm4mti)gchtml or ftp3))parcftp(ero(com)pub)gc)gchtml Great Circle from Geodesic Systems 5sales6geodesiccom7 or 899-:;9-8:88 or http3))wwwgeodesiccom) <ein Warne 5warne6directca7 or 899-=9=-=>=>

Folk myths • • • •

GC is necessarily slower than manual memory management GC will necessarily ma"e my program pause &anual memory management won't cause pauses GC is incompatible with C and C++

Folk truths •

• •

• • •

&ost allocated obects are dynamically referenced by a ery small number of  pointers he most important small number is O*@ &ost allocated obects hae short lifetimes 0llocation patterns $sie distributions, lifetime distributions% are bursty, not uniform A& behaior matters Cache behaior matters 1Optimal1 strategies can fail miserably

radeoffs • • • • •

 precise s conseratie moing)compacting s non-moing e(plicit s implicit reclamation phase stopping s incremental s concurrent generational s non-generational

GC, C, and C++ !hat do you mean, gar"age #olle#tion and C$ Rater tan usin malloc and free to otain and reclaim memor, it is possile to lin? in a arae collector and alloE it to reclaim unused memor automaticall. Gis usuall een Eor?s if malloc is replaced Eit te arae collectorHs allocator and free is replaced Eit a do-notin suroutine. Gis approac as Eor?ed Eit te X11 lirar, for instance. $t is also possile to proram in a stle Eere free still reclaims storae, ut te arae collector acts as a ac?stop, preentin lea?s tat mit oterEise occur. Gis stle as also een tested Eit man applications, and it Eor?s Eell. Ge adantae ere is tat Eere it is eas for te prorammer to manae memor, te prorammer manaes te memor, ut Eere it is not, te arae collector does te >o. Gis doesnHt necessaril run an faster tan free-doesnotin, ut it ma elp ?eep te eap smaller.

%o& is this ossi"le$ #-compatile arae collectors ?noE Eere pointers ma enerall e found (e.., "ss", "data", and stac?), and maintain eap data structures tat alloE tem to uic?l determine Eat it patterns mit e pointers. Wointers, of course, loo? li?e pointers, so tis euristic traces out all memor reacale trou pointers. Oat isnHt reaced, is reclaimed.

his doesn't sound (ery orta"le) !hat if * need to ort my #ode and there's no gar"age #olle#tor on the target latform$ 8ome of tis code is necessaril sstem-dependent, ut te features of most operatin sstems ae een enumerated, so arae collection for # is aailale almost eerEere. Gat is, portailit isnHt a prolem if te code as alread een ported, and it as. 8pea?in personall (tis is Taid #ase) itHs also not ard to port tese arae collectors to neE platforms; $He ported te Joem-Oeiser collector tEice mself, Een te code ad not et een ported to terril man platforms, and Een $ ad muc less e'perience Eit te loE-leel interfaces to arious operatin sstems.

!on't this lea(e "ugs in my rogram$ Gis depends on our point of ieE. sin a arae collector soles a lot of prolems for a prorammer, Eic ies a prorammer time to sole oter prolems, or lets te >o e finised faster. $tHs similar in flaor to floatin point aritmetic or irtual memor. Jot of tese sole a tedious prolem (scalin aritmetic, or pain unused data to dis?) tat a prorammer could, in principle, sole. 8ome specialiAed code is Eritten Eitout MW or UV support, ut in

practice, if tese features are aailale, people use tem. GeHre enerall  >uded to e Eell Eort te cost. oE, if a proram is deeloped usin arae collection, and te collector is ta?en aEa, ten es, te result ma contain us in te form of memor lea?s. 8imilarl, if a proram is deeloped usin MW (or UV) and tat is ta?en aEa, tat proram, too, ma contain us.  !lso in practice, man prorams tat use malloc and free alread lea? memor, so use of a arae collector can actuall reduce te numer of us in a proram, and do so muc more uic?l tan if te ad to e trac?ed doEn and fi'ed  and. Gis is especiall true if te memor lea? is inerent in a lirar tat cannot e repaired.

Can't a de(ious C rogrammer "reak the #olle#tor$ #ertainl, ut most people ae etter Eas to spend teir time tan dreamin up Eas to rea? teir tools. Ge collector does rel on ein ale to locate copies of pointers somewhere in an address space, so certain tins EonHt Eor?. Mor instance, te XKRHd pointers tric? for compactl encodin a idirectional list cannot e used -- te pointers donHt loo? li?e pointers. $f a process Erites pointers to a file, and reads tem ac? aain, te memor referenced  tose pointers ma ae een reccled. Vost prorams donHt do tese tins, so most prorams Eor? Eit a arae collector. Krdinar (leal) pointer aritmetic is tolerated  arae collectors for #. *nsert more questions here -- send them to g#listie##)#om

!hat does a gar"age #olle#tor do a"out destru#tors$  ! destructor is some code tat runs Een an o>ect is aout to e freed. Kne of te main uses of destructors is to do manual memor manaement. Mor e'ample, te destructor for an o>ect ma recursiel free te o>ects it references. ! arae collector oiates te need for suc uses* $f an o>ect is arae, all te o>ects it references Eill also e arae if te are not referenced elseEere, and so te, too, Eill e freed automaticall. Gere remains te uestion of Eat to do Eit destructors tat do sometin oter tan assist in memor manaement. Gere are a couple of tpical uses. Kne use is for o>ects tat ae state outside te proram itself. Ge canonical e'ample is an o>ect tat refers to a file. Oen a file o>ect ecomes eliile for reclamation, te arae collector needs to ensure tat uffers are flused, te file is closed, and resources associated Eit te file are returned to te operatin sstem.  !noter use is Eere a proram Eants to ?eep a list of o>ects tat are referenced elseEere. Ge proram ma Eant ?noE Eat o>ects are in e'istence for, sa, accountin purposes ut does not Eant te mecanism of accountin to preent o>ects from oterEise ein freed. Gere are seeral Eas of andlin suc situations*

> !n systems where the garbage collector is 1built in,1 it typically has special "nowledge of all the cases where outside resources can be referenced and can deal with them appropriately B &any GC systems hae a notion of a 1wea" pointer1 0 wea" pointer is one that is not considered as a reference by the garbage collector So if an obect is referenced only by wea" pointers, it is eligible for reclamation Wea" pointers can  be used to implement the obect list e(ample : &any GC systems hae a notion of 1finaliation1 0n obect may be registered with the GC system so that when it is about to reclaim the obect, it runs a function on the obect that can perform necessary cleanups inaliation is fundamentally tric"y Some of the issues are3 > When does a finaliation function run, particularly with respect to when other finaliers run?D B What happens when registered obects reference each other?D : What happens if a finaliation function ma"es an obect not be garbage any more? here are no pat answers to these uestions

Smart Pointers - !hat, !hy, !hi#h$ Yonat Sharon

!hat are they$ 8mart pointers are o>ects tat loo? and feel li?e pointers, ut are smarter. Oat does tis mean0 Go loo? and feel li?e pointers, smart pointers need to ae te same interface tat pointers do* te need to support pointer operations li?e dereferencin (operator ) and indirection (operator -F). !n o>ect tat loo?s and feels li?e sometin else is called a pro' o>ect, or >ust pro'. Ge pro' pattern and its man uses are descried in te oo?s Tesin Watterns and Wattern Kriented 8oftEare !rcitecture. Go e smarter tan reular pointers, smart pointers need to do tins tat reular pointers donHt. Oat could tese tins e0 Wroal te most common us in #++ (and #) are related to pointers and memor manaement* danlin pointers, memor lea?s, allocation failures and oter >os. Iain a smart pointer ta?e care of tese tins can sae a lot of aspirin... Ge simplest e'ample of a smart pointer is autoYptr, Eic is included in te standard #++ lirar. Nou can find it in te eader :memorF, or ta?e a loo? at 8cott VeersH autoYptr implementation . Iere is part of autoYptrHs implementation, to illustrate Eat it does* template :class GF class auto.tr  { G ptr;

pulic* e'plicit auto.tr (G p 6 3) * ptr(p) {} /auto.tr () {delete ptr;} G& oerator0() {return ptr;} G oerator-() {return ptr;}  ... };

 !s ou can see, autoYptr is a simple Erapper around a reular pointer. $t forEards all meaninful operations to tis pointer (dereferencin and indirection). $ts smartness in te destructor* te destructor ta?es care of deletin te pointer. Mor te user of autoYptr, tis means tat instead of Eritin* oid foo() { 1yClass p(neE 1yClass); p-F2oSomething(); delete p; }

Nou can Erite* oid foo() { auto.tr :1yClassF p(neE 1yClass); p-F2oSomething(); }

 !nd trust p to cleanup after itself. Oat does tis u ou0 8ee te ne't section.

!hy &ould * use them$ Kiousl, different smart pointers offer different reasons for use. Iere are some common reasons for usin smart pointers in #++.

!hy3 Less bugs Automati# #leanu) !s te code aoe illustrates, usin smart pointers tat clean after temseles can sae a feE lines of code. Ge importance ere is not so muc in te ?estro?es saed, ut in reducin te proailit for us* ou donHt need to rememer to free te pointer, and so tere is no cance ou Eill foret aout it. Automati# initiali4ation)  !noter nice tin is tat ou donHt need to initialiAe te autoYptr to 22, since te default constructor does tat for ou. Gis is one less tin for te prorammer to foret. 2angling ointers) ! common pitfall of reular pointers is te danlin pointer* a pointer tat points to an o>ect tat is alread deleted. Ge folloEin code illustrates tis situation* 1yClass p(neE 1yClass); 1yClass  6 p; delete p; p-F2oSomething();  Oatc out p is noE danlin p 6 22;  p is no loner danlin -F2oSomething();  Kuc  is still danlin

Mor autoYptr, tis is soled  settin its pointer to 22 Een it is copied* template :class GF auto.tr :GF& auto.tr :GF**oerator5(auto.tr :GF& rs) { if (tis 6 &rs) { delete ptr; ptr 6 rs.ptr; rs.ptr 6 22; } return tis; }

Kter smart pointers ma do oter tins Een te are copied. Iere are some possile strateies for andlin te statement  6 p, Eere p and  are smart pointers* Create a new copy  of the obect pointed by p, and hae  point to this copy his strategy is implemented in copied4ptrh Ownership transfer 3 Eet both p and  point to the same obect, but transfer the responsibility for cleaning up $1ownership1% from p to  his strategy is implemented in owned4ptrh Reference counting 3 &aintain a count of the smart pointers that point to the same obect, and delete the obect when this count becomes ero So the statement  F p causes the count of the obect pointed by p to increase by one his strategy is implemented in counted4ptrh Scott &eyers offers another reference counting implementation in his boo" &ore @ffectie C++ Reference linking 3 he same as reference counting, only instead of a count, maintain a circular doubly lin"ed list of all smart pointers that point to the same obect his strategy is implemented in lin"ed4ptrh Copy on write 3 se reference counting or lin"ing as long as the pointed obect is not modified When it is about to be modified, copy it and modify the copy his strategy is implemented in cow4ptrh •









 !ll tese tecniues elp in te attle aainst danlin pointers. Dac as eac oEn enefits and liailities. Ge Which section of tis article discusses te suitailit of different smart pointers for arious situations.

!hy3 Exception Safety  2etHs ta?e anoter loo? at tis simple e'ample* oid foo() { 1yClass p(neE 1yClass); p-F2oSomething(); delete p; }

Oat appens if To8ometin() troEs an e'ception0 !ll te lines after it Eill not et e'ecuted and p Eill neer et deleted $f EeHre luc?, tis leads onl to memor lea?s. IoEeer, V#lass ma free some oter resources in its destructor (file andles, treads, transactions, #KV references, mute'es) and so not callin it m cause seere resource loc?s.

$f Ee use a smart pointer, oEeer, p Eill e cleaned up Eeneer it ets out of scope, Eeter it Eas durin te normal pat of e'ecution or durin te stac? unEindin caused  troEin an e'ception. Jut isnHt it possile to Erite e'ception safe code Eit reular pointers0 8ure, ut it is so painful tat $ dout anone actuall does tis Een tere is an alternatie. Iere is Eat ou Eould do in tis simple case* oid foo() { 1yClass p; tr { p 6 neE 1yClass; p-F2oSomething(); delete p; } catc (...) { delete p; troE; } }

oE imaine Eat Eould appen if Ee ad some ifHs and forHs in tere...

!hy3 Garbage collection 8ince #++ does not proide automatic arae collection li?e some oter lanuaes, smart pointers can e used for tat purpose. Ge simplest arae collection sceme is reference countin or reference lin?in, ut it is uite possile to implement more sopisticated arae collection scemes Eit smart pointers. Mor more information see te arae collection M!Q .

!hy3 Efficiency  8mart pointers can e used to ma?e more efficient use of aailale memor and to sorten allocation and deallocation time.  ! common strate for usin memor more efficientl is cop on Erite (#KO). Gis means tat te same o>ect is sared  man #KO pointers as lon as it is onl read and not modified. Oen some part of te proram tries to modif te o>ect ("Erite"), te #KO pointer creates a neE cop of te o>ect and modifies tis cop instead of te oriinal o>ect. Ge standard strin class is commonl implemented usin #KO semantics (see te :strinF eader). string s("Iello"); string t 6 s;

 t and s point to te same uffer of caracters

t +6 " tere";  a neE uffer is allocated for t efore  appendin " tere", so s is uncaned.

KptimiAed allocation scemes are possile Een ou can ma?e some assumptions aout te o>ects to e allocated or te operatin enironment. Mor e'ample, ou ma ?noE tat all te o>ects Eill ae te same siAe, or tat te Eill all lie in a sinle tread. !ltou it is possile to implement optimiAed

allocation scemes usin class-specific neE and delete operators, smart pointers ie ou te freedom to coose Eeter to use te optimiAed sceme for eac o>ect, instead of ain te sceme set for all o>ects of a class. $t is terefore possile to matc te allocation sceme to different operatin enironments and applications, Eitout modifin te code for te entire class.

!hy3 STL containers Ge #++ standard lirar includes a set of containers and aloritms ?noEn as te standard template lirar (8G2). 8G2 is desined to e generic  (can e used Eit an ?ind of o>ect) and efficient  (does not incur time oeread compared to alternaties). Go aciee tese tEo desin oals, 8G2 containers store teir o>ects  alue. Gis means tat if ou ae an 8G2 container tat stores o>ects of class Jase, it cannot store of o>ects of classes deried from Jase. class 6ase { ... }; class 2eri(ed * pulic 6ase { ... }; 6ase ; 2eri(ed d; (e#tor :6aseF ; .ush."a#k();  KL .ush."a#k(d);  error 

Oat can ou do if ou need a collection of o>ects from different classes0 Ge simplest solution is to ae a collection of pointers* (e#tor :6aseF ; .ush."a#k(neE 6ase);  KL .ush."a#k(neE 2eri(ed);  KL too  cleanup* for  ((e#tor :6aseF**iterator  i 6 ."egin(); i 6 .end(); ++i) delete i;

Ge prolem Eit tis solution is tat after ouHre done Eit te container, ou need to manuall cleanup te o>ects stored in it. Gis is ot error prone and not e'ception safe. 8mart pointers are a possile solution, as illustrated eloE. (!n alternatie solution is a smart container, li?e te one implemented in pointainer..) (e#tor : linked.tr :6aseF F ; .ush."a#k(neE 6ase);  KL .ush."a#k(neE 2eri(ed);  KL too  cleanup is automatic

8ince te smart pointer automaticall cleans up after itself, tere is no need to manuall delete te pointed o>ects. ote* 8G2 containers ma cop and delete teir elements eind te scenes (for e'ample, Een te resiAe temseles). Gerefore, all copies of an element must e euialent, or te Eron cop ma e te one to surie all tis copin and deletin. Gis means tat some smart pointers cannot e used Eitin 8G2

containers, specificall te standard autoYptr and an oEnersip-transferrin pointer. Mor more info aout tis issue, see #++ 4uru of te Oee? <.

!hi#h one should * use$  !re ou confused enou0 Oell, tis summar sould elp.

!hi#h3 Local variables Ge standard autoYptr is te simplest smart pointer, and it is also, Eell, standard. $f tere are no special reuirements, ou sould use it. Mor local ariales, it is usuall te rit coice.

!hi#h3 Class members  !ltou ou can use autoYptr as a class memer (and sae ourself te troule of freein o>ects in te destructor), copin one o>ect to anoter Eill nullif te pointer, as illustrated JeloE. class 1yClass { auto.tr :intF p;  ... }; 1yClass ';  do some meaninful tins Eit ' 1yClass  6 ';  '.p noE as a 22 pointer 

sin a copied pointer instead of autoYptr soles tis prolem* te copied o>ect () ets a neE cop of te memer. ote tat usin a reference counted or reference lin?ed pointer means tat if  canes te memer, tis cane Eill also affect ' Gerefore, if ou Eant to sae memor, ou sould use a #KO pointer and not a simple reference countedlin?ed pointer.

!hi#h3 STL containers  !s e'plained aoe, usin arae-collected pointers Eit 8G2 containers lets ou store o>ects from different classes in te same container. $t is important to consider te caracteristics of te specific arae collection sceme used. 8pecificall, reference countinlin?in can lea? in te case of circular references (i.e., Een te pointed o>ect itself contains a counted pointer, Eic points to an o>ect tat contains te oriinal counted pointer). $ts adantae oer oter scemes is tat it is ot simple to implement and deterministic. Ge deterministic eaior ma e important in some real time sstems, Eere ou cannot alloE te sstem to suddenl Eait Eile te arae collector performs its ouse?eepin duties.

4enerall spea?in, tere are tEo Eas to implement reference countin* intrusie and non-intrusie. $ntrusie means tat te pointed o>ect itself contains te count. Gerefore, ou cannot use intrusie reference countin Eit 5-rd part classes tat do not alread ae tis feature. Nou can, oEeer, derie a neE class from te 5-rd part class and add te count to it. on-intrusie reference countin reuires an allocation of a count for eac counted o>ect. Ge countedYptr. is an e'ample of non-intrusie reference countin.

Heference lin"ing does not reuire any changes to be made to the pointed obects, nor does it reuire any additional allocations 0 reference lin"ed pointer ta"es a little more space than a reference counted pointer - ust enough to store one or two more pointers

Jot reference countin and reference lin?in reuire usin loc?s if te pointers are used  more tan one tread of e'ecution.

!hi#h3 Explicit ownership transfer  8ometimes, ou Eant to receie a pointer as a function arument, ut ?eep te oEnersip of tis pointer (i.e. te control oer its lifetime) to ourself. Kne Ea to do tis is to use consistent namin-conentions for suc cases. GalientHs 4uide to Tesinin Wrorams recommends usin "adopt" to mar? tat a function adopts oEnersip of a pointer. sin an oEned pointer as te function arument is an e'plicit statement tat te function is ta?in oEnersip of te pointer.

!hi#h3 Big objects $f ou ae o>ects tat ta?e a lot of space, ou can sae some of tis space  usin #KO pointers. Gis Ea, an o>ect Eill e copied onl Een necessar,

and sared oterEise. Ge sarin is implemented usin some arae collection sceme, li?e reference countin or lin?in.

!hi#h3 Summary  For this:

Use that:

 Local variables

auto4ptr 

Class members

Copied pointer 

STL Containers

Garbage collected pointer $eg reference counting)lin"ing%

 Explicit ownership transfer  Owned pointer   Big objects

Copy on write

Con#lusion 8mart pointers are useful tools for Eritin safe and efficient code in #++. 2i?e an tool, te sould e used Eit appropriate care, tout and ?noElede. Mor a compreensie and in dept analsis of te issues concernin smart pointers, $ recommend readin !ndrei !le'andrescuHs capter aout smart pointers  in is oo? Vodern #++ Tesin. Meel free to use m oEn smart pointers in our code, and do tell me if ou are ain an prolems Eit tem. Ge Joost #++ liraries include some smart pointers, Eic are more riorousl tested and actiel maintained. To tr tem first, if te are appropriate for our needs. Copyright 1999 by Yonat Sharon http3))ootipsorg)yonat)Ide)smart-pointershtml

This article was first published in #++ report in two parts, in the July/August 199 and !ecember 199 issues"

1emory 1anagement in C++  by Nathan C. Myers

#emory usage in $%% is as the sea come to land& a tide rolls in, and sweeps out again, lea'ing only puddles and stranded fish"

 At inter'als, a wa'e crashes ashore( but the ripples never cease" *ntrodu#tion Van prorams ae little need for memor manaement; te use a fi'ed amount of memor, or simpl consume it until te e'it. Ge est tat can e done for suc prorams is to sta out of teir Ea. Kter prorams, includin most #++ prorams, are muc less deterministic, and teir performance can e profoundl affected  te memor manaement polic te run under. nfortunatel, te memor manaement facilities proided  man sstem endors ae failed to ?eep pace Eit roEt in proram siAe and dnamic memor usae. Jecause #++ code is naturall oraniAed  class, a common response to tis failure is to oerload memer operator neE for indiidual classes. $n addition to ein tedious to implement and maintain, oEeer, tis piece-meal approac can actuall urt performance in lare sstems. Mor e'ample, applied to a tree-node class, it forces nodes of eac tree to sare paes Eit nodes of oter (proal unrelated) trees, rater tan Eit related data. Murtermore, it tends to frament memor  ?eepin lare, mostl empt loc?s dedicated to eac class. Ge result can e a uic? neEdelete ccle tat accidentall causes irtual memor trasin. !t est, te approac interferes Eit sstem-Eide tunin efforts. Gus, Eile detailed ?noElede of te memor usae patterns of indiidual classes can e elpful, it is est applied  tunin memor usae for a Eole proram or ma>or susstem. Ge first alf of tis article descries an interface Eic can ease suc tunin in #++ prorams. Jefore tunin a particular proram, oEeer, it pas to improe performance for all prorams,  improin te loal memor manaer. Ge second alf of tis article coers te desin of a loal memor manaer tat is as fast and space-efficient as per-class allocators. Jut raE speed and efficienc are onl a einnin. ! memor manaement lirar Eritten in #++ can e an oraniAational tool in its oEn rit. Den as Ee confront te traditional prolems inolin lare data structures, proress in operatin sstems is ieldin different )inds of memor -- sared memor, memor-mapped files, persistent storae -- Eic must e manaed as Eell. Oit a common interface to all tpes of memor, most classes need not ?noE te difference. Gis ma?es uite a contrast Eit sstems of classes ard-Eired to use onl reular memor. Glo"al Oerator 7e& $n #++, te onl Ea to oraniAe memor manaement on a larer scale tan te class is  oerloadin te loal operator neE. Go select a memor manaement polic reuires addin a  placement argument , in tis case a reference to a class Eic implements te polic* e'tern oid operator neE(siAeYt, class Ieap&);

Oen Ee oerload te operator neE in tis Ea, Ee reconiAe tat te reular operator neE is implementin a polic of its oEn, and Ee Eould li?e to tune it as Eell. Gat is, it ma?es sense to offer te same coices for te reular operator neE as for te placement ersion.

$n fact, one cannot proide an interestin placement operator neE Eitout also replacin te reular operator neE. Ge loal operator delete can ta?e no user parameters, so it must e ale to tell Eat to do >ust  loo?in at te memor ein freed. Gis means tat te operator delete and all operators neE must aree on a memor manaement arcitecture. Mor e'ample, if our loal operators neE Eere to e uilt on top of malloc(), Ee Eould need to store e'tra data in eac loc? so tat te loal operator delete Eould ?noE Eat to do Eit it. !ddin a Eord of oeread for eac o>ect to malloc()Hs oEn oeread (a total of 1= tes, on most R$8#s), Eould seem a craA Ea to improe memor manaement. Mortunatel, all tis space oeread can e eliminated  passin malloc(), as Eill e seen later. Ge need to replace te loal operators neE and delete Een addin a placement operator neE as profound effects on memor manaement sstem desin. $t means tat it is impossile to interate different memor manaement arcitectures. Gerefore, te top-leel memor manaement arcitecture must e totall eneral, so tat it can support an polic Ee mit Eant to appl. Gotal eneralit, in turn, reuires asolute simplicit. An *nterfa#e IoE simple can Ee et0 2et us consider some declarations. Ieap is an astract class* class Ieap {   protected* irtual SIeap();   pulic* irtual oid allocate(siAeYt) 6 3; static Ieap& EatIeap(oid); };

(Ge static memer function EatIeap(oid) is discussed later.) IeapHs astract interface is simple enou. 4ien a loal Ieap pointer, te reular loal operator neE can use it* e'tern Ieap YYloalYeap; inline oid operator neE(siAeYt sA) { return **YYloalYeap-Fallocate(sA); }

$nline dispatcin ma?es it fast. $tHs eneral too; Ee can use te Ieap interface to implement te placement operator neE, proidin access to an priate eap* inline oid operator neE(siAeYt siAe, Ieap& eap { return eap.allocate(siAe); }

Oat ?ind of implementations mit Ee define for te Ieap interface0 Kf course te first must e a eneral purpose memor allocator, class Ieap!n. (Ieap!n is te memor manaer descried in detail in te second alf of tis article.) Ge loal eap pointer, used  te reular operator neE defined aoe, is initialiAed to refer to an instance of class Ieap!n* e'tern class Ieap!n YYGIDYloalYeap; Ieap YYloalYeap 6 &YYGIDYloalYeap;

sers, too, can instantiate class Ieap!n to ma?e a priate eap* Ieap!n& meap 6 neE Ieap!n;

and allocate storae from it, usin te placement operator neE* VGpe mine 6 neE(meap) VGpe;

 !s promised, deletion is te same as alEas* delete mine;

oE Ee ae te asis for a memor manaement arcitecture. $t seems tat all Ee need to do is proide an appropriate implementation of class Ieap for an polic Ee mit Eant. !s usual, life is not so simple. Comli#ations Oat appens if VGpeHs constructor itself needs to allocate memor0 Gat memor sould come from te same eap, too. Oe could pass a eap reference to te constructor* mine 6 neE(meap) VGpe(meap);

and store it in te o>ect for use later, if needed. IoEeer, in practice tis approac leads to a massie proliferation of Ieap& aruments -- in constructors, in functions tat call constructors, eerEere -- Eic penetrates from te top of te sstem (Eere te eaps are manaed) to te ottom (Eere te are used). ltimatel, almost eer function needs a Ieap& arument. !pplied earnestl, te result can e orrendous. Den at est, suc an approac ma?es it difficult to interate oter liraries into a sstem. Kne Ea to reduce te proliferation of Ieap aruments is to proide a function to call to discoer Eat eap an o>ect is on. Gat is te purpose of te te Ieap**EatIeap() static memer function. Mor e'ample, ereHs a VGpe memer function tat allocates some uffer storae* car VGpe**ma?eYuffer() { Ieap& aIeap 6 Ieap**EatIeap(tis); return neE(aIeap) carJM8$Z/; }

($f "tis" points into te stac? or static space, EatIeap() returns a reference to te default loal eap.)  !noter Ea to reduce Ieap arument proliferation is to sustitute a priate eap to e used  te loal operator neE. 8uc a loal resource calls for inerl andlin. #lass Ieap8tac?GopHs constructor replaces te default eap Eit its arument, ut retains te old default so it can e restored  te destructor* class Ieap8tac?Gop { Ieap oldY;   pulic* Ieap8tac?Gop(Ieap& );   SIeap8tac?Gop(); };

Oe mit use tis as folloEs* { Ieap8tac?Gop top 6 meap; mine 6 neE VGpe; }

oE space for te VGpe o>ect, and an secondar store allocated  its constructor, comes from meap. !t te closin rac?et, te destructor SIeap8tac?Gop() restores te preious default loal eap. $f one of VGpeHs memer functions mit later Eant to allocate more space from te same eap, it

can use EatIeap(); or te constructor can sae a pointer to te current loal eap efore returnin. #reatin a Ieap8tac?Gop o>ect is er clean Ea to install an loal memor manaement mecanism* a Ieap8tac?Gop o>ect created in main() uietl slips a neE memor allocator under te Eole proram. 8ome classes must allocate storae from te top-leel loal eap reardless of te current default. !n o>ect can force itself to e allocated tere  definin a memer operator neE, and can control Eere its secondar storae comes from  te same tecniues descried aoe. Oit Ieap8tac?Gop, man classes need not ?noE aout Ieap at all; tis can ma?e a i difference Een interatin liraries from arious sources. Kn te oter and, te meanin of Ieap**EatIeap() (or a Ieap& memer or arument) is easier to rasp; it is clearer, and terefore safer. Oile neiter approac is Eoll satisfactor, a careful mi' of te tEo can reduce te proliferation of Ieap& aruments to a reasonale leel. 8ses for Pri(ate %eas Jut Eat can priate eaps do for us0 Oe ae inted tat improed localit of reference leads to etter performance in a irtual memor enironment, and tat a uniform interface elps Een usin special tpes of memor. Kne oious use for priate eaps is as a sort of poor manHs arae collection* Ieap meap 6 neE IeapGras; ...  lots of calls to neE(meap) delete meap;

$nstead of deletin o>ects, Ee discard te Eole data structure at one troE. Ge approac is sometimes called "lifetime manaement". 8ince te destructors are neer called, ou must carefull control Eat ?ind of o>ects are put in te eap; it Eould e aAardous eer to install suc a eap as te default (Eit Ieap8tac?Gop) ecause man classes, includin iostream, allocate space at unpredictale times. Tanlin pointers to o>ects in te deleted eap must e preented, Eic can e tric? if an o>ects secretl sare storae amon temseles. K>ects Eose destructors do more tan >ust delete oter o>ects reuire special andlin; te eap ma need to maintain a reistr of o>ects tat reuire "finaliAation". Jut priate eaps ae man oter uses tat donHt iolate #++ lanuae semantics. Weraps te uietest one is simpl to et etter performance tan our endorHs malloc() offers. $n man lare sstems, memer operator neE is defined for man classes >ust so te ma call te loal operator neE less often. Oen te loal operator neE is fast enou, suc code can e deleted, ieldin easier maintenance, often Eit a net ain in performance from etter localit and reduced framentation.  !n idea tat stri?es man people is tat a priate eap could e Eritten tat is optimiAed to Eor? Eell Eit a particular aloritm. Jecause it need not field reuests from te rest of te proram, it can concentrate on te needs of tat aloritm. Ge simplest e'ample is a eap tat allocates o>ects of onl one siAe; as Ee Eill see later, oEeer, te default eap can e made fast enou tat tis is no reat adantae. ! mar?release mecanism is optimal in some conte'ts (suc as parsin), if it can e used for onl part of te associated data structure.

Oen sared memor is used for interprocess communication, it is usuall allocated  te operatin sstem in loc?s larer tan te o>ects tat ou Eant to sare. Mor tis case a eap tat manaes a sared memor reion can offer te same enefits tat reular operator neE does for priate memor. $f te interface is te same as for non-sared memor, o>ects ma not need to ?noE te are in sared memor. 8imilarl, if ou are constrained to implement our sstem on an arcitecture Eit a tin address space, ou ma need to sEap memor sements in and out. $f a priate eap ?noEs oE to andle tese sements, o>ects tat donHt een ?noE aout sEappin can e allocated in tem. $n eneral, Eeneer a cun? of memor is to e cared up and made into arious o>ects, a Ieap-li?e interface is called for. $f tat interface is te same for te Eole sstem, ten oter o>ects need not ?noE Eere te cun? came from.  !s a result, o>ects Eritten Eitout te particular use in mind ma safel e instantiated in er peculiar places. $n a multi-treaded proram, te loal operator neE must carefull e'clude oter treads Eile it operates on its data structures. Ge time spent >ust ettin and releasin te loc? can itself ecome a ottlenec? in some sstems. $f eac tread is ien a priate eap Eic maintains a cace of memor aailale Eitout loc?in, te treads need not sncroniAe e'cept Een te cace ecomes empt (or too full). Kf course, te operator delete must e ale to accept loc?s allocated  an tread, ut it need not sncroniAe if te loc? ein freed came from te eap oEned  te tread tat is releasin it.  ! eap tat rememers details aout oE, or Een, o>ects in it Eere created can e er useful Een implementin an o>ect- oriented dataase or remote procedure call mecanism. ! eap tat sereates small o>ects  tpe can alloE tem to simulate irtual function eaior Eitout te oeread of a irtual function tale pointer in eac o>ect. ! eap tat Aero-fills loc?s on allocation can simplif constructors. Wrorams can e instrumented to collect statistics aout memor usae (or lea?ae)  sustitutin a specialiAed eap at arious places in a proram. se of priate eaps alloEs muc finer ranularit tan te traditional approac of sadoEin malloc() at lin? time. $n te remainder of tis article Ee Eill e'plore oE to implement Ieap!n efficientl, so tat malloc(), te loal operator neE(siAeYt), te loal operator neE(siAeYt, Ieap&), and Ieap**EatIeap(oid) can e uilt on it. A 1emory 1anager in C++  !n optimal memor manaer as minimal oeread* space used is ut fractionall larer tan te total reuested, and te neEdelete ccle time is small and constant. Van factors Eor? aainst aciein tis optimum. $n man endor liraries, memor used  te memor manaer itself, for oo??eepin, can doule te total space used. Mramentation, Eere loc?s are free ut unaailale, can also multipl te space used. 8pace matters, een toda, ecause irtual memor pae faults sloE doEn our proram (indeed, our entire computer), and sEap space limits can e e'ceeded >ust as can real memor.

 ! memor manaer can also Easte time in man Eas. Kn allocation, a loc? of te rit siAe must e found or made. $f made, te remainder of te split loc? must e placed Eere it can e found. Kn deallocation, te freed loc? ma need to e coalesced Eit an neiorin loc?s, and te result must e placed Eere it can e found aain. 8stem calls to otain raE memor can ta?e loner tan an oter sinle operation; a pae fault tat results Een idle memor is touced is >ust a idden sstem call. !ll tese operations ta?e time, time spent not computin results. Ge effects of Easteful memor manaement can e ard to see. Gime spent trasin te sEap file doesnHt soE up on profiler output, and is ard to attriute to te responsile code. Kften te prolem is easil isile onl Een memor usae e'ceeds aailale sEap space. Va?e no mista?e* poor memor manaement can multipl our proramHs runnin time, or so o doEn a macine tat little else can run. Jefore uin (or ma?in our customers u) more memor, it ma?es sense to see Eat can e done Eit a little code. Prin#iles  ! memor manaer pro>ect is an opportunit to appl principles of ood desin* 8eparate te common case from special cases, and ma?e te common case fast and ceap, and oter cases tolerale; ma?e te user of a feature ear te cost of its use; use ints; reuse ood ideas. 2ampson/ Jefore delin into detailed desin, Ee must e clear aout our oals. Oe Eant a memor manaer tat satisfies te folloEin* J Speed3 !t must be much faster than e(isting memory managers, especially for small obects .erformance should not suffer under common usage patterns, such as repeatedly allocating and freeing the same bloc" J Eow oerhead3 he total sie of headers and other wasted space must be a small percentage of total space used, een when all obects are tiny Hepeated allocation and deallocation of different sies must not cause memory usage to grow without  bound J Small wor"ing set3 he number of pages touched by the memory manager in satisfying a reuest must be minimal, to aoid paging delays in irtual memory systems nused memory must be returned to the operating system periodically J Hobustness3 @rroneous programs must hae difficulty corrupting the memory manager's data structures @rrors must be flagged as soon as possible, not allowed to acc umulate Out-of-memory eents must be handled gracefully J .ortability3 he memory manager must adapt easily to different machines J Conenience3 sers mustn't need to change code to use it

J le(ibility3 !t must be easily customied for unusual needs, without imposing any additional oerhead e#hniques Kptimal memor manaers Eould e common if te Eere easil uilt. Ge are scarce, so ou can e'pect tat a ariet of sutle tecniues are needed een to approac te optimum. Kne suc tecniue is to treat different reuest siAes differentl. $n most prorams, small loc?s are reuested oerEelminl more often tan lare loc?s, so ot time and space oeread for tem is felt disproportionatel.  !noter tecniue results from notin tat tere are onl a feE different siAes possile for er small loc?s, so tat eac suc siAe ma e andled separatel. Oe can een afford to ?eep a ector of free loc? lists for tose feE siAes.  ! tird is to aoid sstem call oeread  reuestin memor from te operatin sstem in i cun?s, and  not toucin unused (and possil paed-out) loc?s unnecessaril. Gis means data structures consulted to find a loc? to allocate sould e stored compactl, apart from te unused loc?s te descrie. Ge final, and most important, tecniue is to e'ploit address aritmetic Eic, Eile not strictl portale accordin to lanuae standards, Eor?s Eell on all modern flat-memor arcitectures. ! pointer alue can e treated as an inteer, and itEise loical operations ma e used on it to ield a neE pointer alue. $n particular, te loE its ma e mas?ed off to ield a pointer to a eader structure tat descries te loc? pointed to. $n tis Ea a loc? need not contain a pointer to tat information. Murtermore, man loc?s can sare te same eader, amortiAin its oeread across all. (Gis tecniue is familiar in te 2$8W communit, Eere it is ?noEn as "pae-tain".) Oit so man oals, principles, and tecniues to ?eep trac? of, it sould e no surprise tat tere are plent of pitfalls to aoid. Ge Eill e discussed later. A 2esign Ge first ma>or feature of te desin is suested  te final tEo tecniues aoe. Oe reuest memor from te operatin sstem in units of a lare poEer of tEo (e.. =9L tes) in siAe, and place tem so te are alined on suc a oundar. Oe call tese units "sements". !n address Eitin te sement ma ae its loE its mas?ed off, ieldin a pointer to te sement eader. Oe can treat tis eader as an instance of te astract class Ieap8ement* class Ieap8ement {   pulic* irtual oid free(oid) 6 3; irtual oid realloc(oid) 6 3; irtual Ieap& oEnedY(oid) 6 3; };

Ge second ma>or feature of te desin ta?es adantae of te small numer of small-loc? siAes possile. ! sement (Eit a eader of class IeapWaese) is split up into paes, Eere eac pae contains loc?s of onl one siAe. ! ector of free lists, Eit one element for eac siAe, alloEs instant access to a free loc? of te rit siAe. Teallocation is >ust as uic?; no coalescin is needed. Dac pae

as >ust one eader to record te siAe of te loc?s it contains, and te oEnin eap. Ge pae eader is found  address aritmetic, >ust li?e te sement eader. $n tis Ea, space oeread is limited to a feE percent, een for te smallest loc?s, and te time to allocate and deallocate te pae is amortiAed oer all usae of te loc?s in te pae. Mor larer loc?s, tere are too man siAes to ie eac a sement; ut suc loc?s ma e pac?ed ad>acent to one anoter Eitin a sement, to e coalesced Eit neiorin free loc?s Een freed. (Oe Eill call suc loc?s "spans", Eit a sement eader of tpe Ieap8panse.) Mramentation, te proliferation of free loc?s too small to use, is te cief daner in span sements, and tere are seeral Eas to limit it. Jecause te common case, small loc?s, is andled separatel, Ee ae some reatin room* spans ma ae a lare ranularit, and Ee can afford to spend more time manain tem. ! alanced tree of aailale siAes is fast enou tat Ee can use seeral searces to aoid creatin tin unusale spans. Ge tree can e stored compactl, apart from te free spans, to aoid toucin tem until te are actuall used. Minall, aressie coalescin elps reclaim small loc?s and ?eep lare loc?s aailale. Jloc?s too i to fit in a sement are allocated as a contiuous seuence of sements; te eader of te first sement in te seuence is of class IeapIuese. Vemor Easted in te last sement is muc less tan mit e feared; an paes not touced are not een assined  te operatin sstem, so te aerae Easte for ue loc?s is onl alf a irtual-memor pae. Tispatcin for deallocation is simple and uic?* oid operator delete(oid ptr) { lon eader 6 (lon)ptr & V!8L;   ((Ieap8ement)eader)-Ffree(ptr); }

Ieap8ement**free() is a irtual function, so eac sement tpe andles deallocation its oEn Ea. Gis alloEs different Ieaps to coe'ist. $f te freed pointer does not point to allocated memor, te proram Eill most li?el cras immediatel. (Gis is a feature. Jus tat are alloEed to accumulate are e'tremel difficult to trac? doEn.) Ge classical # memor manaement functions, malloc(), calloc(), realloc(), and free() can e implemented on top of Ieap!n >ust as Eas te loal operator neE. Knl realloc() reuires particular support. Ge onl remainin feature to implement is te function Ieap**EatIeap(oid ptr). Oe cannot assume tat ptr refers to eap storae; it ma point into te stac?, or static storae, or elseEere. Ge solution is to ?eep a itmap of allocated sements, one it per sement. Kn most arcitectures tis ta?es L Eords to coer te entire address space. $f te pointer refers to a manaed sement, Ieap8ement**oEnedY() reports te oEnin eap; if not, a reference to te default loal eap ma e returned instead. ($n te 2$8W communit, tis tecniue is referred to as JJKW, or "i a oH paes".) Pitfalls

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close