Cèšèªã®æ¬ãã«ã¯ãããŠã
mallocããã¡ã¢ãªãŒããã䜿ããªããªãæŸã£ãŠããã°
çµäºæã«èªåçã«è§£æŸãããã®ã§æ瀺çã«freeããªããŠãè¯ã
ãšæžããŠããã®ã§ãããå
æ¥ããéèªãèŠããš
æ瀺çã«freeããªããšã¡ã¢ãªãŒãªãŒã¯ã«ãªã
ãšæžãããŠããŸãããã©ã¡ããæ£ããã®ã§ããããã
Arita
ããã¯èšèªä»æ§ã®åé¡ã§ã¯ãªããŠOSã®åé¡ã§ãããŸãšã
ãªOSãªããmalloc()ããåŒã°ããã·ã¹ãã ã³ãŒã«ã§ç¢ºä¿
ãããã¡ã¢ãªãŒã¯ããã°ã©ã ã®çµäºæã«ã¯è§£æŸãããã¯
ãã§ãããªãããšãããšãOSããã®ããã«äœã£ãŠãããª
ããšãããããªåé¡ãèµ·ããŠå°ãããã§ãããããã£ãŠã
éœã«free()ããå¿
èŠã¯ãªãããšããã®ãäžè¬è§£ã§ãã
ãããéèªãã«æžãããŠããã®ã¯ç¹å®ã®OSã®ãã°ã«ã€ã
ãŠã®èšè¿°ã§ã¯ãªãã§ãããã?
--
倪ç°çŽ(Junn Ohta) (æ ª)ãªã³ãŒ 販äºæ¬Sèšç»C NWSèšç»å®€SéG
oh...@src.ricoh.co.jp/JCF0...@nifty.ne.jp
bar...@geocities.co.jpãã:
> mallocã§ç¢ºä¿ããã¡ã¢ãªãŒãfreeããªããšãããªããã©ããã«ã€ããŠ
> 質åãããŠãã ããã
freeããªããšãããŸããã
> Cèšèªã®æ¬ãã«ã¯ãããŠãmallocããã¡ã¢ãªãŒããã䜿ããªããªãæŸã£
> ãŠããã°çµäºæã«èªåçã«è§£æŸãããã®ã§æ瀺çã«freeããªããŠãè¯
> ããšæžããŠããã®ã§ããã
ããè¯ããã°ããããã®æ¬ã®æžåãæããŠé ããŸãããã ä¹ é
P.S. çµäºæã«è§£æŸããããã©ããã¯OSã®æ©èœæ¬¡ç¬¬ã§ãããé·æéåã
ç¶ãããœãããæžããšããããªããšãããããããããšã«ãªãã®
ã§ãå¿
ã解æŸããç¿æ
£ãã€ããã¹ãã§ãã
oh...@src.ricoh.co.jpãã
> ããã¯èšèªä»æ§ã®åé¡ã§ã¯ãªããŠOSã®åé¡ã§ãããŸãšã
> ãªOSãªããmalloc()ããåŒã°ããã·ã¹ãã ã³ãŒã«ã§ç¢ºä¿
> ãããã¡ã¢ãªãŒã¯ããã°ã©ã ã®çµäºæã«ã¯è§£æŸãããã¯
> ãã§ãããªãããšãããšãOSããã®ããã«äœã£ãŠãããª
> ããšãããããªåé¡ãèµ·ããŠå°ãããã§ãããããã£ãŠã
> éœã«free()ããå¿
èŠã¯ãªãããšããã®ãäžè¬è§£ã§ãã
>
ä»ãã£ãŠã¿ããšããNTãšSolarisã¯freeããªããŠãè¯ãã¿ããã§ããã
DOS/Windows95ã§ã¯ã©ããªãã§ããããã
ku...@gssm.otsuka.tsukuba.ac.jpãã
>ããè¯ããã°ããããã®æ¬ã®æžåãæããŠé ããŸãããã
>ä¹
é
äžç°ããã®å®ç¿Cèšèª(å®è·µCèšèªã ã£ããã)
ã«äŸä»ãã§æžããŠãããŸããäžå¿DOSã念é ã«ããã€ã€
ãã¹ãŠã®ãã©ããããŒã ã«å¯ŸããŠæå¹ãšãã觊ã蟌ã¿ã®æ¬ãªãã§ããã
VCã®æ¬ãå«ããŠçã£ç«¯ããç«ã¡èªã¿ããŠã¿ããšãã
ãã®ä»å€ãã®æ¬ã«
freeããã«æŸã£ãŠãããŠè¯ã
ãšæžããŠããã®ã§èŠã€ãåºãã®ã¯é£ãããªããšæããŸãã
Arita
Arita <bar...@geocities.co.jp> wrote in message news:38AB90...@geocities.co.jp...
> oh...@src.ricoh.co.jpãã
>
> > ããã¯èšèªä»æ§ã®åé¡ã§ã¯ãªããŠOSã®åé¡ã§ãããŸãšã
> > ãªOSãªããmalloc()ããåŒã°ããã·ã¹ãã ã³ãŒã«ã§ç¢ºä¿
> > ãããã¡ã¢ãªãŒã¯ããã°ã©ã ã®çµäºæã«ã¯è§£æŸãããã¯
> > ãã§ãããªãããšãããšãOSããã®ããã«äœã£ãŠãããª
> > ããšãããããªåé¡ãèµ·ããŠå°ãããã§ãããããã£ãŠã
> > éœã«free()ããå¿
èŠã¯ãªãããšããã®ãäžè¬è§£ã§ãã
> >
>
> ä»ãã£ãŠã¿ããšããNTãšSolarisã¯freeããªããŠãè¯ãã¿ããã§ããã
> DOS/Windows95ã§ã¯ã©ããªãã§ããããã
OS ã®åé¡ã§ããããŸãããMS-DOS ãšããŸã§èŠéã«å
¥ãããš
C runtime library ãšãã®åé¡ã§ããããŸãã
ã§ãMS-DOS ã®å Žåã§ã¯ãMS-C ãšãã䜿ã£ãŠãããªããéåžž
ã®çµäºæé ã§çµäºããå ŽåãªãïŒããã°ã©ã ãèœã¡ããšããããª
ããŠïŒãã¡ã¢ãªãŒã¯è§£æŸãããã¯ãããã® OS ã®å Žåã¯ãOS ãã¡
ã¢ãªãŒã®ç®¡çã£ãŠèšã£ãŠè¯ããã£ãŠã®ã¯åãæ¢ããèã«çœ®ãã(^^;
Windows95 ã®å Žåã䌌ããããªãã®ã§ãéåžžã®æé ã§çµäºãã
ãªããæ¬æ¥ãªã解æŸãããã¯ãã§ãããäœæ
ããããªããªãæã
ãããããªãã¡ã¢ãªãŒä»¥å€ã®ãªãœãŒã¹å«ããŠã
> ku...@gssm.otsuka.tsukuba.ac.jpãã
>
> >ããè¯ããã°ããããã®æ¬ã®æžåãæããŠé ããŸãããã
> >ä¹
é
>
> äžç°ããã®å®ç¿Cèšèª(å®è·µCèšèªã ã£ããã)
> ã«äŸä»ãã§æžããŠãããŸããäžå¿DOSã念é ã«ããã€ã€
> ãã¹ãŠã®ãã©ããããŒã ã«å¯ŸããŠæå¹ãšãã觊ã蟌ã¿ã®æ¬ãªãã§ããã
> VCã®æ¬ãå«ããŠçã£ç«¯ããç«ã¡èªã¿ããŠã¿ããšãã
> ãã®ä»å€ãã®æ¬ã«
> freeããã«æŸã£ãŠãããŠè¯ã
> ãšæžããŠããã®ã§èŠã€ãåºãã®ã¯é£ãããªããšæããŸãã
ãããç¶æ³äŸåã§ãããã
OS ã CRT ãã¡ãããšåŸåŠçããŠããããããªã·ã¹ãã ã§ããã®
ããã°ã©ã ã®ããã»ã¹ã®å¯¿åœãçããã¡ã¢ãªãŒè³æºãã»ãšãã©äœ¿ã
ãªããããªãã®ã®å Žåãæ瀺çã«è§£æŸããªããŠããå®çšäžå€ãã¯ã
åé¡ãªãã£ãŠããšã§ãä¹
éãããææããŠãããããªãããã°ã©ã
ã®ããã»ã¹ãšããŠã®å¯¿åœãé·ããã¡ã¢ãªãŒè³æºã䜿ãæšãŠãããã
ããªããªã£ãã¡ã¢ãªãŒã解æŸããªããšæéãšãšãã«æ¶è²»éãå¢å€§ã
ããããªããã°ã©ã ã®å Žåã¯ããããªããªã£ããã¡ãããšæ瀺çã«
解æŸããŠãããªããšãšãã§ããªãããšã«ãªãã
ãŸããåŸè
ã®ããšãèããããããã°ã©ãã®è¯å¿ãšããŠãåè
ã®
å Žåã§ãã¡ãããšç®¡çããããã«ããŠããæ¹ããCèšèªããã°ã©
ãã®æ
床ãšããŠãcanonical ã§ããã
--
Narita Takaoki @A.I.SOFT,INC.
ã¹ã¿ã³ã¹ã®éããšããã°ãããŸã§ã§ããã
> P.S. çµäºæã«è§£æŸããããã©ããã¯OSã®æ©èœæ¬¡ç¬¬ã§ãããé·æéåã
> ç¶ãããœãããæžããšããããªããšãããããããããšã«ãªãã®
> ã§ãå¿
ã解æŸããç¿æ
£ãã€ããã¹ãã§ãã
ããããç¿æ
£ã身ã«ã€ããããšã®æ£ããã¯ãšããããšã
ãŠãäœã§ãå®å
šåŽã«æ¯ã£ãŠããã°åé¡ãªããšããŠã·ã¹ã
ã ãç解ããããšããªãããããã¯ç解ããã«ããŸããŠ
ããŸã姿å¢ã«ãåé¡ãããããªãããšç§ã¯ã€ãã¥ãæã£
ãŠããã®ã§ããã
ã§ããããã質åã«ã¯ãã£ã±ããã·ã¹ãã ããã¡ããšã
ãã©ãã¯ã¿ãŠããããã©ãã ããã£ãŠããŒã£ã¡ãã ãã
ãããšçããã¹ããªãã§ãããã...ã
In article <UEHARA.00F...@maia0.ec.ntts.co.jp>
ueh...@po.ntts.co.jp (UEHARA, Junji) writes:
|int main(int argc, char** argv) {
| int n = atoi(argv[1]);
| int* data = (int*)malloc(n * sizeof(int));
ïœ
|}
ããã«
free(data);
ãè¿œå ããšããŠãã ãã(^^;ã
# Javaãæžãããã..ã
--
§NTTSâFT æè¡éçºéšãšã¬ã¯ããããã¯ã³ããŒã¹æè¡ã»ã³ã¿ãŒ äžå æœ€äº Â§
PGP Key fingerprint = B7 C0 CB 1F 1C 88 69 2A 25 36 8A EE 93 A3 61 72
>ããããç¿æ
£ã身ã«ã€ããããšã®æ£ããã¯ãšããããšã
>ãŠãäœã§ãå®å
šåŽã«æ¯ã£ãŠããã°åé¡ãªããšããŠã·ã¹ã
>ã ãç解ããããšããªãããããã¯ç解ããã«ããŸããŠ
>ããŸã姿å¢ã«ãåé¡ãããããªãããšç§ã¯ã€ãã¥ãæã£
>ãŠããã®ã§ããã
ã§ã malloc 㯠free ãããä»æ§ãã®é¢æ°ã§ãã
ããç¹å®ã® OS ã®ã¡ã¢ãªé¢é£ã®ã³ãŒã«ã®ä»æ§ãš
åæãªå€æã§äžç·ã«ãã姿å¢ã«åé¡ãããã®ã§ãããã
ãããªåºæ¬çãªããšããïŒæ¬æ°ã§ïŒãããããªã人æã
è²ãŠãŠããŸãããšããããfree ããªããŠããããšã¢ããã€ã¹ããŠ
ããŸãããšã®åé¡ã®æ¬è³ªã ãšæããŸãã
oh...@src.ricoh.co.jpãã:
> ããããç¿æ
£ã身ã«ã€ããããšã®æ£ããã¯ãšããããšããŠãäœã§ãå®
> å
šåŽã«æ¯ã£ãŠããã°åé¡ãªããšããŠã·ã¹ãã ãç解ããããšããªãã
> ãããã¯ç解ããã«ããŸããŠããŸã姿å¢ã«ãåé¡ãããããªãããšç§
> ã¯ã€ãã¥ãæã£ãŠããã®ã§ããã
ããããç¥ããŸãããç§ãUnixã§ããã°å€§äžå€«ãªã®ã¯ç¥ã£ãŠããŸãã
DOSãªããã ãšæªããããªãã§çµ¶å¯Ÿå®å
šãµã€ãã§äœã£ã¡ãããšæããªã
ããå æžãªå¥Žã§ãããŸããã
ã§ãDOSã§åããããã°ã©ã ãªããŠãŸã絶察äœããªããã ä¹ é
In article <88gq1e$soq$1...@news01be.so-net.ne.jp> ,
"Yoshiki Kataoka" <kat...@ka2.so-net.ne.jp> writes
>ã§ã malloc 㯠free ãããä»æ§ãã®é¢æ°ã§ãã
ãããšã¯æããªããªãå€éã« malloc ããåŸãã©ã³ãã ãªé åºã§ free
ãããšæ¥µããŠé
ããªã malloc ã¯ããã€ããããŸãããã®ãããªå Ž
åã¯ãfree ããªãã§æããã»ããæ£ããã
C++ ã®å Žåã¯ããªããžã§ã¯ãã®åŸå§æ«ãããã®ã§ãdestructã匷å¶
ãããŸããããããã§ã殺ããŠããªããªãããªãªãã¢ããªã±ãŒã·ã§
ã³ãããããããã§ããã? ãããåœãåã ãšããã£ãŠã¯ãããªããŠã
ã¡ãããšæ©ãçµãããã®ã¯æ©ãçµã£ãŠããã»ãããããšæããŸãã
>ããç¹å®ã® OS ã®ã¡ã¢ãªé¢é£ã®ã³ãŒã«ã®ä»æ§ãš
>åæãªå€æã§äžç·ã«ãã姿å¢ã«åé¡ãããã®ã§ãããã
exit æã«ãã©ã®é åã®ã¡ã¢ãªãæ®ããã©ããã¯ãmalloc ãšã¯
ç¬ç«ã®åé¡ã§ãããã®ãããããã£ã¡ãã«ãã¹ãã§ã¯ãªãã§ãã
>ãããªåºæ¬çãªããšããïŒæ¬æ°ã§ïŒãããããªã人æã
>è²ãŠãŠããŸãããšããããfree ããªããŠããããšã¢ããã€ã¹ããŠ
>ããŸãããšã®åé¡ã®æ¬è³ªã ãšæããŸãã
åã¯ãexit() ãããªããclose ããªããŠãããããfree ããªããŠã
è¯ãã£ãŠæããŸããexit()ãcloseããã®ã¯ä»æ§ã§ãããexit()ã§
ããã»ã¹ã®ã¡ã¢ãªé åã解æŸãããã®ãä»æ§ã§ãããã
ããã¯ãã¡ãããšç解ããŠããã°ããã®ã§ããã?
---
Shinji KONO @ Information Engineering, University of the Ryukyus
æ²³éçæ²» @ çç倧åŠå·¥åŠéšæ
å ±å·¥åŠç§
In article <38AB90...@geocities.co.jp>
Arita <bar...@geocities.co.jp> writes:
| > ããã¯èšèªä»æ§ã®åé¡ã§ã¯ãªããŠOSã®åé¡ã§ãããŸãšã
| > ãªOSãªããmalloc()ããåŒã°ããã·ã¹ãã ã³ãŒã«ã§ç¢ºä¿
| > ãããã¡ã¢ãªãŒã¯ããã°ã©ã ã®çµäºæã«ã¯è§£æŸãããã¯
| > ãã§ãããªãããšãããšãOSããã®ããã«äœã£ãŠãããª
| > ããšãããããªåé¡ãèµ·ããŠå°ãããã§ãããããã£ãŠã
| > éœã«free()ããå¿
èŠã¯ãªãããšããã®ãäžè¬è§£ã§ãã
|
| ä»ãã£ãŠã¿ããšããNTãšSolarisã¯freeããªããŠãè¯ãã¿ããã§ããã
ãããªããŠè¯ãããšããã®ãã¡ãã³ã£ãšåŒã£ããããŸããã
ãå¿ èŠã¯ãªããâ ãããªããŠè¯ãã
ã§ãã
ã»ç©ç®ããŠäœ¿çšããã¡ã¢ãªãå°éã§ããã
ã»ãããã¯å®è¡ãçæéã§ããããšã確å®ããŠãããªã©ã®çç±ã§
ã»ã¡ã¢ãªã解æŸããªãããšã§ã·ã¹ãã ã«è² è·ããããªãããšã
ã»ãããŠãã®ããšããã®ããã°ã©ã ã®ãŠãŒã¶ã䜿ãå¯èœæ§ã®
ãããã¹ãŠã®ç°å¢ã«ãããŠçã§ãããä»åŸæ°žå«ã«çã§ãããšèŠèŸŒãŸã
ã»ããã«ãã®ããã°ã©ã æçãåå©çšããŠãå
šãå¥ã®ããã°ã©ã ã«
çµã¿èŸŒãã ãããå¯èœæ§ãç¡ãããš
ãªã©ã®æ¡ä»¶ãæãç«ã€ãªããã¶ããããªããŠè¯ããã§ããããã
æ¬åœã«ãããªã®ããããããèããŠã¿ãŠãã ããã
ã§ãçµå±ãããããããšã¯ãå®ã¯çãç®ã«ãã£ãŠããè¯ãåãããŸãã
Windowsã®ãµãŒãã¹ãäœã£ããšããŠãããã©ãŒãã³ã¹ã¢ãã¿ã§èŠãŠãããã«ã¯
è¯ãåãããªãã»ã©ã¡ã¢ãªäœ¿çšéããããããšäžãã£ãŠãããïŒé±éåŸã«ã¯äœ¿
ãç©ã«ãªããªãã»ã©ïŒ®ïŒŽãéããªããããªããããªæç
ãæã£ãæ°äžïœåæ°äž
è¡ã®ããã°ã©ã ãç®åã«ç«ã¡å°œããããšããæ³åããŠã¿ãŠãã ããããèªåãª
ããŒãããªããã§éãã¡ãé§ç®ã ãéãã¡ãé§ç®ã ããšèªåã«èšãèãããã
ãããªããšãä»ã§ã¯è¯ãæãåºã§ãã
ããšããµãŒãã¹ãããªããŠããããŒããšããWindows 2000ãšãããµã¹ãã³
ãã»ã¬ãžã¥ãŒã ã§ããç°å¢ãæ®åããŠããŠãå幎ãšãïŒå¹Žãšãã®æéããã°ã©
ã ãèµ°ããã£ãŠã®ãããã»ã©çããããšãããªããªã£ãŠãããã£ãŠããšãèã
ãšããŠãè¯ãã§ãããã
ããã¯æ£ãããªãã®ã§ã¯ãªãã§ãã?
malloc()ãã·ã¹ãã ããã¡ã¢ãªãŒãããã£ãŠããé¢æ°ã§
ããããšã¯æ£ããã®ã§ãããfree()ã¯ãã®éãããé¢æ°
ã§ã¯ãªãã®ã§ããfree()ã¯ãããããã®ããšã§malloc()
ãè¡ã£ããšãã«å確ä¿ãããã¡ã¢ãªãŒãçšæããé¢æ°ãš
ãšãããã»ããããã§ãããã
ã€ãŸãmalloc()ãšfree()ã¯å¯Ÿç§°ã§å¯Ÿçãªé¢ä¿ã«ãããã
ã§ã¯ãªããšããããšã§ãã
malloc()ãããã®ã¯ç¡æ¡ä»¶ã«free()ãã¹ãã ãšæããã
ãšã¯ããã®é察称æ§ã«æ°ã¥ããã£ã³ã¹ã奪ãããšã«ãã€
ãªãããŸãããã
# ãã¡ãããmalloc()ã§äœ¿ãçµãã£ãã¡ã¢ãªãŒã¯ãã®æ®µ
# éã§free()ããã®ãããç¿æ
£ã ãšããç¹ã«ã€ããŠã¯äœ
# ã®ç°è°ããããŸããã
å
èšäºã®æ¹ã¯ããã®ãããªmalloc()ã®äœ¿ããããããŠã
ããšãã«äœ¿ãçµãã£ãã¡ã¢ãªãŒãfree()ããªããŠããã
ãšãå°ããªã®ã§ã¯ãªãã§ãããã
ãããããã®ãããªããã°ã©ã ãé·ã
ãšçšŒåããããšã§
ä»äºãçµããŠçµäºããçŽåã«ããããŸã§ç¢ºä¿ããŠããã
ãŸããŸãšããã¡ã¢ãªãŒããã¹ãŠã®åå¥ã«è§£æŸããããšã
æ¬è³ªçã«å¿
èŠãªã®ãããå°ããªã®ã ãšæããŸãã
ãããã¯ã¹ã€ã®åé¡ã§ãããã
ããããæ··åããŠå®å
šåŽã«æ¯ã£ãŠãäžå©çãããã»ã©ã
ããšã¯æããŸããããåé¡ãç°ãªãããšã¯ç解ããŠãã
ãã»ããããããšããã®ãç§ã®èãã§ãã
oh...@src.ricoh.co.jpãã:
> malloc()ãã·ã¹ãã ããã¡ã¢ãªãŒãããã£ãŠããé¢æ°ã§ããããšã¯æ£
> ããã®ã§ãããfree()ã¯ãã®éãããé¢æ°ã§ã¯ãªãã®ã§ããfree()ã¯
> ãããããã®ããšã§malloc() ãè¡ã£ããšãã«å確ä¿ãããã¡ã¢ãªãŒ
> ãçšæããé¢æ°ãšãšãããã»ããããã§ãããã
ããšãfree()ããäœãããªããé¢æ°ã ãšããŠããfree()ãåŒã³ãã ^_^;
ããããæ²³éãããèšã£ãŠãããããªå Žåã¯äœãããªãfree()ãæ¡ä»¶
ä»ãã³ã³ãã€ã«ã§çªã£èŸŒãã°ããã®ãã
> malloc()ãããã®ã¯ç¡æ¡ä»¶ã«free()ãã¹ãã ãšæããããšã¯ããã®é
> 察称æ§ã«æ°ã¥ããã£ã³ã¹ã奪ãããšã«ãã€ãªãããŸãããã
å¥ã«malloc()ãšfree()ãšã¯äœããšããããšãæ®éã«æããã°ãããã
ãªæ°ããããã§ããã©ããã£ãšããã®ã¯èªåã§malloc()ãšfree()ãæžã
ããããšã ã£ããããŠã
ãšããã§ãæ§ãç®GCãå
¥ãã¡ãã£ãŠfree()ã¯åŒã°ãªãããšã«ããããš
ããã®ãªãããããããšæã£ãŠãŸãã
ã©ãããä¿å®çGCããšããèš³èªã¯å«ãã§âŠ ä¹ é
ããããããªãããšããããšãã·ã¹ãã ã«äŒããé¢æ°ãš
ããŠfree()ãåŒã¶ã®ã¯ããããšã ãšç§ãæããŸãããã
äŒããŠã·ã¹ãã ãäœãããŠããããã«ã€ããŠã¯éå°ãªæ
åŸ
ãããªããšããåæã§ãã§ãã...ã:-)
> å¥ã«malloc()ãšfree()ãšã¯äœããšããããšãæ®éã«æããã°ãããã
> ãªæ°ããããã§ããã©ããã£ãšããã®ã¯èªåã§malloc()ãšfree()ãæžã
> ããããšã ã£ããããŠã
ã§ãããããæå³ãããã°ã©ã äœæ³ããªã話ããªã
> ãšããã§ãæ§ãç®GCãå
¥ãã¡ãã£ãŠfree()ã¯åŒã°ãªãããšã«ããããš
> ããã®ãªãããããããšæã£ãŠãŸãã
mark&releaseã®æã«éæ»ããã§ããããã:-)
ç§èªèº«ã¯ã¢ããªã±ãŒã·ã§ã³ãã©ããªã¡ã¢ãªãŒã®äœ¿ããã
ãããã®ããšããç¶æ³å€æãæãã«ããŠäžåŸã«malloc()
ãšfree()ã䜿ãããšã«ãã©ã¡ãããšããã°å察ã§ãã
èªåã§ã¡ã¢ãªãŒããŒã«ãäœã£ãŠäžéã®ç®¡çãšã䜿çšç¶æ³
ã®ç£èŠãããã»ããããããšã¯ãããããŸãããã
In article <88h6bs$v1$1...@ns.src.ricoh.co.jp> ,
oh...@src.ricoh.co.jp (Junn Ohta) writes
>ç§èªèº«ã¯ã¢ããªã±ãŒã·ã§ã³ãã©ããªã¡ã¢ãªãŒã®äœ¿ããã
>ãããã®ããšããç¶æ³å€æãæãã«ããŠäžåŸã«malloc()
>ãšfree()ã䜿ãããšã«ãã©ã¡ãããšããã°å察ã§ãã
åãããæããŸããfork()ããŠãã²ããã malloc() ããŸãã exit()
ãšããã®ã¯ãUnix ã§ã¯ãå²ãšå¹ççãªããã°ã©ãã³ã°ã§ãã
Effective C++ ã§ããèªåã®new()ãäœãããšã¯æšå¥šããŠããŸããã
C++ ã§ãdestructor ãå¿
ãåŒã°ããŠããŸãã®ã¯ãåã¯ãããæå³
ã§å€±æã ãšæããŸããå¿
èŠããªãã®ã«åŒã¶ã®ã¯å€ã ããå¹çãæªã
ããŠããããšã¯ç¢ºãã§ãã
>èªåã§ã¡ã¢ãªãŒããŒã«ãäœã£ãŠäžéã®ç®¡çãšã䜿çšç¶æ³
>ã®ç£èŠãããã»ããããããšã¯ãããããŸãããã
ããããããŸããã
>ã§ãããããæå³ãããã°ã©ã äœæ³ããªã話ããªã
malloc(),free() ãå¿
ãçµã§åŒã¹ã°å®å
šã ã£ãŠããšã«ã¯ãªããªããš
æããŸããfree() ãå¿ããã£ãŠã®ãè¯ãããããšãªã®ã¯èªããã
ã©ãã©ã¡ãããšããã°ãfree()ããã®ã«ããŸã䜿ã£ãŠããŸã£ããšã
ã®æ¹ãå±ãªãããã§ããããfree()ããªããã°ãããããããšã«ã¯
ãªããŸããã
GC ããexit() ã®æ¹ãã¯ããã«å®å
šãªããšã¯ç¢ºãã ãšæãã人ã®
æã«é Œã£ãŠ free() ããã®ã£ãŠããããªã«å®å
šãªããšãããªã
ãšæããŸãããalloca() ãšãã®æ¹ããŸã ãŸãã
ã ããããsbrk() ããããããè¿ããªã OS ã ã£ãŠçµæ§ãããã
ã ãã䜿ããªãã¡ã¢ãªã¯ãã©ããä»®æ³èšæ¶ã«è¿œãåºãããããã ã
ããããã»ã©å€§ããªå®³ã ãšèšãããã§ããããŸããã
ãããfree()/malloc()ã§ãfragmentation ãåé¡ã«ãªããªãã
ããã解決ããã¹ããªã®ã§ãã£ãŠãfree()ããã°ããã£ãŠãã®
ã§ã¯ãªããšæããŸããã
ããäžãäžã®æã¯èŠå¯ãªãæ¶é²ãå©ããŠããããã ãããæ²³åã§ãã£ã³ãããŠ
ãããããããã£ãŠã®ãšãæ¬è³ªçã«å€ããããããªæ°ãããŸããããã¶ãã
ãããã人ã¯çŸå®ã«æ»ã«ãããªç®ã«éãããå®éæ»ãã§ããŸãããšãããããš
æããŸãã
ããŸããæ¬äººã¯èªå·±è²¬ä»»ã®çµæãªãã§ããã§ãšãšãã§ãããäžå¹žãªã®ã¯ããã
ãã人ã«æãã£ããããŠããŸãç¡ç¥ãªäººãã¡ã§ããªã
+---------------------------------------------------------------------+
| From : Dairyo Gokan ( åŸç¥ å€§éµ ) |
| Org. : Hitmark Computer Corporation ( ãããããŒã¯ã³ã³ãã¥ãŒã¿ ) |
| Adrs : 13256 Northup Way Suite 3, Bellevue WA 98005 |
| TEL:425-649-8808 FAX:425-649-9001 mailto:na...@can.bekkoame.ne.jp |
+---------------------------------------------------------------------+
ããå¹ççãã¢ããªããŠãåãªããææããããã°ã©ãã³ã°ã§ããïŒ
ãèªåã§äœ¿ãã¡ãã£ãšããããŒã«ãäœæããã®ã«ã现ãããšã©ãŒãã§ãã¯ãã
å
¥åãã©ã¡ãŒã¿ã®è§£æã端æã£ãŠææ©ãä»äžããã®ãšãå®éã«èª°ã§ã䜿ãã
ãããªããã°ã©ã ãäœãã®ãåäžèŠããŠããããªæ°ãããŸãã
> Effective C++ ã§ããèªåã®new()ãäœãããšã¯æšå¥šããŠããŸããã
> C++ ã§ãdestructor ãå¿
ãåŒã°ããŠããŸãã®ã¯ãåã¯ãããæå³
> ã§å€±æã ãšæããŸããå¿
èŠããªãã®ã«åŒã¶ã®ã¯å€ã ããå¹çãæªã
> ããŠããããšã¯ç¢ºãã§ãã
ãã³ã³ãã€ã©ã«äŸåããåé¡ãããããŸãããããã¹ãã©ã¯ã¿ããããã»ãã¡
ã€ã«å
ã®ã¯ã©ã¹å®£èšã§äœãããªãã€ã³ã©ã€ã³é¢æ°ãšããŠå®çŸ©ããŠããŸãã°ã
ã³ã³ãã€ã«ãããå®è¡ãã¡ã€ã«äžã§ã¯ãå
šãåŒã³åºãããªããšæããŸããïŒ
> malloc(),free() ãå¿
ãçµã§åŒã¹ã°å®å
šã ã£ãŠããšã«ã¯ãªããªããš
> æããŸããfree() ãå¿ããã£ãŠã®ãè¯ãããããšãªã®ã¯èªããã
> ã©ãã©ã¡ãããšããã°ãfree()ããã®ã«ããŸã䜿ã£ãŠããŸã£ããšã
> ã®æ¹ãå±ãªãããã§ããããfree()ããªããã°ãããããããšã«ã¯
> ãªããŸããã
ããããªã³ããæ¬æ°ã§èšã£ãŠããããã®ã§ããããïŒ
ããfree()ãããã€ã³ã¿ã䜿ã£ãŠããŸã人ãã¯ããmalloc()ããã«ãã€ã³ã¿ã
䜿ã£ãŠããŸã人ããšåã¬ãã«ã¢ããªãã§ããããã確ãã«ãã€ã³ã¿ãããŒã«ã«
å€æ°ãªãã³ã³ãã€ã©ããåæåããŠãªããã€ã³ã¿ã䜿ã£ã¡ããã¡ããâããš
芪åã«ã¯ãŒãã³ã°ã¡ãã»ãŒãžã§æããŠãããŸãããã°ããŒãã«å€æ°ãªãããã
ãç¡çã§ããã
ããã¶ããããªããã¯ãfree()ããã«ãåããã€ã³ã¿ãmalloc()ã§äžæžãããŠ
䜿ããŸãããããªã³ãŒãããå¹ççããšãèšã£ãŠèªæ
¢ãã«æžããŠããŸãæ°ã
ããŸãã
> GC ããexit() ã®æ¹ãã¯ããã«å®å
šãªããšã¯ç¢ºãã ãšæãã人ã®
> æã«é Œã£ãŠ free() ããã®ã£ãŠããããªã«å®å
šãªããšãããªã
> ãšæããŸãããalloca() ãšãã®æ¹ããŸã ãŸãã
ãå°ãªããšããæ¬åœã®ããã°ã©ãã¯ãæã«é Œã£ãŠ free() ããããªããŠãã
ã°ã©ãã³ã°ã¹ã¿ã€ã«ãããªãããšã ãã¯ç¢ºãã§ãã
> ã ããããsbrk() ããããããè¿ããªã OS ã ã£ãŠçµæ§ãããã
> ã ãã䜿ããªãã¡ã¢ãªã¯ãã©ããä»®æ³èšæ¶ã«è¿œãåºãããããã ã
> ããããã»ã©å€§ããªå®³ã ãšèšãããã§ããããŸããã
ãæ¬æ°ã§èšã£ãŠãã®ã§ããããïŒãçµèŸŒè£
眮ã家é»ãªã©ãå«ããŠèããã°ã
çŸå®ã«çšŒåããŠãã³ã³ãã¥ãŒã¿ã®ãã¡ãä»®æ³èšæ¶ããªãã·ã¹ãã ããã¡ã¢ãª
ã«å³ããå¶éãããã·ã¹ãã ã®ã»ãããã©ãã ãå€ããæ¿ç¥ããäžã§ã®çºèš
ãªãã§ããããïŒ
ãç 究宀ã®UNIXãã·ã³ããé ã«æµ®ãã°ãªããããªè²§å°ãªçºæ³ã¢ããã©ãããã
ããªãã§ãã...ã
ãããšãä»®æ³èšæ¶ãåããOSã§ãã£ãŠããå®éã«ã¯äœ¿ãããŠããªãã¡ã¢ãªã
free()ããªãããšã§ããããèŠæ±ããããã»ã¹ãååšãç¶ããéãäžå¿
èŠãª
ã¹ã¯ããã¢ãŠããçºçããŠã·ã¹ãã å
šäœã®ããã©ãŒãã³ã¹ãäœäžããããšã¯
ã倧ããªå®³ã以å€ã®äœè
ã§ããããŸããã
> ãããfree()/malloc()ã§ãfragmentation ãåé¡ã«ãªããªãã
> ããã解決ããã¹ããªã®ã§ãã£ãŠãfree()ããã°ããã£ãŠãã®
> ã§ã¯ãªããšæããŸããã
ããªãããåé¡ã®æ¬è³ªããŸã£ããç解ãããŠãªãæ°ãããŸãã
ããããç§ã®èšãããã®ãšã¯ã¡ãã£ãšéã...ã(^_^;)
ããã°ã©ã ãæžãã®ã«ãã£ã¡ããšããããŒã¿ç®¡çã¯å¿
èŠ
ã§ãããããªããã®ã¯ãããªããªã£ããšãã«ãã宣èšã
ãã»ããããã®ã¯ééããªãããfree()ããã®ã«ãäœã
æªãããšãèµ·ãã£ããšãããããã®åå ããfree()ãã
ããšãã«æ±ããã®ã¯ããããã§ãã
ãšããããã§ãèªåãããŸã©ãã ãã®ã¡ã¢ãªãŒãã©ãã§
䜿ã£ãŠããããããã°ã©ã ãæžããšãã«èæ
®ããããšã¯
倧äºãªã®ã ãã©ãããšãã°ããã°ã©ã ã§ããŒã£ãšæ±ããŠ
ããã¹ãããŒã¿ããã£ãŠããããããã°ã©ã ã®çµäºãšãš
ãã«äžèŠã«ãªããšããå Žåãªãããããããããfree()
ããããããªãã§ããããšããã®ãç§ã®æèŠã«è¿ãã§ã
ãããã
> GC ããexit() ã®æ¹ãã¯ããã«å®å
šãªããšã¯ç¢ºãã ãšæãã人ã®
> æã«é Œã£ãŠ free() ããã®ã£ãŠããããªã«å®å
šãªããšãããªã
> ãšæããŸãããalloca() ãšãã®æ¹ããŸã ãŸãã
ããã«å¿ ãããåæã§ããããã§ã¯ãªãã®ã§ãã...ã
䜿ãçµãã£ãã¡ã¢ãªãŒã¯free()ããŠããã°ã·ã¹ãã ãã©
ãã«ãããŠå¹çããåå©çšããŠãããã ããããšå®æã«
èããã®ã¯ãããªãããšã£ãŠãããã«ãããŸãããã
ããããŒã
ãããã»ã¹ã®çµäºæã«ãã®ããã»ã¹ã確ä¿ããã¡ã¢ãªãŒ
ãèªåçã«ãã¡ããšè§£æŸãããããšããã®ã¯ãããã»ã¹
ã®çµäºæã«ãã®ããã»ã¹ãã®ãã®ãå æããŠããã¡ã¢ãª
ãŒãèªåçã«ãã¡ããšè§£æŸããããã®ãšåãã¬ãã«ã®è©±
ã§ããã? åŸè
ã«ã€ããŠã¯å¿é
ããªããŠããã®ã«åè
ã«
ã€ããŠã¯å¿é
ããªããã°ãªããªãããšããã®ã¯ããããª
話ã§ãã
OSãçæããããã»ã¹ã®åŸå§æ«ã¯OSãã€ããã®ãåœç¶ã§ã
ããããäžãäžããšè¡šçŸããã®ã¯ã©ãããªã...ã
ããããããïœãã(T_T)
ãããã»ã¹ã®å®è¡ã³ãŒããèªã¿èŸŒãããã®ã¡ã¢ãªã確ä¿ããã®ã¯OSã§ããã
ããã»ã¹å
ã®ã³ãŒãã確ä¿ããã¡ã¢ãªã®ç®¡çã¯ããã»ã¹ã®è²¬ä»»ã§ãããã
ããã ããã§ã¯ãããã®æªãããã°ã©ã ãèµ°ããããšãã·ã¹ãã ãåæ¢ãã
ããã¡ã¢ãªãªãœãŒã¹ãæžã£ãŠãæãå¥ã«ããOSã®ãããæªãããªããŠèšãã
ãŠããŸããã§ãOSåŽãæ°ãå©ãããŠããããæ©èœãæã£ãŠãã ãã®è©±ã§ããã
ãåäŸ(ããã»ã¹)ã®äžå§æ«ã¯èŠª(OS)ã®è²¬ä»»ã ãããåäŸã¯äœãããŠããã
ãšããè¡äžã§åžæ°(ããã»ã¹)ããã€æšãŠããŠããè¡ãæž
æããã®ã¯è¡æ¿(OS)
ã®è²¬ä»»ã ããããŽããæŸããªããŠè¯ããšèšã£ãŠããããªã¢ã³ã§ãã
ãæªæ¯ãããªãåäŸãããªããã°ä¿è·è
ã¯ããããããã¿ããã€æšãŠãã
ãã«ãããªããããããè¡æ¿ãæž
æããªããŠãæžããã§ããããŸãäžã®äž
ããéœåãããããã®ã§ããããã®ã·ã¹ãã ãããããã§ãã
> OSãçæããããã»ã¹ã®åŸå§æ«ã¯OSãã€ããã®ãåœç¶ã§ã
> ããããäžãäžããšè¡šçŸããã®ã¯ã©ãããªã...ã
ãã¢ããããã®OSãçæããããã»ã¹å
ã§ããã¯ãããä»ã®ããã»ã¹ã®ãšã³
ããªããæ»ãå¿ããI/Oããã€ã¹ã®ç¶æ
ãŸã§OSã§æ»ããŠããããã§ããïŒ
ããäžãäžãã£ãŠã®ã¯ãé©åã¢ããªããããããããããæ³åŸ(ä»æ§)ã§ã¯ã
ãšãã®ã»ãã劥åœããªãããããã«ããããããã人éã®ãåããŠåœç¶ã®
ãµãŒãã¹ãšæã£ãŠãæ
床ãã¯å€ãããã§ããïŒ
ããŸãèãè¡ãæèã®äžã§ããã®æãèŠåãèããŠåŸ
é¿ããšããããã£ãã
ã£ãŠæã£ãŠãåœäººã®è²¬ä»»ããä»æ¹ãªãããªã
"Arita"ãããããã«ã¡ã¯ã
> Cèšèªã®æ¬ãã«ã¯ãããŠã
> mallocããã¡ã¢ãªãŒããã䜿ããªããªãæŸã£ãŠããã°
> çµäºæã«èªåçã«è§£æŸãããã®ã§æ瀺çã«freeããªããŠãè¯ã
> ãšæžããŠããã®ã§ãããå
æ¥ããéèªãèŠããš
> æ瀺çã«freeããªããšã¡ã¢ãªãŒãªãŒã¯ã«ãªã
> ãšæžãããŠããŸãããã©ã¡ããæ£ããã®ã§ããããã
ç§ã®èªãã§ããCèšèªã®æ¬ã«ã¯ãããŠããfreeãããšããã®ã§ãã(^_^;)ã
freeããªãã£ãã¡ã¢ãªãŒãã©ããªããã¯OSãšã¢ããªã±ãŒã·ã§ã³ã«ãã£ãŠéã£ãŠã
ãŸã(^â^)ããããã£ãŠããåºæ¬çãªå¿åŸãšããŠã¯ãèŠããªããªã£ãã¡ã¢ãªãŒã¯ãã®
æç¹ã§freeããã®ãè¯ãããšãªããŸã(^â^)ã
åã¯ä»¥äžã®ããã«ããŠãªãŒã¯ãæžããåªåã¯ããŠããŸããããããŸãåèã«ã¯ãª
ããŸããã(^â^)ã
// å®çŸ©
T* pT = NULL; // å²ãåœãŠãããŠããªããã€ã³ã¿ã¯å¿
ãNULL
// å²ãåœãŠ
assert(pT); // å€éå²ãåœãŠã¯èš±ããŸãã
pT = (T*)malloc(sizeof(T));
// éæŸ
//assert(pT == NULL); // å²ãåœãŠãããŠããªãã®ã«freeãããª!ã(䜿ããª
ããšãããã)
free(pT);
pT = NULL;
ãåé¡ã¯ãReal programmer don't write Pascal.ãã ã£ãã¯ãããæ»ã«ãã
ãŠãè³çŽ°èã®èšé²ãæ£ãããã°ã1983幎4æå·ãããã®ãBitãèªã«ã翻蚳èšäº
ãèŒã£ãŠãŸãã
ãFM-11BSãšããã¡ã¢ãªç©ºéäžã«VRAMçãããããããŠããªãç¹æ®ãªç°å¢ã§ã¯
960KBã®ã³ã³ãã³ã·ã§ãã«ã¡ã¢ãªã確ä¿ã§ããæ©çš®ããããŸãã
ã
ãããããã¯ãããšã㊠...
ãDOSã§ããã°ã©ã ãå®è¡ãããCOMã¢ãã«ã®å Žåãæ®ãã®ç©ºãã¡ã¢ãªå
šäœã
ããã»ã¹ã«æž¡ãããŸãããmalloc()ã¯æçµçã«ã·ã¹ãã ã³ãŒã«(DOSã®INT21H
ãã¡ã³ã¯ã·ã§ã³)ãåŒã³åºãã®ã§ããã®ãŸãŸã§ã¯malloc()ã§ããŸããã
ãtinyã¢ãã«ã§ã³ã³ãã€ã«ãããå®è¡ãã¡ã€ã«ã¯ãã¹ã¿ãŒãã¢ããã«ãŒãã³
å
ã®ã³ãŒãã§å¿
èŠã¡ã¢ãª(æ倧64KB)ãæ®ããŠãOSã«äžæŠã¡ã¢ãªãè¿ããŸãã
ãEXEã¢ãã«ã®å ŽåãEXEãããå
ã«ãã®ããã°ã©ã ãå¿
èŠãšããã¹ã¿ãã¯é
ã®ã¡ã¢ãªã ãã確ä¿ããã¹ã¿ãã¯ãã€ã³ã¿ãåæåãåŸãããã°ã©ã ãå®è¡
ãããŸãã
ããŸãã_dos_allocmem()/_dosfree()ãšãã£ããINT21HçŽçµã®ã¡ã¢ãªé¢æ°ã§ã¯
ãªããæšæºã®malloc()/free()ã§ã¯ãèŠæ±ãããåäœã§ã¡ã¢ãªããã®éœåºŠOSã«
èŠæ±ããã®ã§ã¯ãªããåºå®é·ã®ãããã¯åäœã§é åèŠæ±/解æŸããŠããŸãã
ãããã¯ãã¡ã€ã«I/Oãšåæ§ã·ã¹ãã ã³ãŒã«ã®åŒã³åºããæžããã®ãšãå°ãã
ãã¯ã®ç¢ºä¿/解æŸã®ç¹°ãè¿ãã«ããæçåãé²ãããã ãšæããŸãã
> ãããDOSã§åé¡ã«ãªãã®ã¯ãåããã»ã¹ãçæãããš
> ãã§ããåããã»ã¹çæã®ã·ã¹ãã ã³ãŒã«ãåŒã¶ãšãã
> ã®ã·ã¹ãã ã³ãŒã«ã¯èŠªããã»ã¹ã®ã¡ã¢ãªãŒäœ¿çšç¶æ
ã調
> ã¹ãŠã䜿çšäžã®æ«å°Ÿã®é åããåŸãããã£ãããã¹ãŠè§£
> æŸããããã«åããã»ã¹ãããŒãããŠã¡ã¢ãªãŒç®¡çãå
> ããã»ã¹ã«æãæž¡ããŸãã
ãã·ã¹ãã ã³ãŒã«(DOSã®INT21Hãã¡ã³ã¯ã·ã§ã³)ã§ã¯ãããŸã§é¢åèŠãŠãã
ãŸããããã¡ã€ã³ããã»ã¹ã¯ãããåããã»ã¹ã®ããŒãã«ã¡ã¢ãªãäžè¶³ãã
ãããªããåããã»ã¹ãå®è¡ããåã«ã䜿ã£ãŠããªãã¡ã¢ãªé åãfree()
ããŠãOSã«è¿ããªããã°ãªããŸããã
ãOSã«ã¯malloc()ãããé åããå®éã«ããã»ã¹å
ã§äœ¿ãããŠãããç¥ã
ãã¹ããããŸããããã·ã¹ãã ã³ãŒã«(OSå
éšã®åŠç)ããã¡ã¢ãªã®äœ¿çšç¶æ
ãç¥ãæ¹æ³ã¯ãé åãããã»ã¹ã«å²ãåœãŠãã(malloc())ãŠãããããã
ã§ãªãã(free())ã®ã¿ã§ãã
ãã§ããããOSã«ããåããã»ã¹çææã«ã芪ããã»ã¹ã®é åãåæã«è§£æŸ
ããŠåããã»ã¹ãå®è¡ããããšã¯ãããŸããã
> ãããã£ãŠãããšãã°èŠªããã»ã¹ãããŒããããåŸã§å€§
> ããªé åAãmalloc()ããå°ããªé åBãmalloc()ãã倧
> ããªé åAãfree()ãããšãããšããããã芪ããã»ã¹
> ã®æ¬äœã®ããšã«é åAã®æ®éªžã®ç©ºãé åããã£ãŠããã®
> ããšã«äœ¿çšäžã®é åBãååšããããšã«ãªããŸãã
ãDOSã§ã¯ãããŒãžã³ã°æ¹åŒã®ã¡ã¢ãªç®¡çæ©æ§ããµããŒãããŠããªãã®ã§ã
ãã£ããããšããã§ãã ...
> ãã®ç¶æ
ã§åããã»ã¹ãçæãããšãåããã»ã¹ã«æž¡ã
> ããã®ã¯é åBã®çŽåŸããã®ã¡ã¢ãªãŒç©ºéãšãªãã芪ã
> ãã»ã¹ãfree()ããé åAã®ã¡ã¢ãªãŒãããšãå®éã«OS
> ã«è¿åŽãããŠãããšããŠãããã®ããšã¯åããã»ã¹ã䜿
> çšå¯èœãªã¡ã¢ãªãŒç©ºéãå¢ããããšã«ã¯ãŸã£ããå¯äžã
> ãªãããã§ãã
ãæšæºé¢æ°ã®malloc()ã«ã¯ã¡ã¢ãªç¢ºä¿ã®éã®çŽ°ããæå®ãå¯èœãªä»æ§ã¯(ç°å¢
ã«äŸåããã®ã§)çã蟌ãŸããŠããŸããããå°ãªããšããDOSãã¡ã³ã¯ã·ã§ã³
(INT21H)ã§ã¯ãåããã»ã¹ã®ããŒããšå®è¡ã«å¿
èŠãªé åã解æŸãããé åAã«
åãŸããªããé åAããã£ãé åã«å床é åã確ä¿ããããæå®ã§ããŸãã
> ããã§å¿
èŠã«ãªãã®ã¯ãã¡ã¢ãªãŒç®¡çã«ã€ããŠã®ã·ã¹ã
> ã å¯ãã®ç¥èã§ããããçåœã®é åã確ä¿ããç¶æ
ã§é·
> åœã®é åã確ä¿ãããããªãããšãããããªã¹ãã©ããž
> ãŒã§ãã£ãŠããmalloc()ãããã®ã¯äžèŠã«ãªã£ããfree
> ()ããããšããã¬ãã«ã®äžè¬è«ã¯åœ¹ã«ç«ã¡ãŸããã
ããmalloc()ãããã®ã¯äžèŠã«ãªã£ããfree()ããããšããäžè¬è«ã ãã§
ãªããã寿åœã®é·ãé åã¯ãããããå
ã«ç¢ºä¿ããŠããããšããã«ãŒã«ã§
ååãªåé¡ãšããæ°ãããŸãã
ããŸããANSI-Cã®æšæºé¢æ°ã¯æšæºã©ã€ãã©ãªãšããŠæçšã§ãããCèšèªã®èšèª
ä»æ§ã®æ¬è³ªçãªéšåã§ã¯ãããŸããã
ãå¯æ¬æ§ãå¥ã«ããã°ãANSI-Cã®èŠå®ãããmalloc()/free()ãã䜿ãããšã¯
Cèšèªã䜿ãéã«åŒ·å¶ãããã¹ãåé¡ã§ããããŸãããããæ°ã«å
¥ããªããã°
å
éšã§èªåã¬ãŒããŒãžã³ã¬ã¯ã·ã§ã³ããç¬èªä»æ§ã®malloc()/free()é¢æ°ãªã
ãäœæããã°ããã ãã§ã¯ãªãã§ããïŒããã£ãšããé¢æ°ãè¿ã(æž¡ã)å€ã¯
ãã€ã³ã¿ã§ã¯ãªããã³ãã«ã«ããªããã°ãªããŸãããããã€ã³ã¿ã䜿ãã»ãš
ãã©ã®ã©ã€ãã©ãªé¢æ°ã®æžãå€ããå¿
èŠã«ãªããããããŸãããã
> ãããã«ããŠããfree()ããŠãæå³ããªããè¡ãåŠçã
> å¢ããã ãç¡é§ãªå Žå(UNIXã®ããã»ã¹çµäºæ)ããäžèŠ
> ã«ãªã£ã段éã§free()ããã ãã§ã¯ååã§ã¯ãªã(DOSã®
> åããã»ã¹çææ)ãªã©ãããããã§ãããã€malloc()
> ããŠãã€free()ãããã¯ã¢ããªã±ãŒã·ã§ã³ã®æ§æ Œãªã©ã
> ãŸããŸãªç¶æ³ãèæ
®ããŠèšèšããã¹ãã§ããäžèŠã«ãªã£
> ããfree()ããããšããå现èãªæ¹éã§ãã¹ãŠã貫ãã
> ãšããã®ã¯ééãã ãšæã£ãŠããŸãã
ããäžèŠã«ãªã£ããfree()ãããã§ååã¢ããªãã§ããïŒãfree()ãåŒã³åºã
ãŠå®éã«OSãã¡ã¢ãªã解æŸããã®ã«ããããªãŒããŒããããªããŠãã»ãšãã©
ãªãã«çãããšæããŸããïŒããªãã§ããã®çšåºŠã®ã³ããç¹æ®ãªäŸãåŒãåã
ã«åºããŠãŸã§ããµããã³ãã«åºå·ããããã®ãçç±ãããããŸããã
Dairyo Gokan wrote:
> ããå¹ççãã¢ããªããŠãåãªããææããããã°ã©ãã³ã°ã§ããïŒ
ããã¯ããã§ãã(^^)
> > malloc(),free() ãå¿
ãçµã§åŒã¹ã°å®å
šã ã£ãŠããšã«ã¯ãªããªããš
> > æããŸããfree() ãå¿ããã£ãŠã®ãè¯ãããããšãªã®ã¯èªããã
> > ã©ãã©ã¡ãããšããã°ãfree()ããã®ã«ããŸã䜿ã£ãŠããŸã£ããšã
> > ã®æ¹ãå±ãªãããã§ããããfree()ããªããã°ãããããããšã«ã¯
> > ãªããŸããã
>
> ããããªã³ããæ¬æ°ã§èšã£ãŠããããã®ã§ããããïŒ
æ²³éããã¯ãããããäºãããããŠãæ¬æ°ã§ãèšã人ã§ãã
åå¿è
ã®ãã®ã³å€ªåçãé¡æãããã©ãããçãã«è§£æ±ºããããšãããã§ãã
ããïŒã®ã³å€ªãmalloc()ããã¡ã¢ãªã¯free()ããªããã
ã®ã³å€ªïŒããããçä»ããã®ãã€ãå¿ãã¡ãããã ããªãã
ãã©ãããïŒã¯ããããã¹ãŒãã»ãããããã(ãžã£ãžã£ãžã£ãŒã³)
ã¿ãããªãïŒéããã...(^^;
ïŒä»æ¥ãfj.sci.mathã«ãŠ
ïŒ ã®ã³å€ªïŒããããã©ãèŠãã£ãŠ 0.999... < 1 ãããªãããã
ïŒ ãã©ãããïŒã¯ãããã¡ãããã
ããããããã(ãžã£ãžã£ãžã£ãŒã³)
ïŒãããéãïŒ(^^;;;
å€åfjæŽãããŸãã«é·ãããã
ãããµããããã³ãã«é£œããŠãããããªãããšæããŸãã
ïŒã ãããã±ãããããšèšããã
ïŒãåããããå
šéšæŸã£ãŠã転ããããšèšããã
ããããèžé¢š^H^Hã¹ã¿ã€ã«ãªãã§ããå€åã(^^;
> ãå°ãªããšããæ¬åœã®ããã°ã©ãã¯ãæã«é Œã£ãŠ free() ããããªããŠãã
> ã°ã©ãã³ã°ã¹ã¿ã€ã«ãããªãããšã ãã¯ç¢ºãã§ãã
ããã«è¶ã ã
ãæ¬åœã®ããã°ã©ãããšèããšã
ãã¯Pascalãæžããªãããšç¶ããŠããŸãç§ã(^^;
解ããªã人ã®æ¹ãå€ããã ãããªããã®ãã¿ã
ïŒä»ãªããSmallTalkãæžããªãããããrubyãæžããªãããã
ïŒãããããŠãJAVAãæžããªããïŒ
oh...@src.ricoh.co.jpãã:
> ããããªãã ããããçŽåŸã§ããããããã®åé¡æ³ã§ãããšæ²³éãã
> ã¯äœå±ããã«ãªãã®ããªã:-)
æ°åå±ã ä¹
é
P.S. ããŒæ²³éããããã! é¢çœããšã€ãæžããŠããŸããã ããŒã
> > ãå°ãªããšããæ¬åœã®ããã°ã©ãã¯ãæã«é Œã£ãŠ free() ããããªããŠãã
> > ã°ã©ãã³ã°ã¹ã¿ã€ã«ãããªãããšã ãã¯ç¢ºãã§ãã
>
> ããã«è¶ã
ã
> ãæ¬åœã®ããã°ã©ãããšèããšã
> ãã¯Pascalãæžããªãããšç¶ããŠããŸãç§ã(^^;
...
> ãïŒãæ¬åœã®ããããªããŠãæ¬ç©ã®ãã ã£ãã£ãïŒ
ã€ãã§ã«èšã£ãŠãããšãæžããªãããããªããŠã䜿ããªããã§ã¯ãªãã£ãã§ãã
ãã?
å°ãªããšããbitã«ç¿»èš³ãèŒã£ãæã®éŠé¡ã¯ã䜿ããªããã§ããã
åæã¯å€åã
http://www.edfac.usyd.edu.au/staff/souters/Humour/Real.Programmer.Stories.html
--
äžå±±éäº
nakayam...@lab.ntt.co.jp
In article <38ACB403...@cv.sony.co.jp> ,
"Takemoto,Satoshi" <take...@cv.sony.co.jp> writes
>æ²³éããã¯ãããããäºãããããŠãæ¬æ°ã§ãèšã人ã§ãã
ãããããã€ã§ããåã¯ç 究ã®æ¹ã ãããããããããšãããã¹ãã ãšã
æãããã
>åå¿è
ã®ãã®ã³å€ªåçãé¡æãããã©ãããçãã«è§£æ±ºããããšãããã§ãã
> ããïŒã®ã³å€ªãmalloc()ããã¡ã¢ãªã¯free()ããªããã
> ã®ã³å€ªïŒããããçä»ããã®ãã€ãå¿ãã¡ãããã ããªãã
> ãã©ãããïŒã¯ããããã¹ãŒãã»ãããããã(ãžã£ãžã£ãžã£ãŒã³)
>ã¿ãããªãïŒéããã...(^^;
ãŸããããã§ããmalloc()ãããfree()ããã°ããããããã£ãŠã®ã¯ã
æ¬åœã¯ããããªã«ç°¡åãããªãããå®éã
ãã€ã(malloc() ãããªãã°ããã€ã free())
ã£ãŠã®ãGCæãã«æ€èšŒããããšæã£ãããäžè¬çã«ã¯äžå¯èœã§ãã
(æ¬åœã®ããã°ã©ãã¯ããããã©ããã£ãŠä¿èšŒããŠããã®ã ãã?
èªå㧠reference countããã£ãŠã®ã C++ ã§ã¯å€ãããããªãã
ãªã)
ããããããGCã®æ£åœæ§ã蚌æããã»ããããããã£ããããŸãã
ãã£ã¡ã¯ãããã°ã©ã ã®æ¯èã§ã¯ãªããŠãããŒã¿æ§é ã®æ£åœæ§ãšã
ãŠç€ºããŸãããã
>ããããèžé¢š^H^Hã¹ã¿ã€ã«ãªãã§ããå€åã(^^;
ãããããã§ãã?
In article <88i807$2lr$1...@epsongw6.epson.co.jp>, "Narita Takaoki" writes:
> æ²³éããçã«ã¯ã人éã®ããã°ã©ã ã«ãããã¡ã¢ãªç®¡çæ§é ã®èš
> èšèª€è¬¬æ§ãèæ
®ãããšã誀ã£ãæ§é ã«èšèšãé£ã GC ã«ãã管ç
> ãå°å
¥ããæ¹ããããå®å
šã§ãããããšã§ãèšãããã£ãã®ã§ã¯ãª
> ãã§ãããããã
ãŸãããããªãšããã§ãã
> å°ãªããšããããã°ã©ãã³ã°ãšããä»äºã®çµæã®ææç©ã®å質ã«ã
> 責任ãè² ãè·æ¥ããã°ã©ãããèŠãã°ããããªæ
床ã§ä»äºããã
> ãªãèŸããŠãããšèšãããã§ããããã
確ãã«ãã¡ã¢ãªãªãŒã¯ãçãèŠãæ°ã¯ãªãã§ããããã¯ãã£ã¡ããããªã
ããšãã§ãããããé²ãäžã€ã®æ¹æ³ãšããŠã
block release reserve -> malloc in module
module release -> block memory release
ãšããææ³ããã£ãŠãmalloc -> exit ã£ãŠããã®ã¯ãããããå®å
šãµã€ã
ã®ææ³ã®äžã€ã ãšææããããªã
object ã® destruct ã ãšãobject ãOSã®ã·ã¹ãã ãªãœãŒã¹ãæ¡ã£
ãŠããããšãããã®ã§ãã©ãããŠãåŒã¶å¿
èŠããããšããã®ãç解
ã§ãããã§ãããçŽåŸã§ããªããšãããå€ãã§ããã
> ãŸããç 究宀㮠UNIX äºã
ãšã¯èšããªãã§ãããå®éšå®€ã¬ãã«
> ã®è©±ãšå®éã®ãã£ãŒã«ãã§ã®è©±ã¯å¥ã§ããã£ãŠäºã§ãããããã
ãŸããããã¯ããã§ããããã©ã
ãªãã»ã©ããã®ãããã¯ç§ã®ç¥èã§ã¯åŸç¥ããã«ç«ã¡æ
ã¡ã§ããŸããããããããäºè§£ã§ãã
> ãEXEã¢ãã«ã®å ŽåãEXEãããå
ã«ãã®ããã°ã©ã ãå¿
èŠãšããã¹ã¿ãã¯é
> ã®ã¡ã¢ãªã ãã確ä¿ããã¹ã¿ãã¯ãã€ã³ã¿ãåæåãåŸãããã°ã©ã ãå®è¡
> ãããŸãã
ããã«ãã£ãŠæ®ãã®ç©ºãé åãã¹ãŠãmalloc()å¯èœãªã
ãŒããšãªãããã§ãããã
> ãOSã«ã¯malloc()ãããé åããå®éã«ããã»ã¹å
ã§äœ¿ãããŠãããç¥ã
> ãã¹ããããŸããããã·ã¹ãã ã³ãŒã«(OSå
éšã®åŠç)ããã¡ã¢ãªã®äœ¿çšç¶æ
> ãç¥ãæ¹æ³ã¯ãé åãããã»ã¹ã«å²ãåœãŠãã(malloc())ãŠãããããã
> ã§ãªãã(free())ã®ã¿ã§ãã
> ãã§ããããOSã«ããåããã»ã¹çææã«ã芪ããã»ã¹ã®é åãåæã«è§£æŸ
> ããŠåããã»ã¹ãå®è¡ããããšã¯ãããŸããã
ç§ãèšãããã£ãã®ã¯ãmalloc()ãããŠããé åã ãã
æ®ããŠãããšã®éšåãåããã»ã¹ã«æž¡ããšããããšã§ãã
芪ããã»ã¹ã®ãé åããšããããšã°ã®å®çŸ©ããããŸãã«
ããŠãããã®ãæªãã£ãã®ã§ãã...ã
> ããäžèŠã«ãªã£ããfree()ãããã§ååã¢ããªãã§ããïŒãfree()ãåŒã³åºã
> ãŠå®éã«OSãã¡ã¢ãªã解æŸããã®ã«ããããªãŒããŒããããªããŠãã»ãšãã©
> ãªãã«çãããšæããŸããïŒ
ãã? åéããããŠããŸã£ãã...ã
ãfree()ãåŒã³åºããŠå®éã«OSãã¡ã¢ãªã解æŸããã®ã«
ããããªãŒããŒããããã§ã¯ãªããŠããããã°ã©ã ã®çµ
äºæã«ãããŸã§free()ãããŠããªãé åããã¹ãŠèª¿ã¹ã
ããŠfree()ããã®ã«å¿
èŠãªãªãŒããŒãããããåé¡ã«ã
ãŠããã®ã§ãã
ãªãããfree()ãããã«ã¯ãfree()ãããé åããã®æ¬¡
ã«malloc()ãããŠããããã«ã¡ã¢ãªãŒç®¡çããŒã¿ã®åæ§
æãè¡ãããªãŒããŒããããå«ãŸããŠããŸãã
> ãªãã§ããã®çšåºŠã®ã³ããç¹æ®ãªäŸãåŒãåã
> ã«åºããŠãŸã§ããµããã³ãã«åºå·ããããã®ãçç±ãããããŸããã
ããµããã³ããã«åºå·ããŠããããã§ã¯ãªããŠããµãã£
ãŠãå®å
šãã©ãããç¥ãã³ããã«åºå·ããŠããã®ã§ãã
ã絶察ã«ãµãããªããšæ±ºããŠãããŠããµãã£ããšãã«ã©
ããªãããç¥ããã«ããŸãããã®ã¯ãããªãç¿æ
£ã ãšã
ãããšã§ãã
>ãããšã¯æããªããªãå€éã« malloc ããåŸãã©ã³ãã ãªé åºã§ free
>ãããšæ¥µããŠé
ããªã malloc ã¯ããã€ããããŸãããã®ãããªå Ž
ããã¯ãã§ãã®æªãå®è£ ãã§ãã£ãŠããä»æ§ããšã¯å¥ãªè©±ã§ãã
ã©ãããŠãçŸå ŽãšããŠèŠéããããã«è¡ããªãå Žåã¯ã
æ¡ä»¶ã³ã³ãã€ã«ããããæ€èšãã¹ãçš®é¡ã®åé¡ã§ãã
>exit æã«ãã©ã®é åã®ã¡ã¢ãªãæ®ããã©ããã¯ãmalloc ãšã¯
>ç¬ç«ã®åé¡ã§ãããã®ãããããã£ã¡ãã«ãã¹ãã§ã¯ãªãã§ãã
埡æã
ãã£ã¡ãã«ãããããäžè¬çã«ã free äžèŠãšããå°è©ã«è³ã
ã®ã§ããããmalloc ã¯ã¡ã¢ãªç®¡çãexit ã¯ããã»ã¹ç®¡çã
ããããæ
åœåéã®éããã®ã§ãã
>è¯ãã£ãŠæããŸããexit()ãcloseããã®ã¯ä»æ§ã§ãããexit()ã§
>ããã»ã¹ã®ã¡ã¢ãªé åã解æŸãããã®ãä»æ§ã§ãããã
ãã¢ããªãããããšãå
šãŠã解æŸãããããã§ã¯ãªããã
éã«ïŒé¢æ°ã®ç¬æã§è§£æŸããŠããè³æºã°ãããšãéããŸããã
ããç¹å®ã®å Žé¢ã§ãããã¹ãããšããµãã£ãŠãå©ããããšã
ç¥ã£ãŠãããããšãã£ãŠããããäžè¬è«ã«èãããæãæ¹ã¯
æãŸãããªããšãã£ãŠããã®ã§ããïŒããã§èšããããã¹ãããšã
ãšã¯ãåãããè¿ããéãããéãããèªåã§èããçš®ã®
èœãšãåã¯èŠªã«é Œããªãããªã©ã®ããšã§ãïŒ
>ããã¯ãã¡ãããšç解ããŠããã°ããã®ã§ããã?
ãããã¡ãããšç解ãããã¹ããªã®ã§ãã
ïŒåºåŒµã®ãã次ã®ã³ã¡ã³ãã¯ïŒé±é匱é ããŸã
ããã€ã¯ãã®éãã§ãããã©ã
ïœ > mallocããã¡ã¢ãªãŒããã䜿ããªããªãæŸã£ãŠããã°
ïœ > çµäºæã«èªåçã«è§£æŸãããã®ã§æ瀺çã«freeããªããŠãè¯ã
ããããäžè¬åã£ãŠã®ãåé¡ãªãã§ãããïŒ
æ¬æ¥ã¯ãè¿åŽãã¹ããã§ããããã»ã£ãŠãããŠåé¡ãªãå Žåã«
éãã (exit ã§ã free ã§ãããã) æåŸã«ãŸãšããŠããã°
ãããããã£ãŠã ãã®ããšã§ã
ïŒ free ããã¡ãã¡ããã®ã§æéãé£ãã®ã¯ã°ãã°ããããã©ã
ïŒ ããã§è³æºãå§è¿«ããããã°ãã°ããããããæžãŸãªãå Žå
ïŒ ãããã§ãããããã
--
ã¿ã〠- mi...@minz.org - http://www.minz.org/ - ã¿ããŸã€ã»ã²ãã
> 䜿ã£ãŠããããããã°ã©ã ãæžããšãã«èæ
®ããããšã¯
> 倧äºãªã®ã ãã©ãããšãã°ããã°ã©ã ã§ããŒã£ãšæ±ããŠ
> ããã¹ãããŒã¿ããã£ãŠããããããã°ã©ã ã®çµäºãšãš
> ãã«äžèŠã«ãªããšããå Žåãªãããããããããfree()
> ããããããªãã§ããããšããã®ãç§ã®æèŠã«è¿ãã§ã
> ãããã
ç§ãããã«äŒŒããããªæèŠã§ãã£ãŠãquick & dirty ããã°ã©ã ã§ã¯
fcloseããªãã£ããXtDestroyApplicationContextãåŒã°ãªãã£ããããŸãã
ãŠããããåŸè
ãåŒã³åºããŠããããã°ã©ã ã¯ããŸãèŠãªãã§ããã
Xã§ã¯ããã°ã©ããŒã®æèã¬ãã«ã§ãµãŒããŒåŽã®ãªãœãŒã¹ãäžå¯§ã«
ç Žæ£ããŠåãããã°ã©ã ãæžãæ¹ãçãããããªæ°ãããŸãã
ãããŠãã¯ãªãœãŒã¹ã®ç Žæ£ãGUIã©ã€ãã©ãªãXã®ä»æãã«ä»»ããŠããŸããŸãã
ãµãŒããŒãšã®æ¥ç¶ãåããããèªåçã«ç Žæ£ãããããšã«ãªã£ãŠããŸãã
XImageãªããžã§ã¯ããïŒãã¡ã€ã«ããèªã¿èŸŒãã ç»åãšäº€æãããšããªããšãããïŒ
çµäºæã«ç Žæ£ããŠããªããã®ãã»ãšãã©ãªã®ã§ãã¯ã©ã€ã¢ã³ãåŽã§ã
å Žåã«ãã£ãŠã¯ïŒå¯Ÿè±¡ã«ãã£ãŠã¯ïŒäžè¬çãããããŸããã
> In article <38AB7D...@geocities.co.jp>, Arita writes:
ïŒäžç¥ïŒ
> ãªã®ã§ããçµäºæãªãã°æ瀺çã«free ããå¿
èŠããªãããšããæ¹ã
> æ®éã§ãããã
> ããã§ã free ããã£ãŠããã®ã£ãŠããã£ãã塵åãã®äžã«ã®ã£ãŠ
> ããã®ã«ããã³ã»ããã§äžã€äžã€ãŽãç®±ã«ç§»ããŠãããããªãã®ã§
> ãã
äŸã®OSã§ã¯ããã¯ãããŸããããããããOSããã以äžãfreeã䜿ããªãã®ã¯ã移æ€æ§ã
æããŸãã
> ããŒãäžéšïŒ¯ïŒ³ã¯malloc()çžåœã®ãã®ãå
±æã¡ã¢ãªã®å²ãåœãŠ
> ãšç䟡ã ã£ãããããŸããã§ãããããã®ã¯ããããã
ãªããšæã£ãŠããŸãããããã£ããã§ããã
Cã®ä»æ§äžå
šãåé¡ãããŸãããããã«ãã¿ã¹ã¯ã®OSãšããŠã¯äžé©åã§ããã
å匷ã«ãªããŸãã
> ããããããšãªããã«ãŒã«ãéµå®ããããšãã人ã¯ãã®
> ã«ãŒã«ãã©ãããæå³ããã£ãŠããŠãã©ãããŠå®ããªã
> ãã°ãªããªãããç解ããå¿
èŠãããã ãããšç§ã¯èã
> ãŸããç解ããã«èšãããããšãå®ãã ãããšãã姿å¢
> ãæãŸãããã®ã§ãããšã¯æããŸããã
ä»åã®åé¡ã§ã¯ãä»æ§ãå®ãããšãã移æ€æ§ã®åäžã«ã€ãªããã
ãªããªããä»æ§ãå®ãããšã§ã移æ€æ§ãä¿ã£ãŠããããã§ãã
ä»æ§ãå®ããªãã£ããã移æ€æ§ã倱ãããŠåœç¶ã®ããšã ããã
ããé©åœãªçç±ã§ããããïŒ
>å
èšäºã®æ¹ã¯ããã®ãããªmalloc()ã®äœ¿ããããããŠã
>ããšãã«äœ¿ãçµãã£ãã¡ã¢ãªãŒãfree()ããªããŠããã
>ãšãå°ããªã®ã§ã¯ãªãã§ãããã
ããããå°ããªã®ã ãšæããŸããã
ãmalloc ããã free ãã¹ããïŒãïŒé·æ皌åãé€ãïŒã
ãããªé€å€ãæžãããããªäººã¯ã
ãããããããªè³ªåãããå¿
èŠããªã人ã§ãããã
ãã£ãšããçŽç³ŸããŠããæ§ãèŠãŠæ¥œãã¿ããã£ãã®ãªã
å¥ã§ããã
>ãããããã®ãããªããã°ã©ã ãé·ã
ãšçšŒåããããšã§
>ä»äºãçµããŠçµäºããçŽåã«ããããŸã§ç¢ºä¿ããŠããã
>ãŸããŸãšããã¡ã¢ãªãŒããã¹ãŠã®åå¥ã«è§£æŸããããšã
>æ¬è³ªçã«å¿
èŠãªã®ãããå°ããªã®ã ãšæããŸãã
ã©ãããŠãããŸããŸããšæããã®ã§ããããã
è³æºãèŠæ±ããåã¢ãžã¥ãŒã«ã®äžã§ãèªåã®åŸçä»ãã ãã
ãã£ãŠããã°ããããšã§ãåŸçä»ãã®æ°ãããŒãã®æ°ã§å²ã
ãšãªãŒãºããã«ãªæ°ã«ãªã£ãŠããã¯ãã§ãã
å§ãã« exit ããããã§èããããšã«ããæ°ã®ããªãã¯ã§ãã
ãªãã§ãããã
ãã®ãããã¯å€ªç°ãããšã¯å¯Ÿç«ããŠããªããšæã£ãŠããŸãããïŒ
UNIXã®ããã»ã¹ã®ä¿è·ã«äŸåããŠããŸããã
ãã以å€ã®ç°å¢ã®ããšãèæ
®ããŠããŸããã
å®éã«ã¯ãWindowsã®ãããªäœããããªãã·ã¹ãã ã®æ¹ãå€ãã®ã§ãããã
UNIXã§ãç©ã«ãã£ãŠã¯ãmallocã§å
±æã¡ã¢ãªã確ä¿ããç©ããããŸããã
exitã§å¿
ãã¡ã¢ãªã解æŸãããšããããšã¯èŠå®ãããŠããŸããã
ãã£ãŠãfreeã䜿ã£ãŠã¡ã¢ãªã解æŸããªããã°ãOSã«ãã£ãŠã¯ãã©ãã«ã®åå ãšãªããŸ
ãã
ãã ããexitã§å¿
ãã¡ã¢ãªã解æŸãããšããããšã¯èŠå®ãããŠããOSã ããã¿ãŒã²ããã«
ãããªã話ã¯å¥ã§ããã
> >ä»æ§ãå®ããªãã£ããã移æ€æ§ã倱ãããŠåœç¶ã®ããšã ããã
> >ããé©åœãªçç±ã§ããããïŒ
>
> é©åœ? 移æ€æ§ãçµã¿èŸŒã¿æ©åšãšããæããªããmalloc()/free()ã
> äºæž¬å¯èœãªç¯å²ã§åŠçããããšã®é£ããã«çç®ãã¹ãã§ããã
移æ€æ§ãšã¯ãéãç°å¢ã§ãåãããšãä¿èšŒããããšèšãããšã§ãã
ããšãã°ãUNIXã§ãDOSã§ãWindowsã§ãåé¡ãªãåããšèšãããšã§ãã
ãŸãçµã¿èŸŒã¿æ©åšãäžå¿ãã®äžã«å
¥ããŸãã(ãã ããä»æ§ã«ãã£ãŠããããš)ã
èšèã®æå³ã誀解ããŠããŸãããïŒ
ä»æ§ãšãã£ããããŸãããããããããªã
malloc()/free()ã®çµã¿åããã§ã¯OSããå²ãåœãŠãŠ
ããã£ãã¡ã¢ãªãŒãOSã«è¿åŽãããšä¿èšŒããããšã¯
ããã°ã©ã ããã§ã¯äžå¯èœ
ãšããã®ãmalloc()/free()ãšããã€ã³ã¿ãŒãã§ãŒã¹ã®
ä»æ§ã§ãããããããã°
malloc()ããã¡ã¢ãªãŒãã¹ãŠãfree()ããªãã§çµäº
ããããã°ã©ã ã¯ãã©ãã«ãèµ·ããããšããä»æ§ã®
malloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£
ããããšã¯
malloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ã®ç§»æ€æ§ãäœäžã
ããé倧ãªéå
ã ãããšããããšã§ãã
ã¡ãªã¿ã«ãANSI Cã§ã¯free()ã«ã€ããŠãé åã解æŸã
ããããªãã¡ã以éã®malloc()ã§ãã®é åãåå©çšã
ããããã«ããããšããæå³ã®ããšãæžãããŠããã¯
ãã§ãã
ãã®ç¹æ®ãªç°å¢ã®malloc()ãfree()ã®ããã¥ã¢ã«ã«ã©
ãããããšãæžãããŠããã®ãèå³ãããŸããã
æ¶ãããã©ããfree()ã§ç¢ºèªã§ããŸãã?
ããããããŸã§ãã
> ããããå°ããªã®ã ãšæããŸããã
> ãmalloc ããã free ãã¹ããïŒãïŒé·æ皌åãé€ãïŒã
> ãããªé€å€ãæžãããããªäººã¯ã
> ãããããããªè³ªåãããå¿
èŠããªã人ã§ãããã
å
èšäºã®è³ªåã¯ãçµäºæã«èªåçã«è§£æŸãããããšãã¡
ã¢ãªãŒãªãŒã¯ã«ãªããã®ã©ã¡ããæ£ããã®ãããšããã
ã®ã§ããããèªåãmalloc()ããã¡ã¢ãªãŒã®ãããã»ã¹
çµäºæã®æåããå°ããŠããã®ã¯æããã§ãã
# ããã»ã¹çšŒåäžã«äžèŠãªã¡ã¢ãªãŒããã®ãŸãŸããããŠ
# ããããšããã¡ã¢ãªãŒãªãŒã¯ããšã¯ãããªãã
ããããç°å¢ã§malloc()/free()ã®ãããªãã¡ã¢ãªãŒã
OSã«è¿åŽã§ãããããã°ã©ã ãã確èªã®ãããããªãã
ã€ã³ã¿ãŒãã§ãŒã¹ã䜿ãããšã®ã»ããããã»ã©ãã©ãã«
ã®åå ã®ãããªæ°ãããŸããããããã§ãã?
ããã°ã©ã ã§è²¬ä»»ããã£ãŠOSã«ã¡ã¢ãªãŒãè¿åŽããããš
ã奚å±ãããªãããã®ããã®é¢æ°ã¯å°ãªããšãæ£ããè¿
åŽã§ãããã©ãããè¿ãå€ãšããŠè¿ãä»æ§ã®ãã®ã§ãªã
ãã°ãªããªãã§ããããfree()ã¯ããã§ã¯ãªãã
ãŽãã®æ¯å©ã§ããã°ãfree()ãããšããã®ã¯ãŽãç®±ã®ã»
ãã«ãŽããæŸãæããŠãããšã¯ç¥ããã·ããããŠããã
ããªãã®ã§ããfree()ã䜿ããããããã以äžã®ããšã¯
ããããã§ããªãã®ã§ãã
ããã¯æ£ãããªããšæããŸããã
free()ããæç¹ã§OSã«è¿ããªãã®ãªãmalloc()/free()
ãOSã«ã¡ã¢ãªãŒãè¿ãäžè¬çãªæ¹æ³ã¯ãããŸãããã©ã
ã«ããã®ããã®ããã¯ã¯ãªãã®ã§ãããã
ãããããã®ã§ããè¿ãããããã«ãªã£ãŠãããªããã
ã®ã¡ã¢ãªãŒãmalloc()ããããŸãŸã®ãã®ã§ãfree()ã§è§£
æŸæžã¿ã®ãã®ã§ãåºå¥ãªãOSã«è¿ããããã«ãªã£ãŠãã
ã®ãåœç¶ã§ãããã
> èŠããã«ãfreeã䜿ããªãããã«ãã©ãã«ãèµ·ãã£ãå Žåã¯ãããã°ã©ããŒã®è²¬ä»»ã ãšæ
> ããŸãã
free()ã䜿ã£ãŠãããã°ã©ããŒã責任ããšããããã§ã¯
ãªãããšããããšãç§ã¯ç€ºããã€ããã§ãã
> mallocã¯ãã©ããããªãœãŒã¹ãåŸãã®ããèŠå®ãããŠããŸããã
> å
±æã¡ã¢ãªãã確ä¿ããOSãããããã§ãã
> ããããä»æ§ã«ã¯åããŠã¯ããªããšæããŸã(ãã«ãã¿ã¹ã¯OSãšããŠã¯äžé©ã§ãã)ã
> ããããç°å¢ããã以äžãfreeã䜿ã£ãŠã¡ã¢ãªã解æŸããªããã°ããã©ãã«ãèµ·ããç°å¢
> ãååšããã®ã§ã移æ€æ§ãäœããšãããã§ãããã
ããã¯ãfree()ããªãã§çµäºããããã°ã©ã ã®ç§»æ€æ§ã
äœããã®ã§ã¯ãªããŠãmalloc()/free()ãšããã€ã³ã¿ãŒ
ãã§ãŒã¹ã®ç§»æ€æ§ãäœããã®ã§ãã
> å ±æã¡ã¢ãªãã確ä¿ããOSãããããã§ãã
ãšãããããªç°å¢ã§ã¯ãããããmalloc()/free()ãã»
ãã®OSãšåãç®çã§äœ¿ãã¹ãã§ã¯ãªãã§ãããã
malloc()/free()ãããã«ã移æ€æ§ããããããªãµãã
ããŠããããæ¹éã§å®è£
ãããŠããç°å¢ãããããšã«ã€
ããŠã¯ãäºå®ãªããããã«åé¡ã§ããããã®ããšã«è§Šã
ãŠããã®ã¯æªãããšã§ã¯ãªãã§ããã
ãã ãããããç°å¢ãªããããããmalloc()ãããŠãã
ãã¹ãŠã®ã¡ã¢ãªãŒãäžåŸã«OSã«è¿åŽãããããªé¢æ°ãçš
æãããŠããã®ã§ã¯ãªãããšæããŸãã
ãã¹ãŠã®ã¡ã¢ãªãŒå²ãåœãŠã¯malloc()çµç±ã§è¡ãããã®
ã§ãããããmalloc()/free()ããã±ãŒãžã¯ã©ãã ãã®
ã¡ã¢ãªãŒã䜿çšäžã§ãããã«ã€ããŠç¥ã£ãŠããããã§ã
ãããmalloc()ããããŸãŸã®ãã®ã§ãããfree()ããã
ãã®ã§ãããèªåãéããŠå²ãåœãŠãããã¡ã¢ãªãŒãã
ã¹ãŠOSã«è¿ãã®ã¯ç°¡åãªããšã§ãã
ããããmalloc()/free()ããã±ãŒãžã®ã»ãã§ãã¹ãŠã®
ã¡ã¢ãªãŒãOSã«è¿åŽãããªããmalloc()ãåŒã³åºããã
ãã°ã©ã ã®åŽã§èªåã§ããŒã¿æ§é ããã©ã£ãŠfree()ããŸ
ãããããå®å
šç¢ºå®ã«åŠçãè¡ããããã§ãããfree()
ã§ã¯äžå¯èœãªãã¡ãããšOSã«è¿ããããæ
å ±ãããã°ã©
ã åŽã«äŒããããšãã§ããããã§ãã
ããããç°å¢ãçšæããªãã§ãå
±æã¡ã¢ãªãŒããã ãã®
ãŸãŸäœ¿ã£ãŠmalloc()/free()ãå®è£
ããŠããOSããããš
ããã®ã¯ãç§ã«ã¯ä¿¡ããããããšã§ããã
ãããã»ã¹ã®çµäºåã«èªåã§ãã¹ãŠfree()ããã°OSã«ã
ã¹ãŠã®ã¡ã¢ãªãŒãè¿åŽããããšã«ãªãããšããmalloc()
/free()ã®å®è£
ã«ã¯ã©ããªãã®ãããåŸããèããŠã¿ãŸ
ãããå¯èœæ§ããããšããããããªãã®ã§ãããã
(1) malloc()ãšfree()ã¯ãã€ã¯OSã®ã¡ã¢ãªãŒç¢ºä¿/解æŸ
ã®ã·ã¹ãã ã³ãŒã«ããã®ãŸãŸåŒãã§ããã ãã
ããã§ã¯çŽ°ããã¡ã¢ãªãŒç¢ºä¿/解æŸã®ã¿ã€ãã³ã°ã§
ã·ã¹ãã ã³ãŒã«ãçºçããããã§ããŸã£ããéå¹ç
ã§ããããã解æŸã«free()ã䜿ã以äžãæ£ãã解æŸ
ããããã©ããããã§ãã¯ããæ¹æ³ãååšããªãã
ãã§ãå±éºãã®ãããªãããããªåœ¢ã§ã·ã¹ãã ã³ãŒ
ã«ã«malloc()/free()ã®ç®ããã¶ãããšããã®ã¯ã
ã£ãŠã¯ãªããªãããšã§ãã
(2) malloc()ã¯å¿
èŠã«å¿ããŠå€§ããªåäœã§OSããã¡ã¢ãª
ãŒã確ä¿ããŠãããã现åãã«ããŠåŒã³åºãåŽã«è¿
ããfree()ã¯è¿ãããã¡ã¢ãªãŒããŸãšãŸã£ãåäœã«
ãªããšOSã«ãããè¿ãã
å¢çæ¡ä»¶ãååšããããšã«ãªãããã®å¢çã§çŽ°ãã
malloc()/free()ãç¹°ãè¿ããšå¹çãèœã¡ãŸããã
ã®ãšãã®å¹çã¯malloc()ãå
éšã§ã¡ã¢ãªãŒç®¡çãã
ãŠããåã ã(1)ããããã«æªããªããŸãã
(3) (2)ãšåãã ããfree()ã§ãã¹ãŠã®ã¡ã¢ãªãŒã解æŸ
ããããšãã ãOSã«ãããã®ã¡ã¢ãªãŒãè¿ãã
æ¬è³ªçã«(2)ãšåãã§ãã¡ã¢ãªãŒãã²ãšã€ã確ä¿ã
ããŠããªãç¶æ
ã§çŽ°ããmalloc()/free()ãç¹°ãè¿
ããš(2)ãšåãç¶æ
ã«ãªããŸãã
ãããã»ã¹ã®çµäºåã«èªåã§ãã¹ãŠfree()ããã°ãã©ã
ã«ãèµ·ãããªããŠããããšãã䞻匵ã®äººã§ããããå¯èœ
ã«ãªããããªmalloc()/free()ã®å®è£
ã瀺ãã人ã¯ãã
ã£ããããŸãã?
ã²ãšã€ã®èŠçŽ ãæ€çŽ¢ããŠè§£æŸããã³ã¹ããO(log n)ãšã
ããšãnåã®èŠçŽ ãããããO(n log n)ããª? logã®åºã¯
Bæšã®æ§é ã«ããã§ãããã
ãªããªãããäŸãåºãããŸããããããã«é§ç®æŒãã
ãããã»ã¹ã®çµäºæã«èªåã§ãã¹ãŠfree()ãã¹ãã ããš
ãèãã®äººã¯ãfree()ã«ã©ãã ãã®ã³ã¹ããããããã
ãŸãèããŠããã£ããããªãããã§ãã
free()ã§ã¯ãã以éã®malloc()ã§é åãåå©çšãããã
ãã«ã解æŸãããã¡ã¢ãªãŒãããªãŒãªã¹ãã«ã€ãªããªã
ãå¿
èŠããããŸããããã ãã§ãããããªã³ã¹ãããã
ãã®ã«ãå¹çãèããmalloc()/free()ããã±ãŒãžã ãšã
ã¡ã¢ãªãŒãæå¹ã«åå©çšãããããã«ãé£ç¶ããå°ããª
ãããã¯ããã¹ãŠè§£æŸãããããããããŸãšããŠã²ãšã€
ã®å€§ããªãããã¯ã«ãããããŸãã
ã€ãŸããmalloc()ã§ç¢ºä¿ãããã¹ãŠã®ã¡ã¢ãªãŒãåå¥ã«
free()ããã³ã¹ãã®ç·èšãã¯ã確ä¿ãããã¹ãŠã®ã¡ã¢ãª
ãŒããŸãšããŠè§£æŸããã³ã¹ããããé£èºçã«å€§ãããªã
ãšããããšã§ãã
ãããããããäžã®O(n log n)ã§å¹ããŠããããã§ãã
ããåå¥ã«free()ããã®ã¯ããããã³ã¹ããããªãããš
ããæèŠã«ã¯ç§ããŸã£ããå調ã§ããªãã§ãã
éã«ãããšãçµäºæã«ãã¹ãŠã®ã¡ã¢ãªãŒã解æŸããªãã
ã°ãªããªãç°å¢ã§ã¯ãmalloc()/free()ã€ã³ã¿ãŒãã§ãŒ
ã¹ã«ãåå©çšãèããã«ãã¹ãŠã®ã¡ã¢ãªãŒã解æŸããé¢
æ°ãåœç¶ååšãã¹ãã§ãããã䜿ãããšã®ã»ãããã¡ã
ã¡åå¥ã«free()ãåŒã¶ãã奚å±ãããã¹ãã§ããããã
ãããèšã£ãããexit()ã§ã¡ãããšããã°ã©ã ãçµäºãããã©ãã確èªã§ã
ãŸãã?ãfree()ã®å®è£
ãæ£ãããŠãmalloc()ãããã€ã³ã¿ãfree()ã®åŒæ°
ãšããŠæž¡ãããŠãã°ã解æŸãããã®ã¯ä»æ§ã§ãã(OSã«ã¡ãããšã¡ã¢ãªãæ»ã
ãã©ããã¯malloc()/free()ã®å®è£
ã«äŸåããåå¥ã®åé¡ã§ã)
ãå·èµåº«ã®ãã¢éããããäžã®ã©ã³ããã¡ãããšæ¶ãããã©ãã確èªã§ãã
ã®ãšäžç·ã§ããã
> malloc()ããã¡ã¢ãªãŒãã¹ãŠãfree()ããªãã§çµäº
> ããããã°ã©ã ã¯ãã©ãã«ãèµ·ããããšããä»æ§ã®
> malloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£
ããããšã¯
> malloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ã®ç§»æ€æ§ãäœäžã
> ããé倧ãªéå
> ã ãããšããããšã§ãã
å¿
ããã©ãã«ãèµ·ããèš³ã§ã¯ãªãããã©ãã«ãèµ·ãããŠããä»æ§ã©ããã«åããŠãããã
ãããããªãã
ãšããããšã§ãããã
ãããšããåæåãããŠããªããã€ã³ã¿ãŒã®ãããã¢ãã¬ã¹ã«æžã蟌ã¿ãè¡ã£ãå Žåãã©
ãã«ãèµ·ãããããªä»æ§ã¯ããã€ã³ã¿ãŒã®ç§»æ€æ§ãäœäžããããšãããããªæãã§æžãã
ã®ã§ããããïŒ
ãããfreeã䜿ã£ãŠã¡ã¢ãªãŒãã¡ãããšè§£æŸããªãããã«åäœããªãããšã¯ãCã®ã·ã¹ã
ã ãšããŠã¯åé¡ãããŸããããfreeã§ã¡ãããšè§£æŸããããšã«ãããã©ãã«ãèµ·ãããã
ãªããšã¯ãã·ã¹ãã ã«ç°åžžããªãéãèµ·ãããŸããã
èŠããã«ãfreeã䜿ããªãããã«ãã©ãã«ãèµ·ãã£ãå Žåã¯ãããã°ã©ããŒã®è²¬ä»»ã ãšæ
ããŸãã
> ã¡ãªã¿ã«ãANSI Cã§ã¯free()ã«ã€ããŠãé åã解æŸã
> ããããªãã¡ã以éã®malloc()ã§ãã®é åãåå©çšã
> ããããã«ããããšããæå³ã®ããšãæžãããŠããã¯
> ãã§ãã
確ä¿ãããŸãŸè§£æŸããªããã°ã空ãã¡ã¢ãªãæžããŸãã
> ããã°ã©ã ã§è²¬ä»»ããã£ãŠOSã«ã¡ã¢ãªãŒãè¿åŽããããš
> ã奚å±ãããªãããã®ããã®é¢æ°ã¯å°ãªããšãæ£ããè¿
> åŽã§ãããã©ãããè¿ãå€ãšããŠè¿ãä»æ§ã®ãã®ã§ãªã
> ãã°ãªããªãã§ããããfree()ã¯ããã§ã¯ãªãã
ãããããã§ããã
ãŸããè¿åŽã«å€±æãããŠãã©ããããããããŸãããã
ä»æ¹ãªãã§ããã
äŸã®å
±æã¡ã¢ãªã䜿ãã¿ã€ãã ã£ãããfreeã䜿ããªããšã倧å€ã§ããããäžå¿æžããŠã
ãã¹ãã ãšæããŸãã
> ãŽãã®æ¯å©ã§ããã°ãfree()ãããšããã®ã¯ãŽãç®±ã®ã»
> ãã«ãŽããæŸãæããŠãããšã¯ç¥ããã·ããããŠããã
> ããªãã®ã§ããfree()ã䜿ããããããã以äžã®ããšã¯
> ããããã§ããªãã®ã§ãã
ä»æ§ã§ã¯ãç§ãã¡ã¯ãã©ãããããšãã§ããŸãããã
Windowsã§ã¯ããããªãããªæ©èœããäžå¿æèŒãããŠããããã§ãã(mallocã§ã¯ãªãã§ã
ã)ã
Windowsã®ã·ã§ã¢ãäžçäžã§ããé㯠å·çããªããŠã¯ãããªã
人éã¯æ²¢å±±ããã§ãããããã
-----
Yasuhiro.Yao
ã¯ãŒãã³ã°ã»ããã倧ãããªããçµæãšããŠããŒãžéåããã£ãã·ã¥ãã¹
ãçºçããŠæ§èœãäœäžããçŸè±¡ã§ããã
æªäœ¿çšãªæªè§£æŸé åãå¢ãããšããããã£ãçŸè±¡ãèµ·ããŸãã
-----
Yasuhiro.Yao
> ãããããã®ã§ããè¿ãããããã«ãªã£ãŠãããªããã
> ã®ã¡ã¢ãªãŒãmalloc()ããããŸãŸã®ãã®ã§ãfree()ã§è§£
> æŸæžã¿ã®ãã®ã§ãåºå¥ãªãOSã«è¿ããããã«ãªã£ãŠãã
> ã®ãåœç¶ã§ãããã
ã»ããšã ãfree()ã§è§£æŸã§ãããšããããåœç¶exit()ã§ã解æŸã§ããã¯ãã§ã
ããã ãšãããšãfree()ããã»ããè¯ãå Žåããããããããšãããã
ããã°ã©ã ãç°åžžçµäºããŠfree()ãexit()ãåŒã°ããªããšãã¡ã¢ãªãOSã«è¿
åŽãããªãç°å¢ãããããã®æã®è¢«å®³ã軜æžããããããªãã¹ãæ©ãã«free()
ããŠããã
ãšããå Žåã§ããããã ãã ããã®ãããªç°å¢ã§å¯èœãªã®ã¯ããããã軜æžã
ã«ãããŸãããã(mallocããã¡ã¢ãªã䜿çšäžã«æ»ãã ãã©ããããããªã)ã
ãã®ãããªOSã§ã¯ãä¿¡é Œæ§ã®ããã¡ã¢ãªè¿åŽã¯æ¬è³ªçã«äžå¯èœãšããããšã«ãª
ããŸãããfreeããã³ãŒããå
¥ãããããšãã£ãŠå®å¿ã§ãããã®ã§ã¯ãªãã
ïŒ ãã£ã±ããå®è£ ã®è³ªãäœãããšããŠããããããããªãã®ã§ã¯ã
ãããã«ãããæšæºCã®ç¯å²ãå€ãã話ã§ããã(ç°åžžçµäºããæã«äœãå¯èœã
ãªããŠã移æ€æ§ã®é«ã話ãã§ããããããªãã)
åç°æŠåž
å¿ èŠãªãã®ã¯ç¥ã£ãŠããŸãã
> ãããªäººã°ã£ãããããã°ã©ãã³ã°ããŠããã®? Windows ã£ãŠ...
> ããããã¢ããªã±ãŒã·ã§ã³ãé
ããªãããã ã
ããã°ã©ã ã®ãšã©ãŒæ€åºããŒã«ã§ããNuâega瀟ã®âoundsâheckerãšèšã
éçºæ¯æŽããŒã«ã¯ãã¢ããªçµäºæã«è§£æŸããŠããªãã¡ã¢ãªé åãååš
ãããšãã¡ã¢ãªãªãŒã¯ããããšããŠèŠåããŸãã
ãŸããã©ãã ãã®äººãâoundsâheckerã䜿ã£ãŠãããããã¯ããããŸãããã
é«äŸ¡ãªåžè²©åãããããšã§ãããé¯èŠãã人ã¯å€ãã¯ãã§ãã
ãããŠãã®ç¶æ³ãèµ·å ãšããŠãçµäºæã«å
šãŠfreeãã¹ããšãããåžžèããçãŸããŠ
ããŸã£ããã
èšèªä»æ§ããOSã®ã¡ã¢ãªç®¡çã®ä»æ§ãŸã§æãäžããŠããšãèãã人ã¯å°
ãªãã§ããããåžè²©åããæ³åŸãã«ãªã£ãŠããŸããã§ãããã
ææåOSã®ãœãããŠã§ã¢äŒç€Ÿãç¯ãããããªã»ã»ã»ã»ã»ïŒç¬ïŒ
(Windows3.1ã®åŸéºçã£ãŠã®ãããããïŒããã€ã¯çµäºããã¢ããªã®æªè§£æŸ
é åã沢山ãããšãããæ©å«ãªãªãã«ãªããŸããã)
ã¡ã¢ãªãé£ãã€ã¶ããŠOSãä»ã®ãœãããŠã§ã¢ã®åäœã«åœ±é¿ãäžããªããã°
ããã§è¯ããšæããŸãããã
çµäºæã¯ããã®ã¢ããªã¯ãã以äžåäœããªãã®ã ãããOSã«ä»»ããŠããŸã£ãŠ
æ§ããªãã§ããã
-----
Yasuhiro.yao
> ã¡ã¢ãªç®¡çã®å®å
šæ§ã®åäžãšãé床ã®åäžã¯äž¡ç«ããŸãããããŠã
> ãããªã¡ã¢ãªç®¡çãšããã§ãªããã®ã®å·®ã¯ã2-3åãããšèšãããŠ
> ããŸãããããåçŽã«ãmalloc()/free() ããŠããã®ãªãã°ãæ ¹æ§
> ãå
¥ãæããå¿
èŠããããŸãããã€ãŸããªãæé©åããããæ¬è³ªçãããã
> ãªåé¡ã ããä»ã®ããã»ã¹ã«ã圱é¿ãåãŒããŸãã
>
> äŸãã°ãã¯ãã¹ãªãã¡ã¬ã³ã¹ã®ããã°ã©ã ãèããŸããããããã€
> ã¯ãã¯ãã¹ãªãã¡ã¬ã³ã¹ãäŸãã°B-Treeããªããã§äœã£ãŠãããŸãã
> ããã解æŸãããã®ã¯ãã¯ãã¹ãªãã¡ã¬ã³ã¹ãçµäºããŠã衚瀺ãçµ
> ã£ãæã§ãããããã¡ãããããã°ã©ã ã¯ãfree()ããã« exit()
> ããŠæ§ããŸãããããŠãããããfree() ããæã®èšç®éã¯ããã€
> ã§ãããã?
>
> exit()æã®äžèŠãªå
šé åã®free()ã¯ããèšç®éçã«ãã£ã¡ããããª
> ãããã°ã©ãã³ã°ãã§ãã£ãŠãããã°ã©ãã¯ããã®ãã㪠ããšã«é
> æã§ãã£ãŠã¯ãªããªãã¯ãã§ãã
>
> ãããæåã«ããŸãšããŠmalloc()ããŠãããããèªåã§ã»ã«ãäœã£ãŠ
> ãšã£ãŠããã°è¯ããæåŸãfree()äžçºã§è¯ããã§ãã? è¯ããšããã«
> æ°ä»ããŸããããããã¯ãmalloc()->sbrk()->exit() ã®åçºæã§ãã
ã·ã¹ãã ã«äŸåããç©ã§ãããç©ã«ãã£ãŠã¯æžããŠãããŸããã
> > > æ¬åœã«malloc()/free()ãšããååã§ãã§ãã? åŸåŠã®ããã«ã©ãããã·ã¹ã
> > > ã ãæããŠããã ããŸãã?
> > ããã©ã«ãããŒãã§Windowsã®HeapAllocãåŒãã§ããã ãã
> > ãã®APIã¯ãmallocãšåããããªæ¹æ³ã§ç¢ºä¿ããŠããã®ã§ãããŸãé
ããããŸããã
>
> æšæºCã®malloc/freeã®è©±ã§ã¯ãªããã§ãããããããã®ã¹ã¬ãããšã¯ç¡é¢ä¿ã§
> ããã
mallocãHeapAllocãåŒãã§ããç©ããããšããããšã§ãã
freeã¯ãHeapFreeãåŒãã§ããŸããã
次ãåãã§ãã
ïŒäžç¥ïŒ
> ãããªããšãã§ããããããªãããexit()æã«(ãããã¯mainããreturnãã
> æã«)ãã¹ãŠã®ã¡ã¢ãªãOSã«è¿ãããšããŠããã®ã§ã¯ã
ãããªã£ãŠããã°åé¡ã¯ãªãã®ã§ããããããªã£ãŠããªãç©ãããããå°ã£ãŠããã®ã§
ãããã
> malloc()/free()ãšããååã®é¢æ°ã®å®è£
ã®è©±ãããŠãããã§ããããã®ã¢ã
> ãªã±ãŒã·ã§ã³ããã£ããã©ãããé¢ä¿ã? å€ç°ããã¯ãã®ã¢ããªã±ãŒã·ã§ã³
> ã®äžã®malloc/freeã䜿ã£ãŠããã°ã©ã ãæžãã®ã§ãã? ãããŠãçãããè
> æ
®ããŠããã°ã©ã ãæžããªããã°ãããªããšããäºã§ãããã?
ããšãããã£ããœããã§ãç§ãäœã£ãç©ã§ã¯ãããŸããã
printfãgetcã«é¡äŒŒããé¢æ°ãå€æ°ãããæšæºé¢æ°ä»¥å€ã®ã«ãŒãã³ãå©çšãããŠããªãã
ãšãããCèšèªã§æžãããŠãããšæãããŸãã
ãã®ã³ãŒããçæããã·ã¹ãã ã®ã©ã€ãã©ãªã«å«ãŸããŠãããšèšãããšã§ãã
> > > > å®éã«ããã®ã§ãããããããã®ç°å¢ãèæ
®ããªããŠã¯ãªããŸããã
> > >
> > > æšæºCã«ãããã£ãŠæ£ããåããªãå®è£
ãæšæºCã®ç¯å²ã§ãèæ
®ãããã®ã¯äžå¯
> > > èœã§ãã
> > äžå¿ãé
ãã§ããæ£ããåããªãããšã¯ãªãã§ãããã
> äžã®ç§ã®æç« ã§ãæ£ããåããªãããšããèšèã¯(malloc/free/exitçã®)å®è£
> ã«ããã£ãŠããã®ã§ãã£ãŠããªãã®æžãããã°ã©ã ã®è©±ã§ã¯ãããŸããã
ç§ã®æžãããã°ã©ã ã®ããšã§ã¯ãªãã
(1)(2)ã®å®è£
ã®è©±ã§ãã
æžãããŠããç°å¢ããããŸããæžãããŠããªããšããã°ã
ããã¯ããã»ã¹çµäºæã«OSãåŸå§æ«ãšããŠè¡ãã¹ãåŠç
ã®ã²ãšã€ã§ãã£ãŠããŠãŒã¶ãŒããã»ã¹ã®åŽã§è¡ãåŠçã§
ã¯ãªãããã§ãããã
ãexit()ãè¡ããšãã®ããã»ã¹ãå ããŠããã¡ã¢ãªãŒç©º
éã¯ãã¹ãŠOSã«è¿åŽãããããšããã®ãšåãã¬ãã«ã®è©±
ã§ããããexit()ã®èª¬æã«ããããæžãããŠããªããŠã
ããããªããšã§ã¯ãããŸããã
> 移æ€æ§ãšã¯ãå¥ã®ç°å¢ã§åãéã§ã®æéãå°ãªãããšã§ãããå€ãã®ç°å¢ã§åãåé¢ã®ã
> ãã°ã©ã ããã®ãŸãŸåãã°ãéåžžã«é«ãããšã«ãªããŸãã
ããããåçŽãªèŠç¹ã§ã移æ€ããè¡ãããŠãããšããã
ãŠãŒã¶ãŒã«ãšã£ãŠã¯ããã¶ãäžå¹žãªè©±ã§ããã©ãã
åãåé¢ã®ããã°ã©ã ãããç°å¢ã§ã¯ãã
ãã¶ããªå¹ç
ã§åããã®ã®ãå¥ã®ç°å¢ã§ã¯ããããŠéå¹çãªåããã
ããšããã°ãããã¯ç§»æ€æ§ã®é«ãããã°ã©ã ã§ã¯ãããŸ
ãããã
移æ€ãããšã¯ãåãç®çã®ããã°ã©ã ãåããããªå¹ç
ã§åãããã«å¥ã®ç°å¢ã«ç§»ãå€ããããšã§ãããããã®
ããã«ã¯ããã°ã©ã ãæå³ããŠããåŠçå
容ã«å¯Ÿå¿ãã
é¢æ°ãæ°ããç°å¢ã§éžãã§å©çšããããšãå¿
èŠã§ãåã
ååã®äŒŒããããªåŠçãããé¢æ°ããã£ããšããŠããã
ããŠããã䜿ããªãããšããããŸãã
# ãã®ããã®å€æã®æ ãã©ãããšãªãã®ãæšæºèŠæ Œã§ã
# ã£ãŠãæšæºèŠæ Œã«åŸã£ãŠããªãç°å¢ã«ããã°ã©ã ã移
# æ€ãããšãã«ã¯ãçšæãããŠããé¢æ°ã®åãããã¹ãŠ
# èªåã§æ€èšŒããå¿
èŠãããããã§ãã
ãåãååã®äŒŒããããªåŠçãããé¢æ°ãã䜿ã£ããšã
ãããããšããããåãããŸã§ã®æéã¯å°ãªããªããã
ãããŸãããããã¡ãããšåãããŸã§ã®æéã¯ããã£ãŠ
å¢ããå¯èœæ§ãé«ãã§ãã
ãããã®ãããã§ããªãã
(C) ANSI CãšããŠãfree()ã¯OSã«ã¡ã¢ãªãŒãè¿ãé¢æ°ã§
ã¯ãªããmalloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ã«ã¡ã¢
ãªãŒãè¿ãé¢æ°ã§ããããšãæèšãããŠãã
ã瀺ããã°ããã§ããŸããªãã§ãããã
ãã®ãããªä»æ§ã®ã¡ã¢ãªãŒå²ãåœãŠ/解æŸã€ã³ã¿ãã§ãŒ
ã¹ããŸãšãã«äœ¿ããããã«ã¯ãããã»ã¹çµäºæã«ãã®ã€
ã³ã¿ãŒãã§ãŒã¹ãããããŠããã¡ã¢ãªãŒããäœããã®ã¡
ã«ããºã ã§ç¢ºå®ã«OSã«è¿åŽãããããšãæé»ã®ãã¡ã«ä¿
蚌ããå¿
èŠãããããã§ãã
ææ°ã®ANSI Cã¯èŠãŠããªãã®ã§ãããANSI Cæšæºèæ¡ã§
ã¯ãfree()ã¯æž¡ãããé åãdeallocateãããããªãã¡ã
以éã®malloc()ã§ãã®é åãåå©çšãããããã«ããã
ãšãã£ãæé¢ã«ãªã£ãŠããã¯ãã§ããdeallocateã®æå³
ã念ãæŒããŠãã®ããã«èšè¿°ãããŠããããã«ã¯ã(C)
ã瀺ãããŠãããšã¿ãŠããã§ãããã
ãªãã»ã©ãå
·äœçã«ã¯äœãšãããœããã§ãã? ãŸãããª
ã³ã¯ãããŠããã©ã€ãã©ãªãŒã®å
容ããã³ã³ãã€ã«ã«äœ¿
ã£ãåŠçç³»ã¯æšå®ã§ããŸããã?
ãããã«ããŠãããã®malloc()/free()ã¯ANSI Cæºæ ã®
é¢æ°ã§ã¯ãªããç¬èªä»æ§ã®ãã®ãšããŠå®è£
ãããŠããå¯
èœæ§ãé«ããã§ãã
> Windows ãåé¡ã«ãªã£ãŠããŸãããããã Win16 ãš Win32 ã§éããŸãã
> ãçµäºããŠã OS ã«è¿ããªãããšããæå³ã§ã®ã¡ã¢ãªãªãŒã¯ãçºçããã®ã¯
> åºæ¬çã«ã¯ Win16 ã ãã®ã¯ãã§ãïŒWin32 ã¯ãããªãã«(?)ãã£ããããŠ
> ããŸãïŒãããã®ç¹ã§åé¡ã«ãªãé¢æ°ã¯ malloc/free ã§ã¯ãªããã·ã¹ãã
> ã³ãŒã«ã® GlobalAlloc/GlobalFree ã®ã»ãã§ãã
> ïŒLocalAlloc/LocalFree ãšããã·ã¹ãã ã³ãŒã«ããããŸãã
ã§ããããmalloc/freeã®å Žåãã©ã€ãã©ãªã§ç®¡çããŠã䜿çšäžã®é åãOSã«
è¿ãã«ãŒãã³ãatexit()ã§ç»é²ããšãã°ããããã§ããã...
ãµã€ãŒèª°ãæžããŠãexit()æã«è¿åŽãããŸãããã
> Win16 ã§ã¯ã¡ã¢ãªç©ºéãããã»ã¹ããšã«ãã£ããåé¢ãããŠããŸããã®ã§ã
> äžæã« GlobalAlloc ã䜿ããšãçµäºããŠã解æŸãããªãã¡ã¢ãªãæ®ããŸãã
> ã¡ãªã¿ã«ãWin32 ã§ã¯ GlobalAlloc 㯠LocalAlloc ãšç䟡ãšãããŠããŸãã
ãŸããã·ã¹ãã ã³ãŒã«ãçŽæ¥äœ¿ãããªã¢ããã€(é«åºŠãª?)話ã ãšãããšããšã¢
ããã€ããšãèµ·ããŠããããããªãã§ãããã
åç°æŠåž
> ã·ã¹ãã ã«äŸåããç©ã§ãããç©ã«ãã£ãŠã¯æžããŠãããŸããã
ãç©ã«ãã£ãŠã¯æžããŠãããŸããã? ãŸãåœãåã§ãã
exitããåŸã«äœãèµ·ãããã¯Cã®å®åç¯å²ã®å€ã§ããããåºæ¬çã«ã¯æžããŠãª
ããŠãåœç¶ã§ããããããšãããšæšæºCã®ç¯å²ã§ã§ããããšãšèšãã°ãæšæºCã
æ³å®ããŠããæ©èœãæºããå®è£
ã§æ£ããåãããã°ã©ã ãæžãäºã ããããªã
ã§ããã
ãšããã§ãmalloc()ã§å²ãä»ããã¡ã¢ãª(Globalãªããšããšããã·ã¹ãã äŸå
ã®ã·ãã¢ããããªããŠ)ããexit()ã§ã¯è§£æŸãããªãä»æ§ã ãšæèšããããã¥
ã¡ã³ãã1ã€ã§ã瀺ããŠããã ããŸã?
åç°æŠåž
> ããã«è¶ã
ã
> ãæ¬åœã®ããã°ã©ãããšèããšã
> ãã¯Pascalãæžããªãããšç¶ããŠããŸãç§ã(^^;
> 解ããªã人ã®æ¹ãå€ããã ãããªããã®ãã¿ã
確ãã«ãFORTRAN ãªããããªè°è«ã¯èµ·ããªãã£ãã§ããããã(^_^)
# ç§ã¯ãPascal 倧奜ããªããããºããã°ã©ããŒã§ãã...ã
--
--------------
é»åã®ãéç®±
ue...@di.mbn.or.jp
(From: ããããŒã¯ã¹ãã 察çã®ãã -nospam ãä»å ããŠããŸãã)
--------------
> > ã§ã malloc 㯠free ãããä»æ§ãã®é¢æ°ã§ãã
> ããã¯æ£ãããªãã®ã§ã¯ãªãã§ãã?
> malloc()ãã·ã¹ãã ããã¡ã¢ãªãŒãããã£ãŠããé¢æ°ã§
> ããããšã¯æ£ããã®ã§ãããfree()ã¯ãã®éãããé¢æ°
> ã§ã¯ãªãã®ã§ããfree()ã¯ãããããã®ããšã§malloc()
> ãè¡ã£ããšãã«å確ä¿ãããã¡ã¢ãªãŒãçšæããé¢æ°ãš
> ãšãããã»ããããã§ãããã
free() ã®ä»æ§ã¯ãããŸã§ããmalloc() ããé åãéæŸããããã®é¢æ°ã§ãããå€
ãã®å®è£
ãã解æŸããé åãé åã OS ã«è¿ããªãããã«ãªã£ãŠãããããšèšã£ãŠã
free() ããé åããã€ãŸã§ãããã»ã¹ãææããŠãããšèšãä¿èšŒã¯ãããŸãããå®
éãVC++ 4.0 ã®ãã«ãã§ã¯ãfree() ãããš OS ã«ã¡ã¢ãªãŒãè¿ãããšããããšæžã
ãŠããŸãã(WindowsNT ã®ã¿ã§ããã©ãã)
> malloc()ãããã®ã¯ç¡æ¡ä»¶ã«free()ãã¹ãã ãšæããã
> ãšã¯ããã®é察称æ§ã«æ°ã¥ããã£ã³ã¹ã奪ãããšã«ãã€
> ãªãããŸãããã
malloc() ãããã®ã free() ãå¿ããããšã¯ãmalloc()/free() ã®å®è£
ãç¥ãã
ãšããéèŠã§ããããã ãããåå¿è
ã«ã¯ malloc() ãããã®ã¯åžžã« free() ãã¹ã
ãšæããã¹ãã§ãã
å®è£ ãç¥ã£ãŠããå¹çã®è¯ãããã°ã©ã ãæžãã®ã¯ãã®åŸã§è¯ããšæããŸãã
> ãŸããããã§ããmalloc()ãããfree()ããã°ããããããã£ãŠã®ã¯ã
> æ¬åœã¯ããããªã«ç°¡åãããªããã
åžžã«ãmalloc()/free() ã察ã«ã§ãããšã¯èšããŸããããã»ãšãã©ã®å Žåå¯èœã ãš
æããŸããã©ãèšãå Žåã«ãé£ããã®ã§ãããã ?
> å®éããã€ã(malloc() ãããªãã°ããã€ã free())
> ã£ãŠã®ãGCæãã«æ€èšŒããããšæã£ãããäžè¬çã«ã¯äžå¯èœã§ãã
ãã®æèŠã¯æ£ããã§ãããæ€èšŒã§ããªããã free() ããªããŠãè¯ããšèšãèš³ã§ã¯
ãªãã§ãããã
> (æ¬åœã®ããã°ã©ãã¯ããããã©ããã£ãŠä¿èšŒããŠããã®ã ãã?
> èªå㧠reference countããã£ãŠã®ã C++ ã§ã¯å€ãããããªãã
> ãªã)
ãããã³ã³ãã€ã©ã«ä¿èšŒãããããã«ãã³ã³ã¹ãã©ã¯ã¿/ãã¹ãã©ã¯ã¿ãããã®ã§
ããã§ãããŸã å¿é
ã ããæºäžãããã¯ãé·æéé£ç¶çšŒåè©Šéšãåžè²©ã®ã¡ã¢ãªãŒ
ãã§ãã«ããŒã«ãªããã§ããã®ãæ®éã§ããã
> ããããããGCã®æ£åœæ§ã蚌æããã»ããããããã£ããããŸãã
> ãã£ã¡ã¯ãããã°ã©ã ã®æ¯èã§ã¯ãªããŠãããŒã¿æ§é ã®æ£åœæ§ãšã
> ãŠç€ºããŸãããã
GC ãªããŠäœ¿ããªãç¶æ³ãšèšããã®ãç¥ããªãã®ã§ãããã ?
> > å°ãªããšããããã°ã©ãã³ã°ãšããä»äºã®çµæã®ææç©ã®å質ã«ã
> > 責任ãè² ãè·æ¥ããã°ã©ãããèŠãã°ããããªæ
床ã§ä»äºããã
> > ãªãèŸããŠãããšèšãããã§ããããã
> 確ãã«ãã¡ã¢ãªãªãŒã¯ãçãèŠãæ°ã¯ãªãã§ããããã¯ãã£ã¡ããããªã
> ããšãã§ãããããé²ãäžã€ã®æ¹æ³ãšããŠã
> block release reserve -> malloc in module
> module release -> block memory release
> ãšããææ³ããã£ãŠãmalloc -> exit ã£ãŠããã®ã¯ãããããå®å
šãµã€ã
> ã®ææ³ã®äžã€ã ãšææããããªã
ããèšãææ³ã«å ããŠãã¢ããªã±ãŒã·ã§ã³åŽã§ãã§ããã ãæåœãŠããã®ãæ®éã§
> OSãããã»ã¹ã«å²ãåœãŠãŠãããã®ã¯ã¡ã¢ãªãŒç©ºéã§ã
> ã£ãŠããã®äžã«ã¯ã³ãŒããããŒããããããã¹ãé åã
> ã³ãŒããæ¶è²»ããã¹ã¿ãã¯é åãããã»ã¹ãmalloc()ã§
> 䜿ãããŒãé åãªã©ãããããã§ãããã®äžãã©ã䜿ã
> ããšããã»ã¹ã®èªç±ã§ãããã»ã¹ã®çµäºæã«ã¯OSããã®
> ã¡ã¢ãªãŒç©ºéå
šäœãååãããã®ã§ããã?
æãã£ãããUnix ã Windows ããèããŠããªãæèŠã§ãããçµèŸŒã¿çšã® OS çã§
ã¯ããããŒãã®ç®¡çã¯å
šéšã¢ããªã±ãŒã·ã§ã³åŽã§ãã£ãŠããããšèšãã¹ã¿ã³ã¹ã®ãã®
ã¯å°ãªããªãã§ããã
> MAEDA Atusi <ma...@is.uec.ac.jp> wrote in message
> news:pypog9c...@tp560e.sowa.is.uec.ac.jp...
> > "Furuta" <furut...@ma2.justnet.ne.jp> writes:
>
> > > > æ¬åœã«malloc()/free()ãšããååã§ãã§ãã? åŸåŠã®ããã«ã©ãããã·ã¹ã
> > > > ã ãæããŠããã ããŸãã?
> > > ããã©ã«ãããŒãã§Windowsã®HeapAllocãåŒãã§ããã ãã
> > > ãã®APIã¯ãmallocãšåããããªæ¹æ³ã§ç¢ºä¿ããŠããã®ã§ãããŸãé
ããããŸããã
> >
> > æšæºCã®malloc/freeã®è©±ã§ã¯ãªããã§ãããããããã®ã¹ã¬ãããšã¯ç¡é¢ä¿ã§
> > ããã
> mallocãHeapAllocãåŒãã§ããç©ããããšããããšã§ãã
> freeã¯ãHeapFreeãåŒãã§ããŸããã
> 次ãåãã§ãã
ãªããšããåŠçç³»ã®ã©ãããããŒãžã§ã³ã§ããã? ã¡ã¢ãªèŠæ±ããŸãšããã«ã
ã®ãŸãŸHeapãªããšãããåŒãã§ããšããäºã¯ã©ããã£ãŠç¢ºèªãªãã£ãã®ã§ãã
ã?
> ïŒäžç¥ïŒ
> > ãããªããšãã§ããããããªãããexit()æã«(ãããã¯mainããreturnãã
> > æã«)ãã¹ãŠã®ã¡ã¢ãªãOSã«è¿ãããšããŠããã®ã§ã¯ã
> ãããªã£ãŠããã°åé¡ã¯ãªãã®ã§ããããããªã£ãŠããªãç©ãããããå°ã£ãŠããã®ã§
> ãããã
ãªããšããåŠçç³»ã®ã©ãããããŒãžã§ã³ã§ããã? exit()æã«ã¡ã¢ãªãè¿ããŠ
ããªããšããäºã¯ã©ããã£ãŠç¢ºèªãªãã£ãã®ã§ããã?
åç°æŠåž
> ãŸããããã§ããmalloc()ãããfree()ããã°ããããããã£ãŠã®ã¯ã
> æ¬åœã¯ããããªã«ç°¡åãããªãããå®éã
> ãã€ã(malloc() ãããªãã°ããã€ã free())
> ã£ãŠã®ãGCæãã«æ€èšŒããããšæã£ãããäžè¬çã«ã¯äžå¯èœã§ãã
> (æ¬åœã®ããã°ã©ãã¯ããããã©ããã£ãŠä¿èšŒããŠããã®ã ãã?
> èªå㧠reference countããã£ãŠã®ã C++ ã§ã¯å€ãããããªãã
> ãªã)
ç§ã ã£ããããããªæ¹æ³äœ¿ããŸããã
ããã°ã©ã ã®æã
ã§ãã°åããªããšãã©ãã§ããã¹ããŠããããããªããªããŸããã
ããšãçµæ§ãããããã¹ãããŠãåéšåããã§ãã¯ãããšãã»ãŒç¢ºå®ã§ãã
...
#include <malloc.h>
void * deb_malloc(size_t s);
{
void *pv;
pv = malloc(s);
//ããã§ãã¡ã€ã«ã«ãã°ãåãã
return(pv);
}
void deb_free(void *pv)
{
free(pv);
//ããã§ãã¡ã€ã«ã«ãã°ãåãã
}
#define malloc deb_malloc
#define free deb_free
...
> ãããã«ããŠãããã®malloc()/free()ã¯ANSI Cæºæ ã®
> é¢æ°ã§ã¯ãªããç¬èªä»æ§ã®ãã®ãšããŠå®è£
ãããŠããå¯
> èœæ§ãé«ããã§ãã
åäœãåãã§ãããã移æ€æ§ã¯å€§äžå€«ã ãšæããŸããã
> ãããããããã£ãŠãããã»ã¹çµäºæã«ãããfree()ã¯äžèŠã§ããããšããäžè¬è«ã
> å°ãåºãããã«ã¯ã
>
> (A) ãã¹ãŠã®OSäžã§ãmalloc()ã䜿çšããããŒãé åã¯ããã»ã¹çµäºæã«OSã«è¿ãã
> ããšããå®è£
ã«ãªã£ãŠããã
> (B) ANSI CãšããŠãmalloc()ãåãåºããé åã¯ããã»ã¹çµäºæã«(free()ããªããšã)
> OSã«è¿ãããããšãèŠæ Œã«æèšãããŠããã
>
> ã®ããããããåæ§ã«ç€ºããªããã°ãªããªãã§ãããã
free()ããŠãã ãã§ããäºãèšããŠããã®ã§ååã§ã¯ãªãã§ããããã
> ãªããªãã°ãå
èšäºã¯OSã®å称ã«ã¯èšåããŠããªããããç¹å®ã®OSã«äŸåããçãã
> åºããŠãäžè¬è§£ãšããŠã¯æ£ãããªãããã§ãã
free()ãããšæ£ããåãããexit()ã§ã¯æ£ããåããªãOSããããšèšãããšèª
äœç§ã¯ãŸã ä¿¡ããããªãã®ã§ããã(ããããå®è£
ã§ã¯free()ããŠããã¡ã§ãã
ã?)
ããšãããããå®è£
ããã£ããšããŠããå
ã®è³ªåè
ã®ãããªååŠè
(ããª?)ã«
察ããŠãæšæºçãªçãã§ãªã(ç¹æ®ãª|å£ã£ã|å€ä»£ã®|ä¿¡é Œã§ããªã)ç°å¢ã«ã
ãã(å¥ç¯ãª|äžåçãª|èŠçã«æºã¡ã|ãŸããªãããã)ããã°ã©ãã³ã°ç¿æ
£ãŸ
ã§èæ
®ããããã«åŒ·å¶ããã®ã¯ããŸãè¯ããªãäºã ãšæããŸãã
> ã©ã®OSã§åãåŠçç³»ããããããã«ãOSãé¢åã¿ãŠããããã ããäžèŠãªé¢æ°ãªã©
> åŒã³åºããªããŠãããã ãããšããæ
床ããšãããšãçµæ§ã§ããããã®ããã«ã¯ãæ¬
> åœã«ã©ã®OSã§ãé¢åã¿ãŠãããããšããè£ããšãããšãå¿
èŠã§ã¯ãããŸãããã
ããã§ãªãOSã¯ãæšæºçãªCã®å®åç¯å²ãå€ããã·ã¹ãã ãšããäºã«ãªããšæ
ããŸããããããå匷ããããšãããããªäººãæãåºããªãæ¹ãè³¢æã§ãããã
(Unixç³»ã®ãã¹ãŠã®OSãWindows95/98/NTãDOSã§ã¯å€§äžå€«ã¿ããã§ãããããŸ
ãŸã§å®éãã¡ãªOSã®å®äŸã£ãŠããã®ãæãã£ãŠããªã以äžããŸããããŠãã®ã
ã©ãããã©ãŒã ã§ã¯å€§äžå€«ãšæã£ãŠè¯ãã®ã§ã¯? ãã以äžã®å€ãªç°å¢ãžã®èæ
®
ãŸã§æ±ããã®ã¯é
·ã§ããã... å¥ã«ããã¹ãŠã®ç°å¢ã§ãã®ãŸãŸåããCããã°
ã©ã ãæžãããªã©ãšããã ãã£ãªéæã«çããŠãããã§ããªãããã§ããã)
åç°æŠåž
èšäº <88orp7$4hn$1...@news.lcv.ne.jp> ã§
furut...@ma2.justnet.ne.jpããã¯æžããŸãã
> ç§ã ã£ããããããªæ¹æ³äœ¿ããŸããã
> ããã°ã©ã ã®æã
ã§ãã°åããªããšãã©ãã§ããã¹ããŠããããããªããªããŸããã
> ããšãçµæ§ãããããã¹ãããŠãåéšåããã§ãã¯ãããšãã»ãŒç¢ºå®ã§ãã
(ç¥)
ããã®æ¹æ³ã ãšãfree ãããã€ã³ã¿ã®å€ã malloc ã§åå©çšããã(å¯èœæ§ã
ãã)ã®ã§ããã€ã³ã¿ã®å€ãèŠãŠããmalloc ãš free ã®å¯Ÿå¿ãã€ããããŸããã
ããšãã©ã㧠malloc / free ããã®ãããããŸãããããç§ã ã£ããã
void * deb_malloc(size_t s, char *file, int line);
{
void *pv;
pv = malloc(s);
//ããã§ãã¡ã€ã«ã«ãã°ãåãã
return(pv);
}
void deb_free(void *pv, char *file, int line)
{
// free(pv); ãããã°äžã¯ free ããªã
//ããã§ãã¡ã€ã«ã«ãã°ãåãã
}
#define malloc(s) deb_malloc((s), __FILE__, __LINE__)
#define free(p) deb_free((p), __FILE__, __LINE__)
ãšããŸããç倧ã«ã¡ã¢ãªãªãŒã¯ãèµ·ãããŸããããŸããããã°äžã¯ããããª
ãã§ãããâŠ
PROJECT TEAM ïœïŒ§ïŒ¡ é«æŽ¥æ£é ta...@doga.co.jp
TBD0...@nifty.ne.jp
PROJECT TEAM DoGAã®ããŒã ããŒãž â http://www.doga.co.jp/ptdoga/
2æ20æ¥(æ¥) ä»æ¥ã®ããŒãã£ãŒã®æ³å [ãããŠã£ããã®æ³å]
ã©ãžãªã®ã¹ã€ãããå
¥ãããšã倧奜ããªæ²ã®ãšã³ãã£ã³ã°ããã£ãŠããã
ã§ã¯ãªããŠãANSI Cã®ç¯å²ããèããŠããªããã§ããã§ã
ANSI Cã®ç¯å²ãªããå°ãªããšãã¡ã¢ãªãŒç®¡çã«ã€ããŠã¯
ããããä»æ§ãæ³å®ããŠåé¡ãªãã¯ãã§ãã
éANSI Cã®ç°å¢ãŸã§å«ããŠèãããªããmalloc()ããã
ã®ã¯free()ããã°è§£æŸãããããšããããšããä¿èšŒã§ã
ãŸãããã(ã²ãã£ãšãããã¯ãªãŒã³ã¢ããã®ããã«äœ
ãå¥ã®é¢æ°ãåŒã¶å¿
èŠããããã)ããããããfree()
ãã¹ãã ãã ãã§ã¯äžè¶³ã§ãããã
> çµèŸŒã¿çšã® OS çã§
> ã¯ããããŒãã®ç®¡çã¯å
šéšã¢ããªã±ãŒã·ã§ã³åŽã§ãã£ãŠããããšèšãã¹ã¿ã³ã¹ã®ãã®
> ã¯å°ãªããªãã§ããã
ããããããŒã管çã«malloc()/free()ãšããååãã€
ããŠãããšãããäžå¹žãªããšã§ãã
ãããšãã ããäœã ãšãã£ãããã®ã§ããã?
ANSI Cã®malloc()/free()ã®ä»æ§ã¯ããã®ä¿èšŒããªããŠ
ãããŸããªãããã«ã§ããŠããŸãã...ã
> malloc() ãããã®ã free() ãå¿ããããšã¯ãmalloc()/free() ã®å®è£
ãç¥ãã
> ãšããéèŠã§ããããã ãããåå¿è
ã«ã¯ malloc() ãããã®ã¯åžžã« free() ãã¹ã
> ãšæããã¹ãã§ãã
ããã¯ãã¡ããã§ãããããªããªã£ãæç¹ã§free()ãã
ããšã¯ããããŠéèŠã§ããã§ããããªããªãã®ãçµäºæ
ã§ããã°ããã®å Žåã¯çµäºããã°èªåçã«free()ããã
ã®ã§free()ã¯äžèŠã ããšæãããšããããšã§ãã
ãããã«ããŠããã©ããªãã®ãmalloc()ããŠããŠãã®ã
ã¡ã©ããfree()ããããããã°ã©ããŒã¯ã€ãã«ææ¡ããŠ
ããã¹ãã§ããã
ã§ãANSI Cã®ä»æ§ã«ã¯ãfree()ããªããã°ãªããªãããš
ã¯æžããŠãããŸããããæžãããŠããªãããšãŸã§èŠæ±ã
ããã®ãªããåäœãåãããšã¯ãããªãã®ã§ã¯?
ãªãéœã«ä¿èšŒãããŠããªããã°ãªããªãã®ã§ããã?
ANSI Cã¯ããã°ã©ã ãæžãåŽããã¿ãåäœãèŠå®ããä»
æ§èŠæ Œã§ãããããããå®çŸããããã«ã·ã¹ãã åŽã§å®
è£
ããªããã°ãªããªãä»æ§ã«ã€ããŠã¯ãæé»ã®ãã¡ã«å®
ããããŠããã°ãã
ãã¶ãã ãšæããŸããã
ã€ãŸãã
> (E) ANSI Cã®èŠæ Œã§ãäžèšã®åæã«åºã¥ããŠmalloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹
> ã¯å®è£
ãããªããã°ãªããªãããšããæ¡ä»¶ãæ瀺ããŠããã
æé»ã®ãã¡ã«ããã¯ç€ºãããŠããã ãããšããããšã§ãã
ANSI Cã
free()ã¯å€ãè¿ããªããšæžããŠãããŸãã
解æŸããããã©ããããã°ã©ã åŽãã確èªã§ããªãä»æ§
ã«ãªã£ãŠãããšããããšã¯ã確èªããªããŠããŸããªããš
ããããšãšè¡šè£äžäœã§ãããã
ãšããã£ãŠãæåã®èšäºã§ç§ã¯ãããã¯èšèªä»æ§ã®åé¡
ã§ã¯ãªããŠOSã®åé¡ã§ãããšãæžããŠãŸãããã(^_^;)
ãèšèªä»æ§(ANSI C)ã®åé¡ãã«èšæ£ããŠãããŸããå¹³ã«
ã容赊ãã(_ _;)
ãªãã»ã©ãããã¯ãmalloc()/free()ãçŽæ¥OSã®ã¡ã¢ãª
ãŒå²ãåœãŠ/解æŸã®ã·ã¹ãã ã³ãŒã«ãåŒãã§ããããã
ãã§ãçµäºæã«free()ããªããŠã倧äžå€«ãªäŸãã ã£ãã®
ã§ãããäºè§£ã§ãã倱瀌ããŸããã
ãç«ã¡æã¡ãâã倪åæã¡ãã§ããã(_ _;)
# ããããã«åŸç¥ããã®ãå°å®ããææããŠãããªããã
# èªåã§ãããªããšãããŠããŠã¯ãããŸãããã(^_^;)
# åŸç¥ãããããã
èšäº <88ousv$rdt$1...@maha2.doga.higashiyodogawa.osaka.jp> ã§
ç§ã¯æžããŸãã
> void * deb_malloc(size_t s, char *file, int line);
> {
> void *pv;
> pv = malloc(s);
> //ããã§ãã¡ã€ã«ã«ãã°ãåãã
> return(pv);
> }
> void deb_free(void *pv, char *file, int line)
> {
> // free(pv); ãããã°äžã¯ free ããªã
> //ããã§ãã¡ã€ã«ã«ãã°ãåãã
> }
> #define malloc(s) deb_malloc((s), __FILE__, __LINE__)
> #define free(p) deb_free((p), __FILE__, __LINE__)
> ãšããŸããç倧ã«ã¡ã¢ãªãªãŒã¯ãèµ·ãããŸããããŸããããã°äžã¯ããããª
> ãã§ãããâŠ
ãç§ã®æžããäžèšããã°ã©ã (ãšãããã©ã€ãã©ãª)ã§ã¯ãmalloc ã§ç¢ºä¿ãã
é åãéæŸããªããŸãŸããã°ã©ã ãçµäºãããŠãåé¡ãªãããšä»®å®ããŠããŸãã
ãä»åã®ã¹ã¬ããã§ã®è°è«ã«æ²¿ã£ãŠèãããšãç§ã¯ deb_free å
㧠free ã
ã¹ããã€ã³ã¿ãªã¹ããä¿æããŠ(atexitãªã©ã§)çµäºæã«ã¡ãããš free ãã
ã¹ããªã®ã§ãããã?⊠ãããªããšãããã®ã¯åãªãCPUæéã®ç¡é§ä»¥å€ã®
äœè
ã§ãç¡ããšç§ã¯æããŸãã
èšäº <88p1v8$7ch$1...@news.lcv.ne.jp> ã§
furut...@ma2.justnet.ne.jpããã¯æžããŸãã
> ããã«ã€ããŠã¯å€§äžå€«ã§ãã
> mallocããããšfreeã§è§£æŸããããŸã§ã¯ãå°ãªããšããmallocã§ç¢ºä¿ãããããšã¯ãããŸ
> ãããããé çªã§ããããŸãã
ããããã°äžãšããããšã¯ãmalloc ãš free ã察ã«ãªããã©ãããçããã
ããããªãã§ããããã
ãããšãã°ããã°äžã«
malloc: ptr=0xABCDABCD
free: ptr=0xABCDABCD
malloc: ptr=0xABCDABCD
free: ptr=0xABCDABCD
free: ptr=0xABCDABCD
ãšãªã£ãŠããå Žåã1ã€ç®ãš2ã€ç®ã®ã©ã¡ãã® malloc ã§ç¢ºä¿ãããã€ã³ã¿ã
äºéã« free ããŠããŸã£ãŠããã®ãããããŸããã
ãç§ããããããããã°ã©ã€ãã©ãªã䜿ãããšããããŸããããã®æã¯ã
free ãå¿ãã®ãã§ãã¯ããããŸãããéè€ free ã®ãã§ãã¯ã®æå³ã倧ã
ãã§ãã
In article <38AFBC4E...@anet.ne.jp> ,
ããã·ãŒ<nas...@anet.ne.jp> writes
># å人çã«ã¯ãç§åäœã®ããã©ãŒãã³ã¹ã®åäžã«è¡çŒã«ãªãããã°ã©ã ã§ãªãéã
># free()ãã掟ãããªã
ããã¯ãconstant order ãšæäœ linear order ãèŠæ±ããã¢ã«ãŽ
ãªãºã ãåºå¥ããåé¡ã§ãããããç解ããŠããªãããã°ã©ãã¯ã
ããã°ã©ã ãæžããŠã¯ãããªããšæããŸãã
ç§åäœ? ãŸããä»ã®äººã¯512MBã®ã¡ã¢ãªã䜿ãåã£ãããšããªãã®
ããç¥ããŸããããGbyteåäœã§çŽ°ããmalloc()ãããã®ãäžã€äž
ã€free()ãããäœãèµ·ãããäœéšããŠã¿ãã®ãããããããªãããªã
free() ã§ãå¥åŠãªé
ããã§ãã®ã¯ããããªã«äžæè°ãªããšã§ã¯ã
ããŸãããperformace test ãããããšããã人ãªãäžåºŠã¯èŠãã
ãšããããããªã?
>(1) free()ã®å¿
èŠæ§ã¯OSã«äŸåããã
>(2) äžåŸã«free()ããŠããã°ãã©ã®OSã§åãåŠçç³»ã«ã察åŠã§ããã
>ããåçãšããŠæ£ãããšæããŸãã
exit()ãšfree()ã®å¯Ÿå¿ã¯ãã©ã®å Žåã§ãäžæã ãšæããŸãããäŸãã
real-time OS ã ãããšãexit()ãšfree()ãå®è£
ãããªãããã®ã
ãã«äœãã®ãåžžèã§ããããã§ãªããã°ããã£ããäœã exit()
ãªãã ããããã...
In article <88opnf$3to$6...@newsjp.mbn.or.jp>, é»åã®ãéç®±writes:
> ãã®æèŠã¯æ£ããã§ãããæ€èšŒã§ããªããã free() ããªããŠãè¯ããšèšãèš³ã§ã¯
> ãªãã§ãããã
æ€èšŒã§ããªããªã(ã€ãŸãããã®ãã€ã³ã¿ã2床ãšäœ¿ããªãããšãä¿
蚌ã§ããªããªã) free() ãã¹ãã§ã¯ãããŸããããã£ãããŸãã ã
> ãããã³ã³ãã€ã©ã«ä¿èšŒãããããã«ãã³ã³ã¹ãã©ã¯ã¿/ãã¹ãã©ã¯ã¿ãããã®ã§
> ããã§ãããŸã å¿é
ã ããæºäžãããã¯ãé·æéé£ç¶çšŒåè©Šéšãåžè²©ã®ã¡ã¢ãªãŒ
> ãã§ãã«ããŒã«ãªããã§ããã®ãæ®éã§ããã
ãããªããšããªããŠããå¥ããã»ã¹ã«ããŠãexit() ããŠããã°ã
ããŒã£ãšå®å¿ã§ããŸããäœãããã®å Žåã¯OSçžå¯Ÿã§èšŒæãããŠãã
ããã ããã
> GC ãªããŠäœ¿ããªãç¶æ³ãšèšããã®ãç¥ããªãã®ã§ãããã ?
ãããããªãã real-time OS ã䜿ã£ãŠããŠãã©ãããŠã dynamic
memory allocation ãå¿
èŠã«ãªã£ããã©ããããã§ãã? åã ã£ããã
reference count ãªã㧠GC ããŸããããããmalloc()/free()
pattern ãåãã£ãŠæ±ºãŸã£ãŠãããªãããã㯠dynamic ãšã¯
åŒã³ãŸãããããreference count ã¯å®çšçãªGCã§ããäžèœã§ã¯
ãªããã©ããdynamic memory allocation 㧠GC ãäžèŠãªäŸã¯ã
exit() ã ãã§ããä»ã«ã¯ååšããŸããã
ã§ãããããreference count ã§ãå¿
ã free() ããããšã
èŠæ±ããã... ããã¯ãfree() ãããã³ã« swap-in ã®risk
ãè² ãããšã«ãªããŸããmemory allocation ã ãã«ãããã
ç°¡åã«èããã¹ãã§ã¯ãããŸãããããããããšæ°G byte
ããç¥ããªããã ãããããããã¯ãfree() ããã« exit()
ããæ¹ããå®å
šãã€é«éã§ãã
> > ãšããææ³ããã£ãŠãmalloc -> exit ã£ãŠããã®ã¯ãããããå®å
šãµã€ã
> > ã®ææ³ã®äžã€ã ãšææããããªã
> ããèšãææ³ã«å ããŠãã¢ããªã±ãŒã·ã§ã³åŽã§ãã§ããã ãæåœãŠããã®ãæ®éã§
> ãã
ããç¿ããå
šéšããŸããã« free() ããŠãããããã®åªåã¯ããŒ
ã§ãã
---
Shinji KONO @ Information Engineering, University of the Ryukyus
æ²³éçæ²» @ çç倧åŠå·¥åŠéšæ
å ±å·¥åŠç§
Shinji Kono wrote:
> >ãå€æ°ã代å
¥ãããfree()ã ã£ãããå®è¡æã§ãªããšfree()ãå¿
èŠãã©ãã
> >ããããŸãããã
>
> ããã¯ãã®éãã§ããããã§ãexit()ã®åã¯äžå¿
èŠãšãããã¯ãã§ãã
ãexit()ã®ã£ãŠãã³ã³ãã€ã©ã«ãœãŒã¹ãéãã¬ãŒã¹ããããã§ããïŒãæ¡ä»¶
åå²ããã£ããçµããããªã
> ã§ãåŸç¥ããã¯ã
>
> if(XXX_error()) {
> free_all(free_memory_list);
> exit();
> }
>
> ã¿ãããªã³ãŒããæ¬åœã«æžããŠãããã§ãã?
ããããå°ãªããšãããªã«ãã®ãšã©ãŒãæ€åºãããã ãã§ããããªãexit()
ãããããªããã°ã©ã ã¯(ä»äºã§ã¯)æžããŸããã
> ã ãšãããšãäŸã®ãä¿è·åœä»€éåããšãã«ã¯ã©ããã£ãŠå¯ŸåŠããã®ã§ããã?
> singal㧠trap ããŠãfree_all() ããŸãã? åã ã£ãããããªã..
ãäŸã®ãä¿è·åœä»€éåãã£ãŠãã£ããããã§ãã ãïŒ
ããã¡ã¢ãªäžè¬ä¿è·ãšã©ãŒãã¯ãã¢ã¯ã»ã¹ãèš±å¯ãããŠãªãé å(Windows95
/98ã§ã¯ã·ã¹ãã é åã§ãã£ãããã¡ã¢ãªãã¢ãã±ãŒããããŠãªãé å)ã«ãã
ã°ã©ã ãã¢ã¯ã»ã¹ããããšããå Žåã«çºçããŸãããããã°ã©ã ã«ãã°ããªã
ãã°èµ·ããããªãçš®é¡ã®ãšã©ãŒã§ãã
> >ããŸããã¡ã¢ãªãããã¯å
ãå
šãåç
§ããªãã§ãfree()ã ãè¡ãªãåŠçã§ã
> >ã©ãããŠå·šå€§ãªã¡ã¢ãªã swap-in ããã¯ãã«ãªãã®ã§ããããïŒãéåžžã
> >ã¡ã¢ãªã®è§£æŸã¯ãã¡ã¢ãªç®¡çããŒãã«ã®æžãå€ãã®ã¿ã§ããã®ã¡ã¢ãªé å
> >ã«ã¯ã¢ã¯ã»ã¹ããŸãããããã¹ã¯ãããçºçãããšã¯æããŸããã
>
> duplicate free() ããã§ãã¯ãããããªå Žåã¯ãfree()ã®æã«memory
> header ããããå Žåãæ®éã§ããããã觊ã£ãŠããŸãã§ãããã
> ãŸã£ãã觊ããªãå Žåããããšã¯æããŸããã§ããåã®ã¯ãã¹ãªãã¡ã¬ã³ã¹
> ã®ãããªå ŽåããMark and sweep ãããªããswapããã¯éããããŸããã
ãmallocã§å²ãåœãŠãŠè¿ãã¡ã¢ãªãšãã¡ã¢ãªç®¡çããŒãã«ãåé¢ããŠããã°ã
ã¡ã¢ãªé åã«ã¢ã¯ã»ã¹ããã«è§£æŸã§ããã®ã§ãã¹ã¯ããã¯èµ·ãããªãã§ã
ããããå°ãªããšããmallocãæåã®ããŒãã«ãšããŠmallocå°çšã®å€æ°é å
ã¯ååšããã¯ãã§ããããããã管çããŒãã«å°çšãšããŠããŸãããšã«äœã
äžéœåã¯ãããŸããã
> >ããã§ããã®ã¹ã¿ãã¯ã¯äœã¡ã¬ãã€ãçšåºŠã®èšæ¶é åããããã§ããããïŒ
>
> ãããäŸã®MSDOSã ãšæ°çŸãã€ããããªãã£ãŠãã€ã§ãã? alloca ãšã
> ãããªããŠãèªåã§çšæããã®ãæ®éããª?
ãæ°çš®ã®ã®ã£ã°ã§ããããïŒãMS-DOSã®ã¹ã¿ãã¯é åã¯ãEXEãããã«å¿
èŠ
ãµã€ãºãåãããŸããŠãããããŒãæã«èŠæ±ã«å¿ããŠåããã°ã©ã æ¯ã«å²ã
åœãŠãããŸããããã¡ãããäžã«ã¯æ°çŸãã€ãããå¿
èŠãšããªããã®ããã
ãããããŸããããåºæ¬çã«ãããã°ã©ã ãäœæããæãããã°ã©ãããªã³
ã«ã«æ瀺ããŠæ±ºå®ããŸãã
ããããããMS-DOSã®ã¹ã¿ãã¯é åãæ倧64KBãªã®ã¯ãi8086ã®ã¢ãŒããã¯
ãã£ããããå¶çŽã§ãããã©ããªOSã§ãããã€ã³ããªã¡ã³ãããCPUã®å¶çŽ
ãè¶ããããšã¯ã§ããŸããã
ããªãããããå æžç²ããŠããããæ¥é±ã¯åºåŒµãªãã§ã3æ以éããã®ã¹ã¬
ãããç¶ããŠãŠãæ°ãåãããæçš¿ããŸãã
+---------------------------------------------------------------------+
| From : Dairyo Gokan ( åŸç¥ å€§éµ ) |
| Org. : Hitmark Computer Corporation ( ãããããŒã¯ã³ã³ãã¥ãŒã¿ ) |
| Adrs : 13256 Northup Way Suite 3, Bellevue WA 98005 |
| TEL:425-649-8808 FAX:425-649-9001 mailto:na...@can.bekkoame.ne.jp |
+---------------------------------------------------------------------+
> (C) ANSI CãšããŠãfree()ã¯OSã«ã¡ã¢ãªãŒãè¿ãé¢æ°ã§
> ã¯ãªããmalloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹ã«ã¡ã¢
> ãªãŒãè¿ãé¢æ°ã§ããããšãæèšãããŠãã
>
> ã瀺ããã°ããã§ããŸããªãã§ãããã
>
> ææ°ã®ANSI Cã¯èŠãŠããªãã®ã§ãããANSI Cæšæºèæ¡ã§
> ã¯ãfree()ã¯æž¡ãããé åãdeallocateãããããªãã¡ã
> 以éã®malloc()ã§ãã®é åãåå©çšãããããã«ããã
> ãšãã£ãæé¢ã«ãªã£ãŠããã¯ãã§ããdeallocateã®æå³
> ã念ãæŒããŠãã®ããã«èšè¿°ãããŠããããã«ã¯ã(C)
> ã瀺ãããŠãããšã¿ãŠããã§ãããã
ã§ããã(C)ã®æç« ã¯
free()ã¯ãmalloc()ã§ååŸããé åãOSã«è¿ãé¢æ°ã§ã¯ãªãã
æ
ã«ãOSã«ã¡ã¢ãªãè¿ãç®çã§ããã°ã©ã çµäºæã«free()ãåŒã³åºã
å¿
èŠã¯ãªãã
ãšããåœé¡ã®æ ¹æ ã«ã¯ãªããŸããã<88i766$b01$1...@ns.src.ricoh.co.jp>ã§
çç±ãšããŠãã
malloc()ã§ååŸãããããŒãé åã¯ãããã»ã¹çµäºæã«OSã«ãã£ãŠåå
ãããã¡ã¢ãªç©ºéã«å«ãŸããŠããã
æ
ã«ãOSã«ã¡ã¢ãªãè¿ãç®çã§ããã°ã©ã çµäºæã«free()ãåŒã³åºã
å¿
èŠã¯ãªãã
ã®ããçŽæ¥ã®ãçç±ã«ã¯ãªããªãã§ãããã
ãªããªãã°ããããçç±ãšããããã«ã¯
> ãã®ãããªä»æ§ã®ã¡ã¢ãªãŒå²ãåœãŠ/解æŸã€ã³ã¿ãã§ãŒ
> ã¹ããŸãšãã«äœ¿ããããã«ã¯ãããã»ã¹çµäºæã«ãã®ã€
> ã³ã¿ãŒãã§ãŒã¹ãããããŠããã¡ã¢ãªãŒããäœããã®ã¡
> ã«ããºã ã§ç¢ºå®ã«OSã«è¿åŽãããããšãæé»ã®ãã¡ã«ä¿
> 蚌ããå¿
èŠãããããã§ãã
ãšããåæãæ瀺çã«ä¿èšŒãããŠããªããã°ãªããªãããã§ãã
éã«èšãã°ã(C)ãæ ¹æ ãšãããã®ã§ããã°ãé åºãšããŠãŸãå
ã«äžèšã®åæã
ä¿èšŒãããŠããããšã瀺ãã®ãå
ã§ãããã
äŸãã°ãä»æãã€ãã®ã¯
(D) ãã¹ãŠã®OSäžã®åŠçç³»ã§å®è£ ãããŠããããšãä¿èšŒãããŠãã
ãããã¯
(E) ANSI Cã®èŠæ Œã§ãäžèšã®åæã«åºã¥ããŠmalloc()/free()ã€ã³ã¿ãŒãã§ãŒã¹
ã¯å®è£
ãããªããã°ãªããªãããšããæ¡ä»¶ãæ瀺ããŠããã
ãããã§ãããä»ã«ããããããããŸããã
--
ããã·ãŒ
> èšäº <88orp7$4hn$1...@news.lcv.ne.jp> ã§
> furut...@ma2.justnet.ne.jpããã¯æžããŸãã
> > ç§ã ã£ããããããªæ¹æ³äœ¿ããŸããã
> > ããã°ã©ã ã®æã
ã§ãã°åããªããšãã©ãã§ããã¹ããŠããããããªããªããŸããã
> > ããšãçµæ§ãããããã¹ãããŠãåéšåããã§ãã¯ãããšãã»ãŒç¢ºå®ã§ãã
> (ç¥)
>
> ããã®æ¹æ³ã ãšãfree ãããã€ã³ã¿ã®å€ã malloc ã§åå©çšããã(å¯èœæ§ã
> ãã)ã®ã§ããã€ã³ã¿ã®å€ãèŠãŠããmalloc ãš free ã®å¯Ÿå¿ãã€ããããŸããã
> ããšãã©ã㧠malloc / free ããã®ãããããŸãããããç§ã ã£ããã
ããã«ã€ããŠã¯å€§äžå€«ã§ãã
mallocããããšfreeã§è§£æŸããããŸã§ã¯ãå°ãªããšããmallocã§ç¢ºä¿ãããããšã¯ãããŸ
ãããããé çªã§ããããŸãã
ãã®ã³ãŒãã¯ãåºæ¬çãªã³ãŒãã®ã¿æžããããããã®ãŸãŸã§ã¯ãåå²ããããœãŒã¹ã«å¯Ÿã
ãŠäœ¿ããŸããã§ãã
ãªã®ã§ãã©ã€ãã©ãªã«ã§ãããŠãããããŒãäœããšäŸ¿å©ã ãšæããŸãã
free()ã®ãããã¿ã€ãã¯
void free(void *);
ã§ãã
確èªãããããããŸããã
åç°æŠåž
> > æ²³é çæ²»@çç倧æ
å ±å·¥åŠã§ãã
>
> > ãŸããããã§ããmalloc()ãããfree()ããã°ããããããã£ãŠã®ã¯ã
> > æ¬åœã¯ããããªã«ç°¡åãããªããã
>
> åžžã«ãmalloc()/free() ã察ã«ã§ãããšã¯èšããŸããããã»ãšãã©ã®å Žåå¯èœã ãš
> æããŸããã©ãèšãå Žåã«ãé£ããã®ã§ãããã ?
ã°ã©ãæ§é ãæ±ãå Žå(GUIã䜿ã£ãããã°ã©ã ãªãæ®éã«åºãŠæ¥ãŸã)ãã€ãŸ
ãæšæ§é ã«äŒŒãŠããããããŒããå
±æãããããåã芪ãå
ç¥ãæããããã
ããšãããããå Žåã§ãã
> > å®éããã€ã(malloc() ãããªãã°ããã€ã free())
> > ã£ãŠã®ãGCæãã«æ€èšŒããããšæã£ãããäžè¬çã«ã¯äžå¯èœã§ãã
>
> ãã®æèŠã¯æ£ããã§ãããæ€èšŒã§ããªããã free() ããªããŠãè¯ããšèšãèš³ã§ã¯
> ãªãã§ãããã
å Žåã«ãã£ãŠã¯free()ããªããŠè¯ãããããªãæ¹ãè¯ãããšããããŸããã
äŸãã°ãäžè¿°ã®ã°ã©ãæ§é ãããã°ã©ã çµäºãŸã§ã»ãšãã©äžèŠã«ãªããªãå Žåã
> > (æ¬åœã®ããã°ã©ãã¯ããããã©ããã£ãŠä¿èšŒããŠããã®ã ãã?
> > èªå㧠reference countããã£ãŠã®ã C++ ã§ã¯å€ãããããªãã
> > ãªã)
>
> ãããã³ã³ãã€ã©ã«ä¿èšŒãããããã«ãã³ã³ã¹ãã©ã¯ã¿/ãã¹ãã©ã¯ã¿ãããã®ã§
> ããã§ãããŸã å¿é
ã ããæºäžãããã¯ãé·æéé£ç¶çšŒåè©Šéšãåžè²©ã®ã¡ã¢ãªãŒ
> ãã§ãã«ããŒã«ãªããã§ããã®ãæ®éã§ããã
ã³ã³ãã€ã©ã§ã¯äžå¯èœã§ããããã¡ã¢ãªãããã¯ãæããã€ã³ã¿ããã€ãŸã§ç
ããŠããã¯æ±ºå®äžèœã§ããããåžè²©ã®ã¡ã¢ãªãã§ãã«ããŒã«ã§ããã€ã³ã¿ã®ã°
ã©ãæ§é ããã©ã£ãŠããŸã åç
§ãããŠããã©ããæããŠãããç©ããããã§ã
ã? (ããã£ãŠGCãšåŒã¶ãã ãšæãã)
> > ããããããGCã®æ£åœæ§ã蚌æããã»ããããããã£ããããŸãã
> > ãã£ã¡ã¯ãããã°ã©ã ã®æ¯èã§ã¯ãªããŠãããŒã¿æ§é ã®æ£åœæ§ãšã
> > ãŠç€ºããŸãããã
>
> GC ãªããŠäœ¿ããªãç¶æ³ãšèšããã®ãç¥ããªãã®ã§ãããã ?
äŸãã°ã©ãããå Žåã§ããã?
ãŸããããããç¶æ³ãããããããç¥ããŸããããããã§ã©ãããããšã䞻匵
ãªãã£ãŠããã®ã§ããã?
åç°æŠåž
> free() ã®ä»æ§ã¯ãããŸã§ããmalloc() ããé åãéæŸããããã®é¢æ°ã§ãããå€
> ãã®å®è£
ãã解æŸããé åãé åã OS ã«è¿ããªãããã«ãªã£ãŠãããããšèšã£ãŠã
> free() ããé åããã€ãŸã§ãããã»ã¹ãææããŠãããšèšãä¿èšŒã¯ãããŸãããå®
> éãVC++ 4.0 ã®ãã«ãã§ã¯ãfree() ãããš OS ã«ã¡ã¢ãªãŒãè¿ãããšããããšæžã
> ãŠããŸãã(WindowsNT ã®ã¿ã§ããã©ãã)
ãã£ããããšããfree()ã®å®è£
ã«ãã£ãŠã©ããŸã§æ»ãã(OSã«è¿ãããããã»
ã¹å
ã§ä¿æãããŸãŸã)ãç°ãªããŸãã
ã§ãããã倪ç°ããã®ãã£ããã£ããšãããmalloc()ãOSããã¡ã¢ãªãæã£ãŠ
æ¥ããšããã®ã¯(OSãšåŒã¹ããã®ãããç°å¢ãªã)åžžã«æ£ããã§ãããfree()ã¯
ããã®éã®ãOSã«ã¡ã¢ãªãè¿åŽããé¢æ°ããšã¯èšããªã(å®è£
ã«ãã)ã§ããã?
> > malloc()ãããã®ã¯ç¡æ¡ä»¶ã«free()ãã¹ãã ãšæããã
> > ãšã¯ããã®é察称æ§ã«æ°ã¥ããã£ã³ã¹ã奪ãããšã«ãã€
> > ãªãããŸãããã
>
> malloc() ãããã®ã free() ãå¿ããããšã¯ãmalloc()/free() ã®å®è£
ãç¥ãã
> ãšããéèŠã§ããããã ãããåå¿è
ã«ã¯ malloc() ãããã®ã¯åžžã« free() ãã¹ã
> ãšæããã¹ãã§ãã
ããããæãæ¹ã ãšããfree()ãããšOSã«ã¡ã¢ãªãè¿åŽãããããšãã(äžè¬
çã«ã¯æ£ãããªã)èããæ€ãä»ããŠããŸããŸãããã(ãã®ã°ã«ãŒãã§ããã
æã£ãŠãããæ¹ãããã¿ããã§ãã)
> å®è£ ãç¥ã£ãŠããå¹çã®è¯ãããã°ã©ã ãæžãã®ã¯ãã®åŸã§è¯ããšæããŸãã
ç§èªèº«ã¯ãã¡ãã£ãšæ²³éãããšã¹ã¿ã³ã¹ãéã£ãŠããŠããexit()åã®free()ã¯
ãã¹ãã§ãªãããšãŸã§ã¯æããŸãããå Žåã«ãã£ãŠã¯ããŠè¯ãæããããšæãã
ãããããexit()åã ãããšäœã ãããšå¿
ãfree()ãã¹ãããšããåçŽãªèã
æ¹ã«ã¯å察ã§ãã
åç°æŠåž
ãããåã®èããŠããã®ã¯å¥ã«ç¢ºèªã§ãããã©ãããšããããšã
èããŠããããããªããã§ããã©âŠ
確èªããªããŠããŸããªãç°å¢ã®ããã®ã€ã³ã¿ãŒãã§ãŒã¹ã§ãããšããããšãã
äœããã®æç« ãšããŠæèšãããŠãããã©ããããšå°ããŠãããã§ããã
In article <pyp1z67...@tp560e.sowa.is.uec.ac.jp> MAEDA Atusi <ma...@is.uec.ac.jp> writes:
| ããŠããã¯æ±ºå®äžèœã§ããããåžè²©ã®ã¡ã¢ãªãã§ãã«ããŒã«ã§ããã€ã³ã¿ã®ã°
| ã©ãæ§é ããã©ã£ãŠããŸã åç
§ãããŠããã©ããæããŠãããç©ããããã§ã
| ã? (ããã£ãŠGCãšåŒã¶ãã ãšæãã)
Sun WorkShop 5.0以éã«å«ãŸãããããã¬dbxã®å®è¡ææ€æ»æ©èœ(RTC)ã§ã¯ãã¡
ã¢ãªã¹ãã£ã³ãè¡ã£ãŠã¡ã¢ãªãªãŒã¯ã®æ€æ»ãè¡ããšãããããŸãããªããªã
ããããã¯ã®å
é ãæããã€ã³ã¿ãæ€ç¥ãããããããã¯ã®å
éšãæããŠãã
ãã€ã³ã¿ãèŠã€ãã£ãå Žåãããå²ãåœãŠãããŠãããããã¯ãžã®å¯äžã®åç
§
ã¯ãã¬ãžã¹ã¿å
ã«å«ãŸããŠããŸãããšãã£ãå Žåã«ãšã©ãŒã»èŠåã¡ãã»ãŒãžã
衚瀺ãããããã§ãã
http://docs.sun.com/ab2/coll.442.1/SWKSHPDEBUG/@Ab2PageView/9207?Ab2Lang=ja&Ab2Enc=euc-jp
| ã? (ããã£ãŠGCãšåŒã¶ãã ãšæãã)
ã¡ãªã¿ã«ã¡ã¢ãªã¹ãã£ã³ãã³ã³ãµããªã®ãaccurate(exact)ãªã®ããããã¥ã¡
ã³ããèŠãŠãç¹ã«æžããŠãªãã£ãã®ã§åãããŸããã§ãããç¹ã«æžããŠãªãã£
ãŠããšã¯accurateãªã®ããªããã§ãããã£ãŠæ¢åã©ã€ãã©ãªãã€ããªãèãã
ãšãã«å¯èœãªã®ã ãããã
ããšäžã®URLã«ã¯ãããªèšè¿°ããããŸããã
> 解æŸãããŠããªããããã¯ããã¡ã¢ãªãŒãªãŒã¯ããšåŒã¶ããšããããŸãããã
> ãããã®å®çŸ©ã¯ããŸã䜿çšãããŸãããããã°ã©ã ãçæéã§çµäºããå Žåã§
> ããéåžžã®ããã°ã©ãã³ã°ã§ã¯ã¡ã¢ãªãŒã解æŸããªãããã§ããããã°ã©ã ã«
> ãã®ãããã¯ã«å¯Ÿãããã€ã³ã¿ãããå ŽåãRTC ã¯ãã®ãããªãããã¯ã¯ã¡ã¢
> ãªãŒãªãŒã¯ãšããŠå ±åããŸããã
--
§NTTSâFT æè¡éçºéšãšã¬ã¯ããããã¯ã³ããŒã¹æè¡ã»ã³ã¿ãŒ äžå æœ€äº Â§
PGP Key fingerprint = B7 C0 CB 1F 1C 88 69 2A 25 36 8A EE 93 A3 61 72
çå±±ïŒ ïŒ°ïŒŠïŒµãããããã«ã¡ã¯ã
> ïŒã¹ã¬ããã倧ãããŠãã©ã®èšäºã«ãã©ããŒãããè¿·ã£ãã®ã§ããããã
ã»ããšã§ããã¹ã¬ããã«ãªã£ãŠããŠãã§ããããŸã£ãŠããŸã(^â^)ã
> 確ãã«èŠæ Œã§ã¯èŠå®ãããŠããŸããããããã°ã©ã çµäºæã«è§£æŸããã
> ãšèããŠããã§ããããå°ãªããšããããããŠãª OS ãªãããããªãã¯
> ãã§ããã§ãªããã°ãããã°ã©ã ã匷å¶çµäºãããããªãããšã«ãªããŸ
> ãã
>
> ïŒãã¡ãããfreestanding environment ã¯èããŠããŸãã
> ïŒãããããmalloc()/free() ãå®çŸ©ãããŠãªãã®ã§ããã
ãã®ãšããã§ãããã§ããå®émalloc()/free()ã¯äžççžã§ã¯è§£æ±ºã§ããŸãã
(^â^)ãå€å䜿çšããŠããç°å¢ãã¢ããªããã®äœ¿çšæ¹æ³ãæ£ç¢ºãå€ããŠããŸããŸãã
OSãä»®æ³ã¡ã¢ãªãŒãã©ãæ±ã£ãŠãããã¢ããªã«ãšã£ãŠååãªå®¹éãšã¹ããŒããããã
ãªã©ã«ããããŸãã(^_-)ã
> >// å²ãåœãŠ
> > assert(pT); // å€éå²ãåœãŠã¯èš±ããŸãã
>
> ããã§ã¯ãpT ã NULLïŒã¡ã¢ãªãŒãå²ãåœãŠãããŠããªãæïŒã«ã¡ãã»ãŒ
> ãžãåºãŸããã©ããã
ãããŸããassertã®è«çãééãã¡ãããŸãã(^_^;)ãfree(NULL)ãèš±ãããŠã
ãäºã¯ç¥ã£ãŠããŸãã
éèŠãªäºã¯ãéæŸããããã€ã³ã¿ãNULLã«ã»ãããããäºã§äžæ£ãªã¢ã¯ã»ã¹ãèµ·ãã
äºãåãããããããäºã§ãã
Junn Ohta wrote:
> å
èšäºã®æ¹ã¯ããã®ãããªmalloc()ã®äœ¿ããããããŠã
> ããšãã«äœ¿ãçµãã£ãã¡ã¢ãªãŒãfree()ããªããŠããã
> ãšãå°ããªã®ã§ã¯ãªãã§ãããã
>
> ãããããã®ãããªããã°ã©ã ãé·ã
ãšçšŒåããããšã§
> ä»äºãçµããŠçµäºããçŽåã«ããããŸã§ç¢ºä¿ããŠããã
> ãŸããŸãšããã¡ã¢ãªãŒããã¹ãŠã®åå¥ã«è§£æŸããããšã
> æ¬è³ªçã«å¿
èŠãªã®ãããå°ããªã®ã ãšæããŸãã
>
ãã£ãããéãã§ããããã䜿ããªããªãããšå
èšäºã«æžããŠãã
ããã«ã䜿ãåãããã¬ããšãã話ãšéã£ãŠãåççã«
ã¡ã¢ãªãŒãªãŒã¯ã«ãªããã©ãããšããåé¡ã§ãã
çµè«ãšããŠã¯ãªããªããšããäºã§è§£æ±ºã§ããã
éã«èšãã°ã¡ã¢ãªãŒãªãŒã¯ãæå³çã«èµ·ããããã«ã¯
ã©ãããã®ã§ãããããæå³çã«ãã³ã°ã¢ãããããŠ
匷å¶çµäºãšãWindowsã®MMFãšãã§ãªããåã«malloc
ã䜿ãã ãã§æ£åžžã«çµäºããŠããéãã¡ã¢ãªãŒãªãŒã¯
ãèµ·ããããšã¯ãªãã¯ãã ãšããã®ãçæ§ã®è°è«ã®
äž»æšã®ããã§ãã®ã§ã
Arita
ãªããããå°ããªã®ãç解ã§ããŸãããææåãããŠã
ãããšãéèŠãªã®ã§ãã?
fj.os.ms-windows.programmingã§"exeãå°ããããã«ã¯ïŒ"ãšãã話é¡ãåºãŠãŸ
ããïŒ
æMSJ(MicrosoftJournal)ããªããã§ãã©ã€ãã©ãªãããããå®è£
ã®èªäœã®ãã®(
åæ©èœã®WinAPIã®ã©ãããŒ)ãšçœ®ãããããã£ãŠã®ããããŸãã
--
BERO
Console/Emulator Programming
http://www.geocities.co.jp/Playtown/2004/
be...@geocities.co.jp
>> >ããããOSã®äºãªã©ããåºãŠããŸãããèšèªã®ç¹åŸŽã®é«ã移æ€æ§ãçããããã«
>ã¯ã
>> >free()ã§ã¡ã¢ãªã¯è§£æŸãã¹ãã§ãããã
>>
>> ã§ã¯ãexit() ãããªãããšã§ããã
>ïŒäžç¥ïŒ
>ãã¯ãUNIXã«äŸåããŠãããããããŸãããã
exit() ã¯æšæºã©ã€ãã©ãªé¢æ°ã§ããã
--
çå±±ïŒ ïŒ°ïŒŠïŒµ
ãããããŠãçµã¿èŸŒã¿ç°å¢ã®ãããªç°å¢ã§ã¯äœ¿ããªãã
ã®ã§ããããšãèŠæ Œãäœãã§ãã¡ããšæèšãããŠãããã
ãšãå°ãã ã£ãã®ã§ãããã?
ããããããšãªããANSI Cã«ã¯ããã¹ãŠã®ã©ã€ãã©ãªãŒ
é¢æ°ã¯hosted environmentã«å¯ŸããŠå®çŸ©ããããfree-
standing environmentã§ã¯å®è£
å®çŸ©ãšãªãããšããæå³
ã®ããšãæžãããŠããŸããhosted environmentã¯argcã
argvã®åãæž¡ããªã©ããã€ãåŸããªããã°ãªããªãå¶çŽ
ãããã®ã§ãANSI Cä»æ§ãçµã¿èŸŒã¿ç°å¢ã®ããã®ãã®ã§
ãªãããšã¯æèšãããŠãããšèããŠãããšæããŸãã
ããã§ã¯äžè¶³ã§ãã?
>äŸã®å
±æã¡ã¢ãªã䜿ãã¿ã€ãã ã£ãããfreeã䜿ããªããšã倧å€ã§ããããäžå¿æžããŠã
>ãã¹ãã ãšæããŸãã
æ¬åœã«ããã®ãããªå®è£
ãããã®ã§ãããããããã¯ãã©ã®ãã㪠OSã
åŠçç³»ã§ãããããäžã€ã§æ§ããŸãããããå®äŸãæããŠäžããã
--
çå±±ïŒ ïŒ°ïŒŠïŒµ
>ã§ããããmalloc/freeã®å Žåãã©ã€ãã©ãªã§ç®¡çããŠã䜿çšäžã®é åãOSã«
>è¿ãã«ãŒãã³ãatexit()ã§ç»é²ããšãã°ããããã§ããã...
ããããå®è£
ã§ã¯ãŸããã§ããããäŸãã°ãabort() ãå®å¿ããŠåŒåºã
ããšãåºæ¥ãªããªããŸãã
>ãµã€ãŒèª°ãæžããŠãexit()æã«è¿åŽãããŸãããã
exit() ããã«çµäºããŠã倧äžå€«ãªããã«å®è£ ããããå¿ èŠããããŸãã
ããã°ã©ã ãçµäºããåŸã®ããšã¯ C ã®èŠæ Œã®ç¯çã§ã¯ãããŸããããã
ãããªã£ãŠããªããŠãèŠæ Œã«æºæ ããŠããªããšã¯èšããªãã§ããããã©ã
å®çšçãªåŠçç³»ã«ã¯ãªããªãã§ãããã
ïŒãã®èšäºã freestanding environment ã«ã€ããŠã¯è¿°ã¹ãŠããŸãã
--
çå±±ïŒ ïŒ°ïŒŠïŒµ
ããã§ãããããŸãŸã§åºãæ
å ±ããŸãšãããšãmalloc()
ããã¡ã¢ãªãŒãfree()ããã«çµäºããå Žåã
(a) UNIXãMS-DOSãWindows95/98/NTãªãã¡ã¢ãªãŒãªãŒ
ã¯ã¯èµ·ããªã
(b) ANSI Cãªãã¡ã¢ãªãŒãªãŒã¯ã¯èµ·ããªã
(c) (malloc()/free()ãšããé¢æ°ä»æ§ãã)ãŸãšããªOS
ãšãŸãšããªèšèªåŠçç³»ã®çµã¿åãããªãã¡ã¢ãªãŒãª
ãŒã¯ã¯èµ·ããªããšæåŸ
ããŠãã
(d) äžèšã®ãããã§ããªãç°å¢ã§ã¯ã¡ã¢ãªãŒãªãŒã¯ãèµ·
ããå¯èœæ§ããã(ãããfree()ãããããšãã£ãŠ
ã¡ã¢ãªãŒãªãŒã¯ãèµ·ããªããšã¯ä¿èšŒã§ããªã)
ãšãã£ããšããã§ããããã
> éã«èšãã°ã¡ã¢ãªãŒãªãŒã¯ãæå³çã«èµ·ããããã«ã¯
> ã©ãããã®ã§ããããã
飯å¶ããã®
<38AFB2D7...@mail.ecc.u-tokyo.ac.jp>
ã«ããã°ã
Windowsã®Win16 APIã§GlobalAlloc/GlobalFreeã䜿ãã
GlobalFreeãåŒã³å¿ããå Žå
ãããããã§ãããã ãmalloc()/free()ããã®APIãçŽ
æ¥åŒã³åºããããªã³ãŒããçæããåŠçç³»ããããã©ã
ãã¯äžæã§ãã
ãŸããå³æŸããã®
<88jtrm$898$1...@pooh.isoternet.org>
ã«ããã°ã
malloc()çžåœã®ãã®ãå
±æã¡ã¢ãªãŒã®å²ãåœãŠãšç䟡
ã«ãªã£ãŠããOSããã
ããã§ããã®ç°å¢ã§free()çžåœã®é¢æ°ãåŒã³å¿ãããšã¡
ã¢ãªãŒãªãŒã¯ãèµ·ããå¯èœæ§ããããŸãããã ãå³æŸã
ãã®èšäºã«ã¯ã¡ã¢ãªãŒãªãŒã¯ãèµ·ãããšæžãããŠããã
ãã§ã¯ãããŸããããŸããçšæãããŠããé¢æ°ã¯malloc
()/free()ã§ã¯ãªãå¯èœæ§ããããŸãã
é»åã®ãéç®±ããã®
<88opnc$3to$3...@newsjp.mbn.or.jp>
ã«ããã°ã
çµã¿èŸŒã¿çšã®OSã§ã¯ãããŒãã®ç®¡çã¯å
šéšã¢ããªã±ãŒ
ã·ã§ã³åŽã§ãã£ãŠãããšèšãã¹ã¿ã³ã¹ã®ãã®ãå°ãªã
ãªã
ããã§ãããã®ç°å¢ã§free()çžåœã®é¢æ°ãåŒã³å¿ãããš
ã¡ã¢ãªãŒãªãŒã¯ãèµ·ãããã§ãããããçšæãããŠãã
é¢æ°ã¯malloc()/free()ã§ã¯ãªãå¯èœæ§ããããŸãããŸ
ãããã®ãããªç°å¢ã§ã¯malloc()/free()ãANSI Cä»æ§
ã«æºæ ããŠããå¯èœæ§ã¯ãªãããã§ãã
ããã«ãå€ç°ããã®
<88nccc$ipm$1...@news.lcv.ne.jp>
ã«ããã°ã
ãfreeã䜿ããªããšããã©ãã«ãèµ·ããã·ã¹ãã ã¯ã
çŸã«ãããŸãã
ã ããã§ããã®ããã©ãã«ããã¡ã¢ãªãŒãªãŒã¯ãæããŠ
ãããšæãããŸãããã ãå€ç°ããã¯å
·äœçã«ã©ã®ã·ã¹
ãã ã§ãããªãã®ãã¯ç€ºããŠããŸãããã¹ã€ã®ãšããã§
Windowsã®HeapAllocãmalloc()ãšããååã§åŒã³åºããŠ
ããã¢ããªã±ãŒã·ã§ã³ããããšãæžãã§ããããããã¯
free()ããªããŠãåé¡ãèµ·ããªããããªã®ã§ãäžãšã¯é¢
ä¿ãªãããã§ãã
ããšãããã€ãã®èšäºã§Windowsã®ãªãœïŒã¹ãªãŒã¯ã®è©±
ãåºãŠããŸããããã¡ã¢ãªãŒãªãŒã¯ãšã¯é¢ä¿ãªããããª
ã®ã§çç¥ããŸãã
ãšããããã§ããŸã
ã»ã¡ã¢ãªãŒå²ãåœãŠ/解æŸã«malloc()ãšfree()ã䜿ã
ã»malloc()ã§å²ãåœãŠãã¡ã¢ãªãŒãfree()ã§è§£æŸããã«
çµäºãããšã¡ã¢ãªãŒãªãŒã¯ã«ãªã
å®äŸã¯ã²ãšã€ã瀺ãããŠããŸããã
# ãfreeã䜿ããªããšããã©ãã«ãèµ·ããã·ã¹ãã ã¯ã
# çŸã«ãããŸãããšãæžãã«ãªã£ãå€ç°ãããå®äŸãã
# 瀺ãã«ãªããªãã®ã¯ãããã¶ãå¥åŠãªè©±ã§ããã©ãã
> ãšãªã£ãŠããå Žåã1ã€ç®ãš2ã€ç®ã®ã©ã¡ãã® malloc ã§ç¢ºä¿ãããã€ã³ã¿ã
> äºéã« free ããŠããŸã£ãŠããã®ãããããŸããã
>
> ãç§ããããããããã°ã©ã€ãã©ãªã䜿ãããšããããŸããããã®æã¯ã
> free ãå¿ãã®ãã§ãã¯ããããŸãããéè€ free ã®ãã§ãã¯ã®æå³ã倧ã
> ãã§ãã
ããã¯ãããŸããã
ãŸããã¡ã¢ãªç¢ºä¿ã ãã®ãã°ã§ã¯ãã©ãããããããã¯ããããªãã®ã§ãäœçœ®ãç¹å®ãã
ã«ã¯ãä»ã®æã§ããã°ãåããªããŠã¯ãªããŸããã
> > ãšããŸããç倧ã«ã¡ã¢ãªãªãŒã¯ãèµ·ãããŸããããŸããããã°äžã¯ããããª
> > ãã§ãããâŠ
>
> ãç§ã®æžããäžèšããã°ã©ã (ãšãããã©ã€ãã©ãª)ã§ã¯ãmalloc ã§ç¢ºä¿ãã
> é åãéæŸããªããŸãŸããã°ã©ã ãçµäºãããŠãåé¡ãªãããšä»®å®ããŠããŸãã
人ã®ç°å¢ã§ã¯ãã©ããªããããããŸããããèªåã®ç°å¢ãªã倧äžå€«ãªäºãããã£ãŠãŸãã
ããã
ã§ããïŒã€åé¡ããããŸããã
ã¡ã¢ãªäžè¶³ã®å±éºãããããšã§ãã
ãµã€ã倧äžå€«ã§ããã
> ãä»åã®ã¹ã¬ããã§ã®è°è«ã«æ²¿ã£ãŠèãããšãç§ã¯ deb_free å
㧠free ã
> ã¹ããã€ã³ã¿ãªã¹ããä¿æããŠ(atexitãªã©ã§)çµäºæã«ã¡ãããš free ãã
> ã¹ããªã®ã§ãããã?⊠ãããªããšãããã®ã¯åãªãCPUæéã®ç¡é§ä»¥å€ã®
> äœè
ã§ãç¡ããšç§ã¯æããŸãã
å ¬éæã«ã¯ãåãå»ã£ãŠããŸãã®ã§ãfreeããªããŠãååã ãšæããŸãã