﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-浩浩的家园</title><link>http://www.cppblog.com/wanghao111/</link><description>长征路上</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:10:07 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:10:07 GMT</pubDate><ttl>60</ttl><item><title>C++程序对内存的占用</title><link>http://www.cppblog.com/wanghao111/archive/2009/04/09/79313.html</link><dc:creator>王浩</dc:creator><author>王浩</author><pubDate>Thu, 09 Apr 2009 00:59:00 GMT</pubDate><guid>http://www.cppblog.com/wanghao111/archive/2009/04/09/79313.html</guid><wfw:comment>http://www.cppblog.com/wanghao111/comments/79313.html</wfw:comment><comments>http://www.cppblog.com/wanghao111/archive/2009/04/09/79313.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghao111/comments/commentRss/79313.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghao111/services/trackbacks/79313.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #008000">//</span><span style="COLOR: #008000">main.cpp&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;a&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">a,全局变量,存储在全局初始化区&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p1;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">p1,全局变量指针,全局未初始化区(注意,p1没有new或者malloc,它是一个空指针,你乱用就是野指针,但是它是地址,本身也占32位的空间)&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;&nbsp;main()<br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000">&nbsp;{<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;b;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">main()函数的栈区&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;s[]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">abc</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">main()函数的栈区&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p2;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;p2也是指针,main()函数的栈区&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">p3&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">123456</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">123456\0在常量区，p3在main()函数的栈区&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">；&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">全局（静态）初始化区&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p1&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);&nbsp;<br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p2&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)malloc(</span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">分配得来得10和20字节的区域就在堆区。但是p1在全局区,p2在main()函数的栈区,你懂了吗?&nbsp;</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(p1,&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">123456</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;123456\0放在常量区，编译器可能会将它与p3所指向&nbsp;的"123456"优化成一个地方</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;}</span></div>
<br>上面的一小段代码,可以看出,一段代码占用内存,基本有一下几种: <br>1、栈区（stack）— 由编译器自动分配释放 ，存放函数的参数值，局部变量的值等。其操作方式类似于数据结构中的栈。 <br>2、堆区（heap） — 一般由程序员分配释放， 若程序员不释放，程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事，分配方式倒是类似于链表。 <br>3、全局区（静态区）（static）—，全局变量和静态变量的存储是放在一块的，初始化的全局变量和静态变量在一块区域， 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后由系统释放。 <br>4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放 。 <br>5、程序代码区—存放函数体的二进制代码。 
<img src ="http://www.cppblog.com/wanghao111/aggbug/79313.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghao111/" target="_blank">王浩</a> 2009-04-09 08:59 <a href="http://www.cppblog.com/wanghao111/archive/2009/04/09/79313.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>给大学生的48条忠告</title><link>http://www.cppblog.com/wanghao111/archive/2009/04/08/79277.html</link><dc:creator>王浩</dc:creator><author>王浩</author><pubDate>Wed, 08 Apr 2009 08:30:00 GMT</pubDate><guid>http://www.cppblog.com/wanghao111/archive/2009/04/08/79277.html</guid><wfw:comment>http://www.cppblog.com/wanghao111/comments/79277.html</wfw:comment><comments>http://www.cppblog.com/wanghao111/archive/2009/04/08/79277.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/wanghao111/comments/commentRss/79277.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wanghao111/services/trackbacks/79277.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 给大学生的48条忠告&nbsp;&nbsp;<a href='http://www.cppblog.com/wanghao111/archive/2009/04/08/79277.html'>阅读全文</a><img src ="http://www.cppblog.com/wanghao111/aggbug/79277.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wanghao111/" target="_blank">王浩</a> 2009-04-08 16:30 <a href="http://www.cppblog.com/wanghao111/archive/2009/04/08/79277.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>