ã¡ãã£ã¨åã®ãã¨ã§ãããè³¼å ¥ãã¦èªã¿ã¾ããã
ã§ãä»åã¯ãç¹ã«ãã®æ¬ã®ã¬ãã¥ã¼ã¨ãã§ã¯ãªãã¦ã
ããããæ¬ã ã¨ãã¸ã£ã³ã«ãã¸ã£ã³ã«ã ãã«ããèªåã ã£ããã©ãæ¸ãããã¨ããè¦ç¹ã§èªãã§ãã¾ãã¾ãã
Javaããã°ã©ãåãã®Cã®å ¥éæ¸ãªãããJavaã§ããæ¸ãã¨ããããCã ã£ããããæ¸ããã¨ããå½¢ã«ãªãã ãããããç§ãæ¸ããªãâ¦â¦
- é
åã¯ãJavaãªã
int[] a = new int[size];
ãã¨æ¸ãã¨ãããCã§ã¯ããæ¸ããã¨ãã
ãint *a = malloc(sizeof(int) * 10);
- ã¯ã©ã¹ã¯ãªãã®ã§ããã¼ã¿ã ãåºãã¦æ±ããããªãæ§é ä½ã使ã£ã¦ãnewã®ä»£ããã«malloc()使ã£ã¦æ¸ããã¨ãã
- ã¡ã½ããããããã¨ãããããã°ç¬¬1å¼æ°ã«ãªãã¸ã§ã¯ãã¸ã®ãã¤ã³ã¿ãæã£ã¦åãããã«ãã¦ããã¨ã¯é¢æ°ã®å½åè¦åã§ãªãã¨ããããã¨ãã
- ç¶æ¿ã¯ãªãã®ã§ããã¼ã¿ã¡ã³ãã®è¿½å ã ãããããã®ãªããæ§é ä½ã¨å
±ç¨ä½ã¨åæåã®ã³ã³ãã使ããã¨ãã
- ã§ãããªã¢ã«ãã£ãºã ã¯ããããããã¨ã*1ã
- malloc()ãããã®ã¯Javaã¨éã£ã¦free()ããªããã°ãªããªãããã¨ã«ããæ°ãã¤ãããã¨ãã
- Javaã¨éã£ã¦é
åã®ç¯å²ãã§ãã¯ã¨ãããªãããã¨ã«ããæ°ãã¤ãããã¨ãã
- é åç ´å£ãèµ·ãããã¨ãã«ã¯ã¨ã«ããä½ãããããããããªãããã¨ã«ããæ°ãã¤ãããã¨ãã
ââã¨ããã話ãæ¸ããå¾ã§ããCã§ã¯ãé åãæ§é ä½ã®ãªãã¸ã§ã¯ããã¹ã¿ãã¯ãstaticã«ç¢ºä¿ãããã¨ãã§ãããã¨ãã話ã«å ¥ããã¡ã¢ãªä¸ã®é ç½®ã®ã¤ã¡ã¼ã¸ãªãããæ¸ãã¦ããããããªãããªããã¨æãã¾ãã
ãããä¸è¬ã«ãAããã°ã©ãã®ããã®Bè¨èªå ¥éãã¿ãããªæ¬ã«ããã¦ããAã§ããæ¸ãã¨ããããBã ã£ããããæ¸ããçãªèª¬æã«ãã¦ãã¾ãã¨ãAã®æµåã§Bãæ¸ãã¦ãã¾ããããããããªããã¨ãããã¨ã¯è¨ããã¨æãã¾ãããCOBOLããã°ã©ãã®ããã®Javaå ¥éããªãã¦æ¬ãããã®ãã©ããç¥ãã¾ããããããããæ¬ããAã§ããæ¸ãã¨ããããBã ã£ããããæ¸ããçãªæ¸ãæ¹ã«ãã¦ãã¾ãã¨æ²æ¨ãªãã¨ã«ãªãããã§ãã
ããããLeptonãããããæãããã®ã§ãããããJavaããã°ã©ãã®ããã®Cä½æ³èã®å·»ãã§ã¯ãmalloc()ã®ã¾ã¨ããªèª¬æã¯ã8ç« ã©ã¤ãã©ãªãã¾ã§åºã¦ããªãã¨ããå¾¹åºã¶ãã§ãã確ãã«ãã®æ¬ã¯ãCä½æ³èã®å·»ãã§ããããããããã¹ã¿ã³ã¹ãæ£ããããã«ãæãã¾ãã
ãã ãç§èªèº«ã«ã¤ãã¦è¨ãã¨ããã¨ãã°crowbarãDiksamã®ã½ã¼ã¹ã³ã¼ãã«ããã¦ãåºå®é·ã®é åãªãã¦ã»ã¨ãã©ä½¿ã£ã¦ããããã»ã¨ãã©malloc()ã§ç¢ºä¿ãã¦ãã¾ããæ示çãªãã¤ã³ã¿æ¼ç®ã使ããã常ã«æ·»ãåã§ã¢ã¯ã»ã¹ã§ããç§ã®å ´åãèªåã§æ¸ãCã®æµåããã»ã¨ãã©Javaã®æµåã¨å¤ãããªããã®ã«ãªã£ã¦ããããã«æãã¾ã*2ã
Cã®æ¸ãæ¹ã¨ãã¦ã¯ãç§ã®æ¹ãéªéã§ããããå®ã¯ãã¾ã©ãä¸éã§ã¯ãCãªãã使ãã®ã¯ã¡ã¢ãªããããéãããçµã¿è¾¼ã¿ç³»ã¨ãã°ããã§ãmalloc()ãªãã¦ã¨ãã§ããªããã¨ã?
以ä¸ãã¨ãã¨ãã®ãªã話ã§ããã
*1:é¢æ°ã¸ã®ãã¤ã³ã¿ã«ããç¡çç¢çããªã¢ã«ãã£ãºã ã¯ãããã«æããã¹ãã§ã¯ãªãã
*2:Javaã§ã¯ã§ããªããæ§é ä½ã®ã¡ã³ãã«æ§é ä½ã®å®ä½ãå«ãããã¨ããæ§é ä½ã®å®ä½ã®é åãä½ã£ããã¨ãã¯ãã¾ããã©