[i=s] ±¾Ìû×îºóÓÉ halazi100 ÓÚ 2014-11-27 16:24 ±à¼ [/i]
malloc()º¯ÊýÊÇÓÃÀ´ÔÚÓû§¿Õ¼äÊÖ¶¯·ÖÅäÄÚ´æµÄº¯Êý£¬ÉêÇëµÄÄڴ潫±»·ÖÅäµ½ÄÚ´æµÄ¶ÑÇø£¬²»»á±»×Ô¶¯»ØÊÕ£¬ÐèÒª³ÌÐòÔ±ÊÖ¶¯Ð´º¯Êý£¨free()£©»ØÊÕ¶ÑÇøµÄÄڴ棻
ÄÇô£¬ÔÚLinuxÖÐÿ¸ö½ø³Ì¶¼ÓÐÒ»¸ö¶ÀÁ¢µÄÐéÄâÄÚ´æ¿Õ¼äµÚ0-3GÊôÓÚÓû§¿Õ¼ä£¬µÚ3-4GÊôÓÚÄں˿ռ䣨ËùÓнø³Ì¹²ÏíµÄ£©£»
ÎÊÌâÊÇ£º
1. ½ø³Ì½áÊøʱmalloc()·ÖÅäµÄ¿Õ¼ä»¹´æÔÚÂð£¿ÎÒÃÇ»¹ÓÐûÓбØÒªµ÷ÓÃfree()º¯ÊýÊÍ·ÅÄÚ´æ
2. ½ø³Ì½áÊøʱ£¬¶ÔÓ¦µÄÐéÄâÄÚ´æ¿Õ¼ä»¹´æÔÚÂð£¿ÐéÄâÄÚ´æ¿Õ¼äÖеÄÄÚ´æÇøÓòÊDz»ÊǾͲ»´æÔÚÁËÄØ
halazi100 ÓÚ 2014-11-27 16:36:57·¢±í:
Èç¹û˵ÄãmallocÁËÊý¾Ý
ÔÚ½ø³ÌÍ˳öÇ°×Ô¼ºÃ»ÓÐfree
ÄÇôһ°ã²Ù×÷ϵͳ¶¼»á°ïÄãÊͷŵÄÎÞÂÛÊÇLINUX or WINDOWS
½ø³Ì¹ÜÀíµÄÔÀí¶¼ÊÇÒ»ÑùµÄ
ÔÚ½ø³ÌÍ˳öµÄʱºò²Ù×÷ϵͳ×Ô¶¯»áÊͷŽø³ÌÕ¼ÓõÄ×ÊÔ´ÆäÖаüÀ¨¶ÑÄÚ´æ
Õâ¸öûÎÊÌâµÄ
µ«ÊÇ×÷Ϊ³ÌÐòÔ±ÄãÓ¦¸Ã×ñÊØËÉêÇëËÊͷŵÄÔÔò
²»ÒªÈñðÈË°ïÄãÈ¥²Áƨ¹É
¶øÇÒÄã²»ÊÍ·Å,ÔËÐÐʱ¶à´Îµ÷ÓûᵼÖÂÐéÄâÄÚ´æÓùâ¶øϵͳÂýµ½ËÀ
halazi100 ÓÚ 2014-11-27 16:33:07·¢±í:
[i=s] ±¾Ìû×îºóÓÉ halazi100 ÓÚ 2014-11-27 16:34 ±à¼ [/i]
1. ½ø³Ì½áÊøʱmalloc()·ÖÅäµÄ¿Õ¼ä»¹´æÔÚÂð£¿ÎÒÃÇ»¹ÓÐûÓбØÒªµ÷ÓÃfree()º¯ÊýÊÍ·ÅÄÚ´æ
´ð£º
½ø³Ì½áÊøʱÈç¹ûûÓлØÊÕ£¬ÓÐÐéÄâÄÚ´æ¹ÜÀí»úÖƵÄϵͳµÄMM»á°ïÖúÇåÀí£¬»ØÊÕϵͳµÄËùÓÐ×ÊÔ´£»
Èç¹û½ø³ÌûÓнáÊø£¬²»¶ÏµÄÉêÇëÄڴ浫²»ÊÍ·Å£¬¾ÍÊôÓÚÄÚ´æй©£¬ÏµÍ³²»»áÊÍ·Å£»
¶ÔÓÚÆÕͨµÄÓû§³ÌÐòÄÚ´æй©ûʲô´óÊ£¬ÒòΪÓû§µÄ½ø³Ì¿ÉÄܺܿì¾Í½áÊøÁË£¬µ«¶ÔÓÚÒ»¸ö·þÎñ½ø³Ì¾ÍºÜΣÏÕÁË£¬·þÎñ½ø³ÌÒ»°ã²»»á½áÊø£¬ÕâÑùÄÚ´æй©¾Í»á¶Ñ»ý£¬Ö±µ½ºÄ¾¡ÏµÍ³×ÊÔ´£¬µ¼ÖÂϵͳ±ÀÀ££»ËùÒÔ»¹ÊÇÒªÓÃfreeÊͷŵģ¬ÕâÊÇÒ»¸ö¹æ·¶£¬µ«ÊÇÄã²»ÊÍ·Å£¬Ò²²»»áǹ±ÐÄ㣬²»¹ýÊÇϵͳ±ÀÀ£¶øÒÑ£»
2. ½ø³Ì½áÊøʱ£¬¶ÔÓ¦µÄÐéÄâÄÚ´æ¿Õ¼ä»¹´æÔÚÂð£¿ÐéÄâÄÚ´æ¿Õ¼äÖеÄÄÚ´æÇøÓòÊDz»ÊǾͲ»´æÔÚÁËÄØ
´ð£º
½ø³Ì½áÊøʱ£¬ÏµÍ³»á»ØÊÕ½ø³ÌµÄËùÓÐ×ÊÔ´£¬ÐéÄâÄÚ´æ¿Õ¼äÏûʧ£»µ±È»ÐéÄâÄÚ´æ¿Õ¼äÖеÄÄÚ´æÒ²¾Í²»´æÔÚÁË¡£
halazi100 ÓÚ 2014-11-27 16:24:42·¢±í:
mallocºÍfreeµÄÔÀí
Ò»¡¢malloc()ºÍfree()µÄ»ù±¾¸ÅÄîÒÔ¼°»ù±¾Ó÷¨£º
1¡¢º¯ÊýÔÐͼ°ËµÃ÷£º
void *malloc(long NumBytes)£º¸Ãº¯Êý·ÖÅäÁËNumBytes¸ö×Ö½Ú£¬²¢·µ»ØÁËÖ¸ÏòÕâ¿éÄÚ´æµÄÖ¸Õë¡£Èç¹û·ÖÅäʧ°Ü£¬Ôò·µ»ØÒ»¸ö¿ÕÖ¸Õ루NULL£©¡£
¹ØÓÚ·ÖÅäʧ°ÜµÄÔÒò£¬Ó¦¸ÃÓжàÖÖ£¬±ÈÈç˵¿Õ¼ä²»×ã¾ÍÊÇÒ»ÖÖ¡£
void free(void *FirstByte)£º ¸Ãº¯ÊýÊǽ«Ö®Ç°ÓÃmalloc·ÖÅäµÄ¿Õ¼ä»¹¸ø³ÌÐò»òÕßÊDzÙ×÷ϵͳ£¬Ò²¾ÍÊÇÊÍ·ÅÁËÕâ¿éÄڴ棬ÈÃËüÖØеõ½×ÔÓÉ¡£
2¡¢º¯ÊýµÄÓ÷¨£º
ÆäʵÕâÁ½¸öº¯ÊýÓÃÆðÀ´µ¹²»ÊǺÜÄÑ£¬Ò²¾ÍÊÇmalloc()Ö®ºó¾õµÃÓù»Á˾Í˦ÁËËü°ÑËü¸øfree()ÁË£¬¾Ù¸ö¼òµ¥Àý×Ó£º
³ÌÐò´úÂ룺
// Code...
char *Ptr = NULL;
Ptr = (char *)malloc(100 * sizeof(char));
if (NULL == Ptr)
{
exit (1);
}
gets(Ptr);
// code...
free(Ptr);
Ptr = NULL;
// code...
¾ÍÊÇÕâÑù£¡µ±È»£¬¾ßÌåÇé¿öÒª¾ßÌå·ÖÎöÒÔ¼°¾ßÌå½â¾ö¡£±ÈÈç˵£¬Ä㶨ÒåÁËÒ»¸öÖ¸Õ룬ÔÚÒ»¸öº¯ÊýÀïÉêÇëÁËÒ»¿éÄÚ´æÈ»ºóͨ¹ýº¯Êý·µ»Ø´«µÝ¸øÕâ¸öÖ¸Õ룬ÄÇôҲÐíÊÍ·ÅÕâ¿éÄÚ´æÕâÏ×÷¾ÍÓ¦¸ÃÁô¸øÆäËûº¯ÊýÁË¡£
3¡¢¹ØÓÚº¯ÊýʹÓÃÐèҪעÒâµÄһЩµØ·½£º
A¡¢ÉêÇëÁËÄÚ´æ¿Õ¼äºó£¬±ØÐë¼ì²éÊÇ·ñ·ÖÅä³É¹¦¡£
B¡¢µ±²»ÐèÒªÔÙʹÓÃÉêÇëµÄÄÚ´æʱ£¬¼ÇµÃÊÍ·Å£»ÊͷźóÓ¦¸Ã°ÑÖ¸ÏòÕâ¿éÄÚ´æµÄÖ¸ÕëÖ¸ÏòNULL£¬·ÀÖ¹³ÌÐòºóÃ治СÐÄʹÓÃÁËËü¡£
C¡¢ÕâÁ½¸öº¯ÊýÓ¦¸ÃÊÇÅä¶Ô¡£Èç¹ûÉêÇëºó²»ÊͷžÍÊÇÄÚ´æй¶£»Èç¹ûÎÞ¹ÊÊÍ·ÅÄǾÍÊÇʲôҲûÓÐ×ö¡£ÊÍ·ÅÖ»ÄÜÒ»´Î£¬Èç¹ûÊÍ·ÅÁ½´Î¼°Á½´ÎÒÔÉÏ»á³öÏÖ´íÎó£¨ÊÍ·Å¿ÕÖ¸ÕëÀýÍ⣬ÊÍ·Å¿ÕÖ¸ÕëÆäʵҲµÈÓÚɶҲû×ö£¬ËùÒÔÊÍ·Å¿ÕÖ¸ÕëÊͷŶàÉٴζ¼Ã»ÓÐÎÊÌ⣩¡£
D¡¢ËäÈ»malloc()º¯ÊýµÄÀàÐÍÊÇ(void *),ÈκÎÀàÐ͵ÄÖ¸Õ붼¿ÉÒÔת»»³É(void *),µ«ÊÇ×îºÃ»¹ÊÇÔÚÇ°Ãæ½øÐÐÇ¿ÖÆÀàÐÍת»»£¬ÒòΪÕâÑù¿ÉÒÔ¶ã¹ýһЩ±àÒëÆ÷µÄ¼ì²é¡£
¶þ¡¢malloc()µ½µ×´ÓÄÄÀïµÃÀ´ÁËÄÚ´æ¿Õ¼ä£º
1¡¢malloc()µ½µ×´ÓÄÄÀïµÃµ½ÁËÄÚ´æ¿Õ¼ä£¿´ð°¸ÊÇ´Ó¶ÑÀïÃæ»ñµÃ¿Õ¼ä¡£Ò²¾ÍÊÇ˵º¯Êý·µ»ØµÄÖ¸ÕëÊÇÖ¸Ïò¶ÑÀïÃæµÄÒ»¿éÄÚ´æ¡£²Ù×÷ϵͳÖÐÓÐÒ»¸ö¼Ç¼¿ÕÏÐÄÚ´æµØÖ·µÄÁ´±í¡£µ±²Ù×÷ϵͳÊÕµ½³ÌÐòµÄÉêÇëʱ£¬¾Í»á±éÀú¸ÃÁ´±í£¬È»ºó¾ÍÑ°ÕÒµÚÒ»¸ö¿Õ¼ä´óÓÚËùÉêÇë¿Õ¼äµÄ¶Ñ½áµã£¬È»ºó¾Í½«¸Ã½áµã´Ó¿ÕÏнáµãÁ´±íÖÐɾ³ý£¬²¢½«¸Ã½áµãµÄ¿Õ¼ä·ÖÅä¸ø³ÌÐò¡£¾ÍÊÇÕâÑù£¡
2¡¢Ê²Ã´ÊǶѣº¶ÑÊÇ´ó¼Ò¹²ÓеĿռ䣬·ÖÈ«¾Ö¶ÑºÍ¾Ö²¿¶Ñ¡£È«¾Ö¶Ñ¾ÍÊÇËùÓÐûÓзÖÅäµÄ¿Õ¼ä£¬¾Ö²¿¶Ñ¾ÍÊÇÓû§·ÖÅäµÄ¿Õ¼ä¡£¶ÑÔÚ²Ù×÷ϵͳ¶Ô½ø³Ì³õʼ»¯µÄʱºò·ÖÅ䣬ÔËÐйý³ÌÖÐÒ²¿ÉÒÔÏòϵͳҪ¶îÍâµÄ¶Ñ£¬µ«ÊǼǵÃÓÃÍêÁËÒª»¹¸ø²Ù×÷ϵͳ£¬Òª²»È»¾ÍÊÇÄÚ´æй©¡£
ʲôÊÇÕ»£ºÕ»ÊÇÏ̶߳ÀÓеģ¬±£´æÆäÔËÐÐ״̬ºÍ¾Ö²¿×Ô¶¯±äÁ¿µÄ¡£Õ»ÔÚÏ߳̿ªÊ¼µÄʱºò³õʼ»¯£¬Ã¿¸öÏ̵߳ÄÕ»»¥Ïà¶ÀÁ¢¡£Ã¿¸öº¯Êý¶¼ÓÐ×Ô¼ºµÄÕ»£¬Õ»±»ÓÃÀ´ÔÚº¯ÊýÖ®¼ä´«µÝ²ÎÊý¡£²Ù×÷ϵͳÔÚÇл»Ï̵߳Äʱºò»á×Ô¶¯µÄÇл»Õ»£¬¾ÍÊÇÇл»SS/ESP¼Ä´æÆ÷¡£Õ»¿Õ¼ä²»ÐèÒªÔڸ߼¶ÓïÑÔÀïÃæÏÔʽµÄ·ÖÅäºÍÊÍ·Å¡£
ͨ¹ýÉÏÃæ¶Ô¸ÅÄîµÄÃèÊö£¬¿ÉÒÔÖªµÀ£º
Õ»ÊÇÓɱàÒëÆ÷×Ô¶¯·ÖÅäÊÍ·Å£¬´æ·Åº¯ÊýµÄ²ÎÊýÖµ¡¢¾Ö²¿±äÁ¿µÄÖµµÈ¡£²Ù×÷·½Ê½ÀàËÆÓÚÊý¾Ý½á¹¹ÖеÄÕ»¡£
¶ÑÒ»°ãÓɳÌÐòÔ±·ÖÅäÊÍ·Å£¬Èô²»ÊÍ·Å£¬³ÌÐò½áÊøʱ¿ÉÄÜÓÉOS»ØÊÕ¡£×¢ÒâÕâÀï˵ÊÇ¿ÉÄÜ£¬²¢·ÇÒ»¶¨¡£ËùÒÔÎÒÏëÔÙÇ¿µ÷Ò»´Î£¬¼ÇµÃÒªÊÍ·Å£¡
×¢ÒâËüÓëÊý¾Ý½á¹¹ÖеĶÑÊÇÁ½»ØÊ£¬·ÖÅ䷽ʽµ¹ÊÇÀàËÆÓÚÁ´±í¡£ÖµµÃ¹Ø×¢£¬windows»áÓõ½ÏµÍ³µÄ¶ÑÂð¡£
ËùÒÔ£¬¾Ù¸öÀý×Ó£¬Èç¹ûÄãÔÚº¯ÊýÉÏÃ涨ÒåÁËÒ»¸öÖ¸Õë±äÁ¿£¬È»ºóÔÚÕâ¸öº¯ÊýÀïÉêÇëÁËÒ»¿éÄÚ´æÈÃÖ¸ÕëÖ¸ÏòËü¡£Êµ¼ÊÉÏ£¬Õâ¸öÖ¸ÕëµÄµØÖ·ÊÇÔÚÕ»ÉÏ£¬µ«ÊÇËüËùÖ¸ÏòµÄÄÚÈÝÈ´ÊÇÔÚ¶ÑÉÏÃæµÄ£¡ÕâÒ»µãҪעÒ⣡ËùÒÔ£¬ÔÙÏëÏ룬ÔÚÒ»¸öº¯ÊýÀïÉêÇëÁË¿Õ¼äºó£¬±ÈÈç˵ÏÂÃæÕâ¸öº¯Êý£º
³ÌÐò´úÂ룺
// code...
void Function(void)
{
char *p = (char *)malloc(100 * sizeof(char));
}
¾ÍÕâ¸öÀý×Ó£¬Ç§Íò²»ÒªÈÏΪº¯Êý·µ»Ø£¬º¯ÊýËùÔÚµÄÕ»±»Ïú»ÙÖ¸ÕëÒ²¸ú×ÅÏú»Ù£¬ÉêÇëµÄÄÚ´æÒ²¾ÍÒ»Ñù¸ú×ÅÏú»ÙÁË£¡Õâ¾ø¶ÔÊÇ´íÎóµÄ£¡ÒòΪÉêÇëµÄÄÚ´æÔÚ¶ÑÉÏ£¬¶øº¯ÊýËùÔÚµÄÕ»±»Ïú»Ù¸ú¶ÑÍêȫûÓÐɶ¹Øϵ¡£ËùÒÔ£¬»¹ÊÇÄǾ仰£º¼ÇµÃÊÍ·Å£¡
Èç¹ûÒ»¸ö½ø³ÌÖеÄijº¯Êý·´¸´µÄµ÷Óøú¯ÊýÔòÄÚ´æй©»á¶Ñ»ý£¬Ö±µ½×ÊÔ´ºÄ¾¡£»½ø³Ì½áÊø£¬²Ù×÷ϵͳ»á»ØÊÕÄڴ棬Èç½ø³ÌÒ»Ö±ÔËÐУ¬Ò»Ö±Ð¹Â©£»
Èç¹ûÒ»¸ö½ø³ÌÓÐÏ޴εĵ÷ÓÃÁ˸ú¯Êýµ«»¹Ã»Óе¼ÖÂϵͳdown»úʱ½ø³Ì½áÊøÁË£¬ÕâʱºòÄÚ´æй©Ҳ»áÏûʧ£¬ÒòΪ½ø³Ì½áÊøLinuxÄں˻á»ØÊÕ½ø³ÌµÄ×ÊÔ´£¨´òɨս³¡£©£¬½ø³ÌµÄÐéÄâÄÚ´æ¿Õ¼ä¾Í²»´æÔÚÁË£¬µ±È»ÀïÃæÓÉmalloc·ÖÅäµÄÄÚ´æÒ²¶¼ÏûʧÁË£»Èç¹ûÕâÖÖ½ø³ÌÊÇ·þÎñ½ø³Ì£¬ÄǾÍΣÏÕÁË£¬ÏµÍ³·ÇµÃdown»ú²»¿É£¬ÒòΪ·þÎñ½ø³Ì´Óϵͳ¿ª»úʱ¾ÍÒ»Ö±ÔËÐв¢²»»á½áÊø£¬²»È»»¹ÊÇ·þÎñ³ÌÐòÂð¡£
3¡¢free()µ½µ×ÊÍ·ÅÁËʲô
Õâ¸öÎÊÌâ±È½Ï¼òµ¥£¬free()ÊͷŵÄÊÇÖ¸ÕëÖ¸ÏòµÄÄڴ棡עÒ⣡ÊͷŵÄÊÇÄڴ棬²»ÊÇÖ¸Õ룡Õâµã·Ç³£·Ç³£ÖØÒª£¡Ö¸ÕëÊÇÒ»¸ö±äÁ¿£¬Ö»ÓгÌÐò½áÊøʱ²Å±»Ïú»Ù¡£ÊÍ·ÅÁËÄÚ´æ¿Õ¼äºó£¬ÔÀ´Ö¸ÏòÕâ¿é¿Õ¼äµÄÖ¸Õ뻹ÊÇ´æÔÚ£¡Ö»²»¹ýÏÖÔÚÖ¸ÕëÖ¸ÏòµÄÄÚÈÝÊÇÎÞÓÃÀ¬»øÊý¾Ý£¬ÊÇ䶨ÒåµÄ£¬²»Êܱ£»¤µÄ£¬ËùÒÔ˵ÊÇÀ¬»ø¡£Òò´Ë£¬ÊÍ·ÅÄÚ´æºó°ÑÖ¸ÕëÖ¸ÏòNULL£¬·ÀÖ¹Ö¸ÕëÔÚºóÃ治СÐÄÓÖ±»ÒýÓÃÁË¡£·Ç³£ÖØÒª°¡ÕâÒ»µã£¡
Èý¡¢malloc()ÒÔ¼°free()µÄ»úÖÆ£º
Õâ¸ö²¿·ÖÎÒ½ñÌì²ÅÓÐÁËеÄÈÏʶ£¡¶øÇÒÊÇתÕÛÐÔµÄÈÏʶ£¡ËùÒÔ£¬Õⲿ·Ö¿ÉÄÜ»áÓиü¶àһЩÈÏʶÉϵĴíÎ󣡲»¶ÔµÄµØ·½Çë´ó¼Ò°ïæָ³ö£¡
ÊÂʵÉÏ£¬×Ðϸ¿´Ò»ÏÂfree()µÄº¯ÊýÔÐÍ£¬Ò²ÐíÒ²»á·¢ÏÖËƺõºÜÉñÆ棬free()º¯Êý·Ç³£¼òµ¥£¬Ö»ÓÐÒ»¸ö²ÎÊý£¬Ö»Òª°ÑÖ¸ÏòÉêÇë¿Õ¼äµÄÖ¸Õë´«µÝ¸øfree()ÖеIJÎÊý¾Í¿ÉÒÔÍê³ÉÊͷŹ¤×÷£¡ÕâÀïҪ׷×Ùµ½malloc()µÄÉêÇëÎÊÌâÁË¡£ÉêÇëµÄʱºòʵ¼ÊÉÏÕ¼ÓõÄÄÚ´æÒª±ÈÉêÇëµÄ´ó¡£ÒòΪ³¬³öµÄ¿Õ¼äÊÇÓÃÀ´¼Ç¼¶ÔÕâ¿éÄÚ´æµÄ¹ÜÀíÐÅÏ¢¡£ÏÈ¿´Ò»ÏÂÔÚ¡¶UNIX»·¾³¸ß¼¶±à³Ì¡·ÖеÚÆßÕµÄÒ»¶Î»°£º
´ó¶àÊýʵÏÖËù·ÖÅäµÄ´æ´¢¿Õ¼ä±ÈËùÒªÇóµÄÒªÉÔ´óһЩ£¬¶îÍâµÄ¿Õ¼äÓÃÀ´¼Ç¼¹ÜÀíÐÅÏ¢----·ÖÅä¿éµÄ³¤¶È£¬Ö¸ÏòÏÂÒ»¸ö·ÖÅä¿éµÄÖ¸ÕëµÈµÈ¡£Õâ¾ÍÒâζ×ÅÈç¹ûд¹ýÒ»¸öÒÑ·ÖÅäÇøµÄβ¶Ë£¬Ôò»á¸ÄдºóÒ»¿éµÄ¹ÜÀíÐÅÏ¢¡£ÕâÖÖÀàÐ͵ĴíÎóÊÇÔÖÄÑÐԵģ¬µ«ÊÇÒòΪÕâÖÖ´íÎó²»»áºÜ¿ì¾Í±©Â¶³öÀ´£¬ËùÒÔÒ²¾ÍºÜÄÑ·¢ÏÖ¡£½«Ö¸Ïò·ÖÅä¿éµÄÖ¸ÕëÏòºóÒƶ¯Ò²¿ÉÄÜ»á¸Äд±¾¿éµÄ¹ÜÀíÐÅÏ¢¡£
ÒÔÉÏÕâ¶Î»°ÒѾ¸øÁËÎÒÃÇһЩÐÅÏ¢ÁË¡£malloc()ÉêÇëµÄ¿Õ¼äʵ¼ÊÎÒ¾õµÃ¾ÍÊÇ·ÖÁËÁ½¸ö²»Í¬ÐÔÖʵĿռ䡣һ¸ö¾ÍÊÇÓÃÀ´¼Ç¼¹ÜÀíÐÅÏ¢µÄ¿Õ¼ä£¬ÁíÍâÒ»¸ö¾ÍÊÇ¿ÉÓÿռäÁË¡£¶øÓÃÀ´¼Ç¼¹ÜÀíÐÅÏ¢µÄʵ¼ÊÉÏÊÇÒ»¸ö½á¹¹Ìå¡£ÔÚCÓïÑÔÖУ¬ÓýṹÌåÀ´¼Ç¼ͬһ¸ö¶ÔÏóµÄ²»Í¬ÐÅÏ¢ÊÇÌì¾µØÒåµÄÊ£¡ÏÂÃæ¿´¿´Õâ¸ö½á¹¹ÌåµÄÔÐÍ£º
³ÌÐò´úÂ룺
struct mem_control_block {
int is_available; //ÕâÊÇÒ»¸ö±ê¼Ç£¿
int size; //ÕâÊÇʵ¼Ê¿Õ¼äµÄ´óС
};
¶ÔÓÚsize,Õâ¸öÊÇʵ¼Ê¿Õ¼ä´óС¡£ÕâÀïÆäʵÎÒÓиöÒÉÎÊ£¬is_availableÊÇ·ñÊÇÒ»¸ö±ê¼Ç£¿ÒòΪÎÒ¿´ÁËfree()µÄÔ´´úÂëÖ®ºó¶ÔÕâ¸ö±äÁ¿¸Ð¾õÓеãÄÉÃÆ£¨Ô´´úÂëÔÚÏÂÃæ·ÖÎö£©¡£ÕâÀﻹÇë´ó¼ÒÖ¸³ö£¡
ËùÒÔ£¬free()¾ÍÊǸù¾ÝÕâ¸ö½á¹¹ÌåµÄÐÅÏ¢À´ÊÍ·Åmalloc()ÉêÇëµÄ¿Õ¼ä£¡¶ø½á¹¹ÌåµÄÁ½¸ö³ÉÔ±µÄ´óСÎÒÏëÓ¦¸ÃÊDzÙ×÷ϵͳµÄÊÂÁË¡£µ«ÊÇÕâÀïÓÐÒ»¸öÎÊÌ⣬malloc()ÉêÇë¿Õ¼äºó·µ»ØÒ»¸öÖ¸ÕëÓ¦¸ÃÊÇÖ¸ÏòµÚ¶þÖֿռ䣬Ҳ¾ÍÊÇ¿ÉÓÿռ䣡²»È»£¬Èç¹ûÖ¸Ïò¹ÜÀíÐÅÏ¢¿Õ¼äµÄ»°£¬Ð´ÈëµÄÄÚÈݺͽṹÌåµÄÀàÐÍÓпÉÄܲ»Ò»Ö£¬»òÕß»á°Ñ¹ÜÀíÐÅÏ¢ÆÁ±Îµô£¬ÄǾÍû·¨ÊÍ·ÅÄÚ´æ¿Õ¼äÁË£¬ËùÒԻᷢÉú´íÎ󣡣¨¸Ð¾õ×Ô¼ºÕâÀï˵µÄÊÇ·Ï»°£©
ºÃÁË£¡ÏÂÃæ¿´¿´free()µÄÔ´´úÂ룬ÎÒ×Ô¼º·ÖÎöÁËһϣ¬¾õµÃ±ÈÆðmalloc()µÄÔ´´úÂëµ¹ÊÇÈÝÒ×¼òµ¥ºÜ¶à¡£Ö»ÊÇÓиöÒÉÎÊ£¬ÏÂÃæÖ¸³ö£¡
³ÌÐò´úÂ룺
¿´Ò»Ïº¯ÊýµÚ¶þ¾ä£¬Õâ¾ä·Ç³£ÖØÒªºÍ¹Ø¼ü¡£ÆäʵÕâ¾ä¾ÍÊÇ°ÑÖ¸Ïò¿ÉÓÿռäµÄÖ¸Õëµ¹»ØÈ¥£¬ÈÃËüÖ¸Ïò¹ÜÀíÐÅÏ¢µÄÄÇ¿é¿Õ¼ä£¬ÒòΪÕâÀïÊÇÔÚÖµÉϼõÈ¥ÁËÒ»¸ö½á¹¹ÌåµÄ´óС£¡ºóÃæÄÇÒ»¾äfree->is_available = 1;ÎÒÓеãÄÉÃÆ£¡ÎÒµÄÏë·¨ÊÇ£ºÕâÀïis_availableÓ¦¸ÃÖ»ÊÇÒ»¸ö±ê¼Ç¶øÒÑ£¡ÒòΪ´ÓÕâ¸ö±äÁ¿µÄÃû³ÆÉÏÀ´¿´£¬is_available ·Òë¹ýÀ´¾ÍÊÇ¡°ÊÇ¿ÉÒÔÓᱡ£²»ÒªËµÎÒÍÁ£¡ÎÒ¾õµÃ±äÁ¿Ãû×Ö¿ÉÒÔ·´Ó³Ò»¸ö±äÁ¿µÄ×÷Óã¬ÌرðÊÇÑϽ÷µÄ´úÂë¡£ÕâÊÇÔ´´úÂ룬ËùÒÔÎÒ¾õµÃ¾ø¶ÔÊÇÑϽ÷µÄ£¡£¡Õâ¸ö±äÁ¿µÄÖµÊÇ1£¬±íÃ÷ÊÇ¿ÉÒÔÓõĿռ䣡ֻÊÇÕâÀïÎÒÏëÁËÏ룬Èç¹û°ÑËü¸ÄΪ0»òÕßÊÇÆäËûÖµ²»ÖªµÀ»á·¢ÉúʲôÊ£¿£¡µ«ÊÇÓÐÒ»µãÎÒ¿ÉÒԿ϶¨£¬¾ÍÊÇÊͷžø¶Ô²»»áÄÇô˳Àû½øÐУ¡ÒòΪÕâÊÇÒ»¸ö±ê¼Ç£¡
µ±È»£¬ÕâÀï¿ÉÄÜ»¹ÊÇÓÐÈË»áÓÐÒÉÎÊ£¬ÎªÊ²Ã´ÕâÑù¾Í¿ÉÒÔÊÍ·ÅÄØ£¿£¿ÎÒ¸Õ²ÅÒ²ÓÐÕâ¸öÒÉÎÊ¡£ºóÀ´ÎÒÏëµ½£¬ÊÍ·ÅÊDzÙ×÷ϵͳµÄÊ£¬ÄÇô¾Ífree()Õâ¸öÔ´´úÂëÀ´¿´£¬Ê²Ã´Ò²Ã»ÓÐÊÍ·Å£¬¶Ô°É£¿µ«ÊÇËüȷʵÊÇÈ·¶¨Á˹ÜÀíÐÅÏ¢µÄÄÇ¿éÄÚ´æµÄÄÚÈÝ¡£ËùÒÔ£¬free()Ö»ÊǼǼÁËһЩÐÅÏ¢£¬È»ºó¸æËß²Ù×÷ϵͳÄÇ¿éÄÚ´æ¿ÉÒÔÈ¥ÊÍ·Å£¬¾ßÌåÔõô¸æËß²Ù×÷ϵͳµÄÎÒ²»Çå³þ£¬µ«ÎÒ¾õµÃÕâ¸öÒѾ³¬³öÁËÎÒÕâƪÎÄÕµÄÌÖÂÛ·¶Î§ÁË¡£
ÄÇô£¬ÎÒ֮ǰÓиö´íÎóµÄÈÏʶ£¬¾ÍÊÇÈÏΪָÏòÄÇ¿éÄÚ´æµÄÖ¸Õë²»¹ÜÒƵ½ÄÇ¿éÄÚ´æÖеÄÄĸöλÖö¼¿ÉÒÔÊÍ·ÅÄÇ¿éÄڴ棡µ«ÊÇ£¬ÕâÊÇ´ó´íÌØ´í£¡ÊÍ·ÅÊDz»¿ÉÒÔÊÍ·ÅÒ»²¿·ÖµÄ£¡Ê×ÏÈÕâµãÓ¦¸ÃÒªÃ÷°×¡£¶øÇÒ£¬´Ófree()µÄÔ´´úÂë¿´£¬ptrÖ»ÄÜÖ¸Ïò¿ÉÓÿռäµÄÊ×µØÖ·£¬²»È»£¬¼õÈ¥½á¹¹Ìå´óС֮ºóÒ»¶¨²»ÊÇÖ¸Ïò¹ÜÀíÐÅÏ¢¿Õ¼äµÄÊ×µØÖ·¡£ËùÒÔ£¬ÒªÈ·±£Ö¸ÕëÖ¸Ïò¿ÉÓÿռäµÄÊ×µØÖ·£¡²»ÐÅÂð£¿×Ô¼º¿ÉÒÔдһ¸ö³ÌÐòÈ»ºóÒƶ¯Ö¸Ïò¿ÉÓÿռäµÄÖ¸Õ룬¿´³ÌÐò»á²»»á±À£¡
×îºó¿ÉÄÜÏëµ½malloc()µÄÔ´´úÂë¿´¿´malloc()µ½µ×ÊÇÔõô·ÖÅä¿Õ¼äµÄ£¬ÕâÀïÃæÉæ¼°µ½ºÜ¶àÆäËû·½ÃæµÄ֪ʶ£¡ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔ×Ô¼ºÈ¥ÏÂÔØÔ´´úÂëÈ¥¿´¿´¡£
=================================================
CÓïÑÔµÄmalloc·ÖÅäµÄµÄÄÚ´æ´óС
û¶Á¹ýmalloc()µÄÔ´Â룬ËùÒÔÕâÀï´¿´âÊÇ"ÀíÂÛÑо¿"¡£
malloc()ÔÚÔËÐÐÆÚ¶¯Ì¬·ÖÅä·ÖÅäÄÚ´æ,free()ÊÍ·ÅÓÉÆä·ÖÅäµÄÄÚ´æ¡£malloc()ÔÚ·ÖÅäÓû§´«ÈëµÄ´óСµÄʱºò£¬»¹·ÖÅäµÄÒ»¸öÏà¹ØµÄÓÃÓÚ¹ÜÀíµÄ¶îÍâÄڴ棬²»¹ý£¬Óû§ÊÇ¿´²»µ½µÄ¡£ËùÒÔ£¬
ʵ¼ÊµÄ´óС = ¹ÜÀí¿Õ¼ä + Óû§¿Õ¼ä
ÄÇô£¬Õâ¸ö¹ÜÀíÄÚ´æ·ÅÔÚʲôλÖÃÄØ,ËüÒªÈÃfree()º¯ÊýÄܹ»ÕÒµ½£¬ÕâÑù²ÅÄÜÖªµÀÓжàÉÙÄÚ´æÒªÊÍ·Å£¬ËùÒÔÒ»ÖÖ¿ÉÄܵķ½°¸ÊÇÔÚ·ÖÅäÄÚ´æµÄ³õʼ²¿·ÖÓÃÈô¸É¸ö×Ö½ÚÀ´´æ´¢·ÖÅäµÄÄÚ´æµÄ´óС¡£ÕâÀïҪעÒâÒ»¸öÎÊÌ⣬¾ÍÊÇ£¬ÔÚmalloc()½«Õâ¸ö·ÖÅäµÄ¿Õ¼ä·µ»Ø¸øij¸öÖ¸Õëºó£¬Õâ¸öÖ¸ÕëµÄʹÓÃÓëÆäËüÖ¸ÕëÓ¦¸ÃÊÇûÓвî±ðµÄ£¬ËùÒÔ£¬¹ÜÀí¿Õ¼äÓ¦¸ÃÔÚÕâ¸öÖ¸ÕëÖ¸ÏòµÄ¿Õ¼äÖ®Í⣬µ«ÓÖÒªfree()´ÓÕâ¸öÖ¸Õë¿ÉÒÔÕÒµ½¹ÜÀíÐÅÏ¢£¬ËùÒÔ£¬Õâ¸ö¹ÜÀí¿Õ¼äµÄ´óС·ÅÔÚÖ¸ÕëÖ¸ÏòµÄÏà·´·½Ïò¡£¹Êmalloc()µÄ¾ßÌå²Ù×÷Ó¦¸Ã¾ÍÊÇ·ÖÅäÒ»¿éÄڴ棬ÔÚÇ°ÃæÈô¸É×Ö½ÚÖÐдÈë¹ÜÀíÐÅÏ¢£¬È»ºó·µ»Ø¹ÜÀíÐÅÏ¢ËùÕ¼×Ö½ÚÖ®ºóµÄµØÖ·Ö¸Õë¡£
=================================================
malloc£¨£©¹¤×÷»úÖÆ
¡¡¡¡mallocº¯ÊýµÄʵÖÊÌåÏÖÔÚ£¬ËüÓÐÒ»¸ö½«¿ÉÓõÄÄÚ´æ¿éÁ¬½ÓΪһ¸ö³¤³¤µÄÁбíµÄËùν¿ÕÏÐÁ´±í¡£µ÷ÓÃmallocº¯Êýʱ£¬ËüÑØÁ¬½Ó±íÑ°ÕÒÒ»¸ö´óµ½×ãÒÔÂú×ãÓû§ÇëÇóËùÐèÒªµÄÄÚ´æ¿é¡£È»ºó£¬½«¸ÃÄÚ´æ¿éÒ»·ÖΪ¶þ£¨Ò»¿éµÄ´óСÓëÓû§ÇëÇóµÄ´óСÏàµÈ£¬ÁíÒ»¿éµÄ´óС¾ÍÊÇʣϵÄ×Ö½Ú£©¡£½ÓÏÂÀ´£¬½«·ÖÅä¸øÓû§µÄÄÇ¿éÄÚ´æ´«¸øÓû§£¬²¢½«Ê£ÏµÄÄǿ飨Èç¹ûÓеĻ°£©·µ»Øµ½Á¬½Ó±íÉÏ¡£µ÷ÓÃfreeº¯Êýʱ£¬Ëü½«Óû§ÊͷŵÄÄÚ´æ¿éÁ¬½Óµ½¿ÕÏÐÁ´ÉÏ¡£µ½×îºó£¬¿ÕÏÐÁ´»á±»ÇгɺܶàµÄСÄÚ´æƬ¶Î£¬Èç¹ûÕâʱÓû§ÉêÇëÒ»¸ö´óµÄÄÚ´æƬ¶Î£¬ÄÇô¿ÕÏÐÁ´ÉÏ¿ÉÄÜûÓпÉÒÔÂú×ãÓû§ÒªÇóµÄƬ¶ÎÁË¡£ÓÚÊÇ£¬mallocº¯ÊýÇëÇóÑÓʱ£¬²¢¿ªÊ¼ÔÚ¿ÕÏÐÁ´ÉÏ·Ïäµ¹¹ñµØ¼ì²é¸÷ÄÚ´æƬ¶Î£¬¶ÔËüÃǽøÐÐÕûÀí£¬½«ÏàÁÚµÄС¿ÕÏпéºÏ²¢³É½Ï´óµÄÄÚ´æ¿é¡£
malloc£¨£©ÔÚ²Ù×÷ϵͳÖеÄʵÏÖ
¡¡¡¡ÔÚ C ³ÌÐòÖУ¬¶à´ÎʹÓÃmalloc () ºÍ free()¡£²»¹ý£¬Äú¿ÉÄÜûÓÐÓÃһЩʱ¼äȥ˼¿¼ËüÃÇÔÚÄúµÄ²Ù×÷ϵͳÖÐÊÇÈçºÎʵÏֵġ£±¾½Ú½«ÏòÄúչʾ malloc ºÍ free µÄÒ»¸ö×î¼ò»¯ÊµÏֵĴúÂ룬À´°ïÖú˵Ã÷¹ÜÀíÄÚ´æʱ¶¼Éæ¼°µ½ÁËÄÄЩÊÂÇé¡£
¡¡¡¡Ôڴ󲿷ֲÙ×÷ϵͳÖУ¬ÄÚ´æ·ÖÅäÓÉÒÔÏÂÁ½¸ö¼òµ¥µÄº¯ÊýÀ´´¦Àí£º
¡¡¡¡void *malloc(long numbytes)£º¸Ãº¯Êý¸ºÔð·ÖÅä numbytes ´óСµÄÄڴ棬²¢·µ»ØÖ¸ÏòµÚÒ»¸ö×Ö½ÚµÄÖ¸Õë¡£
¡¡¡¡void free(void *firstbyte)£ºÈç¹û¸ø¶¨Ò»¸öÓÉÏÈÇ°µÄ malloc ·µ»ØµÄÖ¸Õ룬ÄÇô¸Ãº¯Êý»á½«·ÖÅäµÄ¿Õ¼ä¹é»¹¸ø½ø³ÌµÄ¡°¿ÕÏпռ䡱¡£
¡¡¡¡malloc_init ½«Êdzõʼ»¯ÄÚ´æ·ÖÅä³ÌÐòµÄº¯Êý¡£ËüÒªÍê³ÉÒÔÏÂÈý¼þÊ£º½«·ÖÅä³ÌÐò±êʶΪÒѾ³õʼ»¯£¬ÕÒµ½ÏµÍ³ÖÐ×îºóÒ»¸öÓÐЧÄÚ´æµØÖ·£¬È»ºó½¨Á¢ÆðÖ¸ÏòÎÒÃǹÜÀíµÄÄÚ´æµÄÖ¸Õë¡£
Çåµ¥ 1. ÎÒÃǵļòµ¥·ÖÅä³ÌÐòµÄÈ«¾Ö±äÁ¿
ÈçÇ°ËùÊö£¬±»Ó³ÉäµÄÄÚ´æµÄ±ß½ç£¨×îºóÒ»¸öÓÐЧµØÖ·£©³£±»³ÆΪϵͳÖжϵã»òÕßµ±Ç°Öжϵ㡣ÔںܶàUNIXϵͳÖУ¬ÎªÁËÖ¸³öµ±Ç°ÏµÍ³Öжϵ㣬±ØÐëʹÓà sbrk(0) º¯Êý¡£ sbrk ¸ù¾Ý²ÎÊýÖиø³öµÄ×Ö½ÚÊýÒƶ¯µ±Ç°ÏµÍ³Öжϵ㣬Ȼºó·µ»ØеÄϵͳÖжϵ㡣ʹÓòÎÊý 0 Ö»ÊÇ·µ»Øµ±Ç°Öжϵ㡣
Çåµ¥ 2. ·ÖÅä³ÌÐò³õʼ»¯º¯Êý
/* Include the sbrk function */
ÏÖÔÚ£¬ÎªÁËÍêÈ«µØ¹ÜÀíÄڴ棬ÎÒÃÇÐèÒªÄܹ»×·×ÙÒª·ÖÅäºÍ»ØÊÕÄÄЩÄÚ´æ¡£ÔÚ¶ÔÄÚ´æ¿é½øÐÐÁË free µ÷ÓÃÖ®ºó£¬ÎÒÃÇÐèÒª×öµÄÊÇÖîÈ罫ËüÃDZê¼ÇΪδ±»Ê¹ÓõĵÈÊÂÇ飬²¢ÇÒ£¬ÔÚµ÷Óà malloc ʱ£¬ÎÒÃÇÒªÄܹ»¶¨Î»Î´±»Ê¹ÓõÄÄÚ´æ¿é¡£
Çåµ¥ 3. ÄÚ´æ¿ØÖÆ¿é½á¹¹¶¨Òå
ÏÖÔÚ£¬Äú¿ÉÄÜ»áÈÏΪµ±³ÌÐòµ÷Óà malloc ʱÕâ»áÒý·¢ÎÊÌâ ---- ËüÃÇÈçºÎÖªµÀÕâ¸ö½á¹¹£¿´ð°¸ÊÇËüÃDz»±ØÖªµÀ£»ÔÚ·µ»ØÖ¸Õë֮ǰ£¬ÎÒÃǻὫÆäÒƶ¯µ½Õâ¸ö½á¹¹Ö®ºó£¬°ÑËüÒþ²ØÆðÀ´¡£ÕâʹµÃ·µ»ØµÄÖ¸ÕëÖ¸ÏòûÓÐÓÃÓÚÈκÎÆäËûÓÃ;µÄÄÚ´æ¡£ÄÇÑù£¬´Óµ÷ÓóÌÐòµÄ½Ç¶ÈÀ´¿´£¬ËüÃÇËùµÃµ½µÄÈ«²¿ÊÇ¿ÕÏеġ¢¿ª·ÅµÄÄڴ档Ȼºó£¬µ±Í¨¹ý free() ½«¸ÃÖ¸Õë´«µÝ»ØÀ´Ê±£¬ÎÒÃÇÖ»ÐèÒªµ¹Í˼¸¸öÄÚ´æ×ֽھͿÉÒÔÔÙ´ÎÕÒµ½Õâ¸ö½á¹¹¡£
¡¡¡¡ÔÚÌÖÂÛ·ÖÅäÄÚ´æ֮ǰ£¬ÎÒÃǽ«ÏÈÌÖÂÛÊÍ·Å£¬ÒòΪËü¸ü¼òµ¥¡£ÎªÁËÊÍ·ÅÄڴ棬ÎÒÃDZØÐëÒª×öµÄΩһһ¼þÊÂÇé¾ÍÊÇ£¬»ñµÃÎÒÃǸø³öµÄÖ¸Õ룬»ØÍË sizeof(struct mem_control_block) ¸ö×Ö½Ú£¬²¢½«Æä±ê¼ÇΪ¿ÉÓõġ£
Çåµ¥ 4. ½â³ý·ÖÅ亯Êý
Õâ¾ÍÊÇÎÒÃǵÄÄÚ´æ¹ÜÀíÆ÷¡£ÏÖÔÚ£¬ÎÒÃÇÖ»ÐèÒª¹¹½¨Ëü£¬²¢ÔÚ³ÌÐòÖÐʹÓÃËü¼´¿É.¶à´Îµ÷ÓÃmalloc£¨£©ºó¿ÕÏÐÄÚ´æ±»ÇгɺܶàµÄСÄÚ´æƬ¶Î£¬Õâ¾ÍʹµÃÓû§ÔÚÉêÇëÄÚ´æʹÓÃʱ£¬ÓÉÓÚÕÒ²»µ½×ã¹»´óµÄÄÚ´æ¿Õ¼ä£¬malloc£¨£©ÐèÒª½øÐÐÄÚ´æÕûÀí£¬Ê¹µÃº¯ÊýµÄÐÔÄÜÔ½À´Ô½µÍ¡£´ÏÃ÷µÄ³ÌÐòԱͨ¹ý×ÜÊÇ·ÖÅä´óСΪ2µÄÃݵÄÄÚ´æ¿é£¬¶ø×î´óÏ޶ȵؽµµÍDZÔÚµÄmallocÐÔÄÜɥʧ¡£Ò²¾ÍÊÇ˵£¬Ëù·ÖÅäµÄÄÚ´æ¿é´óСΪ4×Ö½Ú¡¢8×Ö½Ú¡¢16×Ö½Ú¡¢2^n×Ö½Ú£¬µÈµÈ¡£ÕâÑù×ö×î´óÏ޶ȵؼõÉÙÁ˽øÈë¿ÕÏÐÁ´µÄ¹ÖÒìƬ¶Î£¨¸÷ÖֳߴçµÄСƬ¶Î¶¼ÓУ©µÄÊýÁ¿¡£¾¡¹Ü¿´ÆðÀ´ÕâºÃÏñÀË·ÑÁ˿ռ䣬µ«Ò²ÈÝÒ׿´³öÀ˷ѵĿռäÓÀÔ¶²»»á³¬¹ý50%