📄 [c++对象模型][5]堆栈与函数调用 - itech's blog - 博客园.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0060)http://www.cnblogs.com/itech/archive/2009/02/23/1395515.html -->
<HTML><HEAD id=Head><TITLE>[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META id=metaKeywords content=[Cpp对象模型],C,对象模型,5,堆栈与函数调用 name=keywords>
<META
content=一C++程序内存分配1)在栈上创建。在执行函数时,函数内局部变量的存储单元都在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,一般使用寄存器来存取,效率很高,但是分配的内存容量有限。2)从堆上分配,亦称动态内存分配。程序在运行的时候用mallo
name=description><LINK id=CommondCss
href="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/common.css" type=text/css
rel=stylesheet><LINK id=MainCss
href="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/style.css" type=text/css
rel=stylesheet><LINK id=CommondCss2
href="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/common2.css" type=text/css
rel=stylesheet><LINK id=RSSLink title=RSS
href="http://www.cnblogs.com/itech/rss" type=application/rss+xml
rel=alternate><LINK id=RSDLink title=RSD
href="http://www.cnblogs.com/itech/rsd.xml" type=application/rsd+xml
rel=EditURI>
<SCRIPT src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/common.js"
type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2900.3492" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 onsubmit="javascript:return WebForm_OnSubmit();"
action=1395515.html method=post>
<DIV><INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT
id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT
id=" __VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV>
<SCRIPT type=text/javascript>
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</SCRIPT>
<SCRIPT src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/WebResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript>
function ctlent(evt,id)
{
if(evt.ctrlKey && evt.keyCode == 13)
{
try
{
TempSave(id);
}
catch(ex)
{
}
finally
{
__doPostBack('AjaxHolder$PostComment$btnSubmit','')
}
}
}</SCRIPT>
<SCRIPT language=JavaScript>function SetReplyAuhor(author){document.getElementById('AjaxHolder_PostComment_tbComment').value+="@"+author+"\n";document.getElementById('AjaxHolder_PostComment_tbComment').focus();return false}</SCRIPT>
<SCRIPT src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/ScriptResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="D:\c++内存模型\[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files\ScriptResource(1).axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="D:\c++内存模型\[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files\ScriptResource(2).axd"
type=text/javascript></SCRIPT>
<SCRIPT src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/js"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</SCRIPT>
<!--done-->
<DIV id=home>
<DIV id=header>
<DIV id=blogTitle><A id=lnkBlogLogo href="http://www.cnblogs.com/itech/"><IMG
id=blogLogo alt=返回主页
src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/logo.png"></A> <!--done-->
<H1><A class=headermaintitle id=Header1_HeaderTitle
href="http://www.cnblogs.com/itech/">iTech's Blog</A></H1>
<H2>编程语言(C++,C++/CLI,C#&Python)架构设计(Design
pattern&Architecture)系统内核(Windows&Linux)!<A
href="http://www.cnblogs.com/itech/articles/1363498.html"><IMG
src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/o_icon_sub_c1s17.gif"></A>
</H2></DIV><!--end: blogTitle 博客的标题和副标题 -->
<DIV style="DISPLAY: none"><IMG
src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/body.gif"></DIV>
<DIV id=navigator><!--done-->
<UL id=navList>
<LI><A class=menu id=MyLinks1_HomeLink href="http://www.cnblogs.com/">博客园</A>
</LI>
<LI><A class=menu id=MyLinks1_SpaceLink
href="http://space.cnblogs.com/">社区</A> </LI>
<LI><A class=menu id=MyLinks1_MyHomeLink
href="http://www.cnblogs.com/itech/">首页</A> </LI>
<LI><A class=menu id=MyLinks1_NewPostLink
href="http://www.cnblogs.com/itech/admin/EditPosts.aspx?opt=1">新随笔</A> </LI>
<LI><A class=menu id=MyLinks1_ContactLink accessKey=9
href="http://www.cnblogs.com/itech/contact.aspx?id=1">联系</A> </LI>
<LI><A class=menu id=MyLinks1_Admin
href="http://www.cnblogs.com/itech/admin/EditPosts.aspx">管理</A> </LI>
<LI><A class=menu id=MyLinks1_Syndication
href="http://www.cnblogs.com/itech/rss">订阅</A> <A class=aHeaderXML
id=MyLinks1_XMLLink href="http://www.cnblogs.com/itech/rss"><IMG
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
alt=订阅 src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/rss.gif"></A>
</LI></UL>
<DIV class=blogStats><!--done-->随笔- 17 文章- 14 评论- 5 </DIV><!--end: blogStats --></DIV><!--end: navigator 博客导航栏 --></DIV><!--end: header 头部 -->
<DIV id=main>
<DIV id=topicList>
<DIV class=forFlow>
<SCRIPT type=text/javascript>
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
</SCRIPT>
<!--done-->
<DIV id=topics>
<DIV class=post>
<H1 class=postTitle><A class=postTitle2 id=AjaxHolder_ctl01_TitleUrl
href="http://www.cnblogs.com/itech/archive/2009/02/23/1395515.html">[C++对象模型][5]堆栈与函数调用</A>
</H1>
<DIV class=clear></DIV>
<DIV class=postBody>
<P>一 C++程序内存分配</P>
<P><SPAN style="COLOR: #ff0000">1)
在栈上创建。在执行函数时,函数内局部变量的存储单元都在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,一般使用寄存器来存取,效率很高,但是分配的内存容量有限。
<BR>2)
从堆上分配,亦称动态内存分配。程序在运行的时候用malloc或new申请任意多少的内存,程序员自己负责在何时用free或delete来释放内存。动态内存的生存期由程序员自己决定,使用非常灵活。
<BR>3) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。 <BR>4)
文字常量分配在文字常量区,程序结束后由系统释放。 <BR>5)程序代码区。 </SPAN></P>
<P>经典实例:</P>
<P> </P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_170313 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_170313').style.display='none'; document.getElementById('Code_Open_Image_170313').style.display='inline'; document.getElementById('Code_Open_Text_170313').style.display='inline';"
height=16
src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_170313
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_170313').style.display='none'; getElementById('Code_Closed_Image_170313').style.display='inline'; getElementById('Code_Closed_Text_170313').style.display='inline';"
height=16
src="[C++对象模型][5]堆栈与函数调用 - iTech's Blog - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_170313>Code</SPAN><SPAN id=Code_Open_Text_170313> <BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN
style="COLOR: #000000">#include </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">></SPAN><SPAN style="COLOR: #000000">
<BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> a</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">;
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">全局初始化区</SPAN><SPAN style="COLOR: #008000">
<BR></SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">p1; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">全局未初始化区</SPAN><SPAN
style="COLOR: #008000"> <BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> main() <BR>{
<BR> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> b;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">栈</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> s[]</SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">abc</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">栈</SPAN><SPAN
style="COLOR: #008000"> <BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">p2;
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">栈</SPAN><SPAN style="COLOR: #008000"> <BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">p3</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">123456</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">;
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">123456\0在常量区,p3在栈上。</SPAN><SPAN style="COLOR: #008000">
<BR></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">static</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> c</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">;
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">全局(静态)初始化区</SPAN><SPAN style="COLOR: #008000">
<BR></SPAN><SPAN style="COLOR: #000000"> p1 </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)malloc(</SPAN><SPAN
style="COLOR: #800080">10</SPAN><SPAN style="COLOR: #000000">);
<BR> p2 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)malloc(</SPAN><SPAN
style="COLOR: #800080">20</SPAN><SPAN style="COLOR: #000000">);
</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">分配得来得10和20字节的区域就在堆区。</SPAN><SPAN style="COLOR: #008000">
<BR></SPAN><SPAN style="COLOR: #000000">
strcpy(p1,</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -