1£®Õ»Çø£¨stack£©¡ª ÓɱàÒëÆ÷×Ô¶¯·ÖÅäÊÍ·Å£¬´æ·Åº¯ÊýµÄ²ÎÊýÖµ£¬¾Ö²¿±äÁ¿µÄÖµµÈ¡£Æä²Ù×÷·½Ê½ÀàËÆÓÚÊý¾Ý½á¹¹ÖеÄÕ»¡£
2£®¶ÑÇø£¨heap£© ¡ª Ò»°ãÓɳÌÐòÔ±·ÖÅäÊÍ·Å£¬Èô³ÌÐòÔ±²»ÊÍ·Å£¬³ÌÐò½áÊøʱ¿ÉÄÜÓÉOS»ØÊÕ ¡£×¢ÒâËüÓëÊý¾Ý½á¹¹ÖеĶÑÊÇÁ½»ØÊ£¬·ÖÅ䷽ʽµ¹ÊÇÀàËÆÓÚÁ´±í¡£
3£®È«¾ÖÇø£¨¾²Ì¬Çø£©£¨static£©¡ª£¬È«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿µÄ´æ´¢ÊÇ·ÅÔÚÒ»¿éµÄ£¬³õʼ»¯µÄÈ«¾Ö±äÁ¿ºÍ¾²Ì¬±äÁ¿ÔÚÒ»¿éÇøÓò£¬ δ³õʼ»¯µÄÈ«¾Ö±äÁ¿¡¢Î´³õʼ»¯µÄ¾²Ì¬±äÁ¿ÔÚÏàÁÚµÄÁíÒ»¿éÇøÓò¡£ - ³ÌÐò½áÊøºóÓÐϵͳÊÍ·Å
4£®ÎÄ×Ö³£Á¿Çø ¡ª³£Á¿×Ö·û´®¾ÍÊÇ·ÅÔÚÕâÀïµÄ¡£³ÌÐò½áÊøºóÓÉϵͳÊÍ·Å
5£®³ÌÐò´úÂëÇø¡ª´æ·Åº¯ÊýÌåµÄ¶þ½øÖÆ´úÂë¡£
Ò»¸öÕý³£µÄ³ÌÐòÔÚÄÚ´æÖÐͨ³£·ÖΪ³ÌÐò¶Î¡¢Êý¾Ý¶Ë¡¢¶ÑÕ»Èý²¿·Ö¡£³ÌÐò¶ÎÀï·Å×ųÌÐòµÄ»úÆ÷Âë¡¢Ö»¶ÁÊý¾Ý£¬Õâ¸ö¶Îͨ³£ÊÇÖ»¶Á£¬¶ÔËüµÄд²Ù×÷ÊÇ·Ç·¨µÄ¡£Êý¾Ý¶Î·ÅµÄÊdzÌÐòÖеľ²Ì¬Êý¾Ý¡£¶¯Ì¬Êý¾ÝÔòͨ¹ý¶ÑÕ»À´´æ·Å¡£
¶ÑÕ»ÊÇÄÚ´æÖеÄÒ»¸öÁ¬ÐøµÄ¿é¡£Ò»¸ö½Ð¶ÑÕ»Ö¸ÕëµÄ¼Ä´æÆ÷£¨SP£©Ö¸Ïò¶ÑÕ»µÄÕ»¶¥¡£¶ÑÕ»µÄµ×²¿ÊÇÒ»¸ö¹Ì¶¨µØÖ·¡£¶ÑÕ»ÓÐÒ»¸öÌصã¾ÍÊÇ£¬ºó½øÏȳö¡£Ò²¾ÍÊÇ˵£¬ºó·ÅÈëµÄÊý¾ÝµÚÒ»¸öÈ¡³ö¡£ËüÖ§³ÖÁ½¸ö²Ù×÷£¬PUSHºÍPOP¡£PUSHÊǽ«Êý¾Ý·Åµ½Õ»µÄ¶¥¶Ë£¬POPÊǽ«Õ»¶¥µÄÊý¾ÝÈ¡³ö¡£
Ôڸ߼¶ÓïÑÔÖУ¬³ÌÐòº¯Êýµ÷Óᢺ¯ÊýÖеÄÁÙʱ±äÁ¿¶¼Óõ½¶ÑÕ»¡£ÎªÊ²Ã´ÄØ£¿ÒòΪÔÚµ÷ÓÃÒ»¸öº¯Êýʱ£¬ÎÒÃÇÐèÒª¶Ôµ±Ç°µÄ²Ù×÷½øÐб£»¤£¬Ò²ÎªÁ˺¯ÊýÖ´Ðк󣬳ÌÐò¿ÉÒÔÕýÈ·µÄÕÒµ½µØ·½¼ÌÐøÖ´ÐУ¬ËùÒÔ²ÎÊýµÄ´«µÝºÍ·µ»ØÖµÒ²Óõ½Á˶ÑÕ»¡£Í¨³£¶Ô¾Ö²¿±äÁ¿µÄÒýÓÃÊÇͨ¹ý¸ø³öËüÃǶÔSPµÄÆ«ÒÆÁ¿À´ÊµÏֵġ£ÁíÍ⻹ÓÐÒ»¸ö»ùÖ·Ö¸Õ루FP£¬ÔÚIntelоƬÖÐÊÇBP£©£¬Ðí¶à±àÒëÆ÷ʵ¼ÊÉÏÊÇÓÃËüÀ´ÒýÓñ¾µØ±äÁ¿ºÍ²ÎÊýµÄ¡£Í¨³££¬²ÎÊýµÄÏà¶ÔFPµÄÆ«ÒÆÊÇÕýµÄ£¬¾Ö²¿±äÁ¿ÊǸºµÄ¡£
µ±³ÌÐòÖз¢Éúº¯Êýµ÷ÓÃʱ£¬¼ÆËã»ú×öÈçϲÙ×÷£ºÊ×ÏȰѲÎÊýѹÈë¶ÑÕ»£»È»ºó±£´æÖ¸Áî¼Ä´æÆ÷(IP)ÖеÄÄÚÈÝ£¬×öΪ·µ»ØµØÖ·(RET)£»µÚÈý¸ö·ÅÈë¶ÑÕ»µÄÊÇ»ùÖ·¼Ä´æÆ÷(FP)£»È»ºó°Ñµ±Ç°µÄÕ»Ö¸Õë(SP)¿½±´µ½FP£¬×öΪеĻùµØÖ·£»×îºóΪ±¾µØ±äÁ¿Áô³öÒ»¶¨¿Õ¼ä£¬°ÑSP¼õÈ¥Êʵ±µÄÊýÖµ¡£
ÔÚº¯ÊýÌåÖж¨ÒåµÄ±äÁ¿Í¨³£ÊÇÔÚÕ»ÉÏ£¬ÓÃmalloc, calloc, reallocµÈ·ÖÅäÄÚ´æµÄº¯Êý·ÖÅäµÃµ½µÄ¾ÍÊÇÔÚ¶ÑÉÏ¡£ÔÚËùÓк¯ÊýÌåÍⶨÒåµÄÊÇÈ«¾ÖÁ¿£¬¼ÓÁËstaticÐÞÊηûºó²»¹ÜÔÚÄÄÀﶼ´æ·ÅÔÚÈ«¾ÖÇø£¨¾²Ì¬Çø£©£¬ÔÚËùÓк¯ÊýÌåÍⶨÒåµÄstatic±äÁ¿±íʾÔÚ¸ÃÎļþÖÐÓÐЧ£¬²»ÄÜexternµ½±ðµÄÎļþÓã»ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄstatic±íʾֻÔڸú¯ÊýÌåÄÚÓÐЧ¡£ÁíÍ⣬º¯ÊýÖеÄ"adgfdf"ÕâÑùµÄ×Ö·û´®´æ·ÅÔÚ³£Á¿Çø¡£
¶Ô±È£º
1£®ÐÔÄÜ
stack¡¾Õ»¡¿£ºÕ»´æÔÚÓÚRAMÖС£Õ»ÊǶ¯Ì¬µÄ£¬ËüµÄ´æ´¢ËÙ¶ÈÊǵڶþ¿ìµÄ¡£
heap¡¾¶Ñ¡¿£º¶ÑλÓÚRAMÖУ¬ÊÇÒ»¸öͨÓõÄÄÚ´æ³Ø¡£ËùÓеĶÔÏ󶼴洢ÔÚ¶ÑÖÐ
2£®ÉêÇ뷽ʽ
stack¡¾Õ»¡¿: ÓÉϵͳ×Ô¶¯·ÖÅä¡£ ÀýÈ磬ÉùÃ÷ÔÚº¯ÊýÖÐÒ»¸ö¾Ö²¿±äÁ¿ int b; ϵͳ×Ô¶¯ÔÚÕ»ÖÐΪb¿ª±Ù¿Õ¼ä ¡£
heap¡¾¶Ñ¡¿: ÐèÒª³ÌÐòÔ±×Ô¼ºÉêÇ룬²¢Ö¸Ã÷´óС£¬ÔÚcÖÐmallocº¯Êý Èçp1 = (char *)malloc(10); ÔÚC++ÖÐÓÃnewÔËËã·û Èçp2 = (char *)malloc(10); µ«ÊÇ×¢Ò⣺p1¡¢p2±¾ÉíÊÇÔÚÕ»Öеġ£
3£® ÉêÇëºóϵͳµÄÏìÓ¦
Õ»¡¾stack¡¿£ºÖ»ÒªÕ»µÄÊ£Óà¿Õ¼ä´óÓÚËùÉêÇë¿Õ¼ä£¬ÏµÍ³½«Îª³ÌÐòÌṩÄڴ棬·ñÔò½«±¨Òì³£ÌáʾջÒç³ö¡£
¶Ñ¡¾heap¡¿£ºÊ×ÏÈÓ¦¸ÃÖªµÀ²Ù×÷ϵͳÓÐÒ»¸ö¼Ç¼¿ÕÏÐÄÚ´æµØÖ·µÄÁ´±í£¬µ±ÏµÍ³ÊÕµ½³ÌÐòµÄÉêÇëʱ£¬»á±éÀú¸ÃÁ´±í£¬Ñ°ÕÒµÚÒ»¸ö¿Õ¼ä´óÓÚËùÉêÇë¿Õ¼äµÄ¶Ñ½áµã£¬È»ºó½«¸Ã½áµã´Ó¿ÕÏнáµãÁ´±íÖÐɾ³ý£¬²¢½«¸Ã½áµãµÄ¿Õ¼ä·ÖÅä¸ø³ÌÐò£»ÁíÍ⣬¶ÔÓÚ´ó¶àÊýϵͳ£¬»áÔÚÕâ¿éÄÚ´æ¿Õ¼äÖеÄÊ×µØÖ·´¦¼Ç¼±¾´Î·ÖÅäµÄ´óС£¬ÕâÑù£¬´úÂëÖеÄdeleteÓï¾ä²ÅÄÜÕýÈ·µÄÊͷű¾ÄÚ´æ¿Õ¼ä¡£ÁíÍ⣬ÓÉÓÚÕÒµ½µÄ¶Ñ½áµãµÄ´óС²»Ò»¶¨ÕýºÃµÈÓÚÉêÇëµÄ´óС£¬ÏµÍ³»á×Ô¶¯µÄ½«¶àÓàµÄÄDz¿·ÖÖØзÅÈë¿ÕÏÐÁ´±íÖС£
4£®ÉêÇë´óСµÄÏÞÖÆ
Õ»¡¾stack¡¿£ºÔÚWindowsÏ£¬Õ»ÊÇÏòµÍµØÖ·À©Õ¹µÄÊý¾Ý½á¹¹£¬ÊÇÒ»¿éÁ¬ÐøµÄÄÚ´æµÄÇøÓò¡£Õâ¾ä»°µÄÒâ˼ÊÇÕ»¶¥µÄµØÖ·ºÍÕ»µÄ×î´óÈÝÁ¿ÊÇϵͳԤÏȹ涨ºÃµÄ£¬ÔÚWINDOWSÏ£¬Õ»µÄ´óСÊÇ2M£¨Ò²ÓеÄ˵ÊÇ1M£¬×ÜÖ®ÊÇÒ»¸ö±àÒëʱ¾ÍÈ·¶¨µÄ³£Êý£©£¬Èç¹ûÉêÇëµÄ¿Õ¼ä³¬¹ýÕ»µÄÊ£Óà¿Õ¼äʱ£¬½«Ìáʾoverflow¡£Òò´Ë£¬ÄÜ´ÓÕ»»ñµÃµÄ¿Õ¼ä½ÏС¡£
¶Ñ¡¾heap¡¿£º¶ÑÊÇÏò¸ßµØÖ·À©Õ¹µÄÊý¾Ý½á¹¹£¬ÊDz»Á¬ÐøµÄÄÚ´æÇøÓò¡£ÕâÊÇÓÉÓÚϵͳÊÇÓÃÁ´±íÀ´´æ´¢µÄ¿ÕÏÐÄÚ´æµØÖ·µÄ£¬×ÔÈ»ÊDz»Á¬ÐøµÄ£¬¶øÁ´±íµÄ±éÀú·½ÏòÊÇÓɵ͵ØÖ·Ïò¸ßµØÖ·¡£¶ÑµÄ´óСÊÜÏÞÓÚ¼ÆËã»úϵͳÖÐÓÐЧµÄÐéÄâÄÚ´æ¡£Óɴ˿ɼû£¬¶Ñ»ñµÃµÄ¿Õ¼ä±È½ÏÁé»î£¬Ò²±È½Ï´ó¡£
5£®ÉêÇëЧÂʵıȽÏ
Õ»¡¾stack¡¿£ºÓÉϵͳ×Ô¶¯·ÖÅ䣬ËٶȽϿ졣µ«³ÌÐòÔ±ÊÇÎÞ·¨¿ØÖƵġ£
¶Ñ¡¾heap¡¿£ºÊÇÓÉnew·ÖÅäµÄÄڴ棬һ°ãËٶȱȽÏÂý£¬¶øÇÒÈÝÒײúÉúÄÚ´æËéƬ£¬²»¹ýÓÃÆðÀ´×î·½±ã.
ÁíÍ⣬ÔÚWINDOWSÏ£¬×îºÃµÄ·½Ê½ÊÇÓÃVirtualAlloc·ÖÅäÄڴ棬Ëû²»ÊÇÔڶѣ¬Ò²²»ÊÇÔÚÕ»ÊÇÖ±½ÓÔÚ½ø³ÌµÄµØÖ·¿Õ¼äÖб£ÁôÒ»¿ìÄڴ棬ËäÈ»ÓÃÆðÀ´×î²»·½±ã¡£µ«ÊÇËٶȿ죬Ҳ×îÁé»î¡£
6£®¶ÑºÍÕ»ÖеĴ洢ÄÚÈÝ
Õ»¡¾stack¡¿£ºÔÚº¯Êýµ÷ÓÃʱ£¬µÚÒ»¸ö½øÕ»µÄÊÇÖ÷º¯ÊýÖкóµÄÏÂÒ»ÌõÖ¸Áº¯Êýµ÷ÓÃÓï¾äµÄÏÂÒ»Ìõ¿ÉÖ´ÐÐÓï¾ä£©µÄµØÖ·£¬È»ºóÊǺ¯ÊýµÄ¸÷¸ö²ÎÊý£¬ÔÚ´ó¶àÊýµÄC±àÒëÆ÷ÖУ¬²ÎÊýÊÇÓÉÓÒÍù×óÈëÕ»µÄ£¬È»ºóÊǺ¯ÊýÖеľֲ¿±äÁ¿¡£×¢Ò⾲̬±äÁ¿ÊDz»ÈëÕ»µÄ¡£
µ±±¾´Îº¯Êýµ÷ÓýáÊøºó£¬¾Ö²¿±äÁ¿ÏȳöÕ»£¬È»ºóÊDzÎÊý£¬×îºóÕ»¶¥Ö¸ÕëÖ¸Ïò×ʼ´æµÄµØÖ·£¬Ò²¾ÍÊÇÖ÷º¯ÊýÖеÄÏÂÒ»ÌõÖ¸Á³ÌÐòÓɸõã¼ÌÐøÔËÐС£
¶Ñ¡¾heap¡¿£ºÒ»°ãÊÇÔڶѵÄÍ·²¿ÓÃÒ»¸ö×Ö½Ú´æ·Å¶ÑµÄ´óС¡£¶ÑÖеľßÌåÄÚÈÝÓгÌÐòÔ±°²ÅÅ¡£