<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>日本へ行きたい</title>
<link>https://ameblo.jp/uctta/</link>
<atom:link href="https://rssblog.ameba.jp/uctta/rss20.xml" rel="self" type="application/rss+xml" />
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
<description>日本語を勉強してます、よろしくお願いいたします。</description>
<language>ja</language>
<item>
<title>Oracle 用户（user)和模式(schema)的区别</title>
<description>
<![CDATA[ <h1><a href="https://www.cnblogs.com/dunjidan/p/4033573.html" id="cb_post_title_url">Oracle 用户（user)和模式(schema)的区别</a></h1><p><strong>概述：</strong></p><h2><strong>（一）什么Oracle叫用户（user）：</strong></h2><p>　　A user is a name defined in the database that can connect to and access objects.</p><p>大意：<em>Oracle用户是用连接数据库和访问数据库对象的。</em>（用户是用来连接数据库访问数据库）。</p><p><strong>（二）什么叫模式(schema)：</strong></p><p>　　A schema is a collection of database objects (used by a user.). Schema objects are the logical structures that directly refer to the database’s data.<br>大意：<em>模式是数据库对象的集合。模式对象是数据库数据的逻辑结构。</em>（把数据库对象用模式分开成不同的逻辑结构）。</p><p><strong>（三）用户（user）与模式(schema)的区别：</strong><br>　　Schemas and users help database administrators manage database security.<br>大意：<em>用户是用来连接数据库对象。而模式用是用创建管理对象的。</em>(模式跟用户在oracle 是一对一的关系。)</p><p>&nbsp;</p><hr><p>&nbsp;</p><p><strong>详解：</strong></p><p>　　从官方的定义中，我们可以看出schema为数据库对象的集合。为了区分各个集合，我们需要给这个集合起个名字，这些名字就是我们在企业管理器的方案下看到 的许多类似用户名的节点，这些类似用户名的节点其实就是一个schema。</p><p>　　schema里面包含了各种对象如tables, views, sequences, stored procedures, synonyms, indexes, clusters, and database links。</p><p>　　</p><p>　　一个用户一般对应一个schema,该用户的schema名等于用户名，并作为该用户缺省schema。这也就是我们在企业管理器的方案下看 到schema名都为数据库用户名的原因。而Oracle数据库中不能新创建一个schema，要想创建一个schema，只能通过创建一个用户的方法解决 (Oracle中虽然有create schema语句，但是它并不是用来创建一个schema的)，在创建一个用户的同时为这个用户创建一个与用户名同名的schem并作为该用户的缺省 shcema。</p><p>　　即schema的个数同user的个数相同，而且schema名字同user名字一一 对应并且相同，所有我们可以称schema为user的别名，虽然这样说并不准确，但是更容易理解一些。</p><p>　　一个用户有一个缺省的schema，其schema名就等于用户名，当然一个用户还可以使用其他的schema。如果我们访问一个表时，没有指明该 表属于哪一个schema中的，系统就会自动给我们在表上加上缺省的sheman名。比如我们在访问数据库时，访问scott用户下的emp表，通过 select * from emp; 其实，这sql语句的完整写法为select * from scott.emp。</p><p>　　在数据库中一个对象的完整名称为schema.object，而不属user.object。类似如果我们在创建对象时不指定该对象 的schema，在该对象的schema为用户的缺省schema。这就像一个用户有一个缺省的表空间，但是该用户还可以使用其他的表空间，如果我们在创 建对象时不指定表空间，则对象存储在缺省表空间中，要想让对象存储在其他表空间中，我们需要在创建对象时指定该对象的表空间。</p>
]]>
</description>
<link>https://ameblo.jp/uctta/entry-12450651256.html</link>
<pubDate>Sat, 30 Mar 2019 14:49:36 +0900</pubDate>
</item>
<item>
<title>Java中List和ArrayList的区别</title>
<description>
<![CDATA[ <p>转载自：http://www.cnblogs.com/aisiteru/articles/1151874.html</p><p>&nbsp; &nbsp; &nbsp; &nbsp;第一次看这篇文章时，是在CSDN博客中看到的，作者写的初衷还是蛮好的，但是确实有错误的地方和不是很明</p><p>白的地方。于是就很想去看看原文，废了半天的功夫终于找到了，原文还是一样有出错和我不理解的地方，我也把原</p><p>文的地址贴在上面了。三月份学习的Java集合框架这部分，这几天拿出来整理就想再深入一点，因此也看了很多的关</p><p>于Java集合框架的文章，这篇我算是有一点点的体会，我只是改了一些我认为错误的地方和不通顺的地方。</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;改过的原文：</p><p>&nbsp; &nbsp; &nbsp; &nbsp;List是一个接口，而ArrayList是List接口的一个实现类。&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;ArrayList类继承并实现了List接口。&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;因此，List接口不能被构造，也就是我们说的不能创建实例对象，但是我们可以像下面那样为List接口创建一个指</p><p>向自己的对象引用，而ArrayList实现类的实例对象就在这充当了这个指向List接口的对象引用。&nbsp;</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;个人见解：</p><p>&nbsp; &nbsp; &nbsp; &nbsp;要是你已经学过了OOP，上面的部分是不难理解的，这是面向对象重要的知识点，面向对象最重要的就是多态，</p><p>我们都知道接口和抽象不能被实例化，但是它们可以创建一个指向自己的对象引用，它们的实现类或子类就在充当这</p><p>样的角色，我想这就是面向对象编程中多态的优势。前些日子在学习UML建模语言和Java设计模式的时候，深深地的</p><p>体会到了面向对象编程的好处，Java集合框架中用到也不足为奇，Java本身就是面向对象的编程语言。</p><p>&nbsp; &nbsp; &nbsp; &nbsp;上面的理解可能有点难度，但是我们找一个具体的实例，就会理解起来比较容易。我们定义一个动物的抽象类</p><p>Animal，再定义一个继承自Animal基类的Dog类，看下面的代码就会理解抽象类和接口不能被实例化：</p><p>&nbsp;</p><p><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="view plain">view plain</a>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="copy">copy</a></p><p>&nbsp;</p><ol start="1"><li>&lt;span&nbsp;style="font-size:18px;"&gt;public&nbsp;class&nbsp;Test{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animal&nbsp;a1&nbsp;=&nbsp;new&nbsp;Animal();//编译出错&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animal&nbsp;a2&nbsp;=&nbsp;new&nbsp;Dog();&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;</li><li>&nbsp;&nbsp;</li><li>abstract&nbsp;class&nbsp;Animal{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//动物名字&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//动物叫声&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;shout(){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("叫声...");&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;</li><li>&nbsp;&nbsp;</li><li>class&nbsp;Dog&nbsp;extends&nbsp;Animal{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//狗类独有的方法&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;guard(){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("狗有看门的独特本领！");&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&lt;/span&gt;&nbsp;&nbsp;</li></ol><p>&nbsp; &nbsp; &nbsp; &nbsp; 编译结果：</p><p>&nbsp;</p><p><img alt="" src="https://img-proxy.blog-video.jp/images?url=http%3A%2F%2Fimg.blog.csdn.net%2F20160502220219202%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQv%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter"></p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;List list;//正确，list = null;&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;List list = new List();//是错误的用法</p><p>&nbsp; &nbsp; &nbsp; &nbsp;List list = new ArrayList();这句创建了一个ArrayList实现类的对象后把它上溯到了List接口。此时它就是一个List对</p><p>象了，它有些ArrayList类具有的，但是List接口没有的属性和方法，它就不能再用了。 而ArrayList list=newArrayList();</p><p>创建一对象则保留了ArrayList的所有属性和方法。&nbsp;</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;个人见解</p><p>&nbsp; &nbsp; &nbsp; &nbsp;我们继续上面的例子，如果我们创建的是抽象类的对象引用，那么这个对象只能调用自己的非抽象方法，下面的</p><p>是shout()方法，不能调用继承它的子类的独有的方法，在下面的就是guard()方法不能被a1调用，继续测试代码：</p><p>&nbsp;</p><p><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="view plain">view plain</a>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="copy">copy</a></p><p>&nbsp;</p><ol start="1"><li>&lt;span&nbsp;style="font-size:18px;"&gt;public&nbsp;class&nbsp;Test{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animal&nbsp;a1&nbsp;=&nbsp;new&nbsp;Dog();&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a1.shout();//编译通过&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//a1.guard();//编译出错&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;</li><li>&nbsp;&nbsp;</li><li>abstract&nbsp;class&nbsp;Animal{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//动物名字&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//动物叫声&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;shout(){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("叫声...");&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;</li><li>&nbsp;&nbsp;</li><li>class&nbsp;Dog&nbsp;extends&nbsp;Animal{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;//狗类独有的方法&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;guard(){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("狗有看门的独特本领！");&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;</li><li>&lt;/span&gt;&nbsp;&nbsp;</li></ol><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;编译结果：</p><p><img alt="" src="https://img-proxy.blog-video.jp/images?url=http%3A%2F%2Fimg.blog.csdn.net%2F20160502220153607%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQv%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter"></p><p>&nbsp; &nbsp; &nbsp; &nbsp;如果我们采用Dog d1 = new Dog();那么d1可以调用抽象类和子类的所有属性和方法，这里不再测试。</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;这是一个例子：&nbsp;</p><p>&nbsp;</p><p><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="view plain">view plain</a>&nbsp;<a href="http://blog.csdn.net/erlian1992/article/details/51298276#" title="copy">copy</a></p><p>&nbsp;</p><ol start="1"><li>&lt;span&nbsp;style="font-size:18px;"&gt;import&nbsp;java.util.*;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;</li><li>public&nbsp;class&nbsp;Demo{&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args){&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList();&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayList&nbsp;arrayList&nbsp;=&nbsp;new&nbsp;ArrayList();&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.trimToSize();//错误，没有该方法。&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrayList.trimToSize();//ArrayList里有该方法。&nbsp;&nbsp;</li><li>}&nbsp;&nbsp;&nbsp;</li><li>}&lt;/span&gt;&nbsp;&nbsp;</li></ol><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;编译一下就知道结果了。</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;个人见解</p><p>&nbsp; &nbsp; &nbsp; &nbsp;我刚在前面的文章里面把Java API中List接口和ArrayList实现类的方法都列出来了，可以看到List接口中并没有</p><p>trimToSize()方法，但这个方法在它的实现类ArrayList中有。因此编译的结果为：</p><p>&nbsp;<img alt="" src="https://img-proxy.blog-video.jp/images?url=http%3A%2F%2Fimg.blog.csdn.net%2F20160502220700833%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQv%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FCenter"></p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 如果是下面这个样子的：&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; List a=new ArrayList();&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 则a拥有List的所有属性和方法，不会拥有其实现类ArrayList的独有的属性和方法。&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 如果List与ArrayList中有相同的属性(如int i),有相同的方法(如void f()),&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 则a.i是调用了List中的i&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; a.f()是调用了ArrayList中的f();&nbsp;</p><p>----------------------------------------------------------------------------------------------------------------</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 问题的关键:&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 为什么要用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢？&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 问题就在于List接口有多个实现类，现在你用的是ArrayList，也许哪一天你需要换成其它的实现类，如&nbsp;</p><p>LinkedList或者Vector等等，这时你只要改变这一行就行了：&nbsp;List list = new LinkedList(); 其它使用了list地方的代码根</p><p>本不需要改动。&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;假设你开始用ArrayList alist = new ArrayList(), 这下你有的改了，特别是如果你使用了ArrayList实现类特有的方法</p><p>和属性。</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;个人见解</p><p>&nbsp; &nbsp; &nbsp; &nbsp;上面的说明，我是在看了设计模式才恍然大悟的，设计模式的原则和理念果然是强大的，只有好好学习了面向对</p><p>象原则和设计模式，那么理解上面的就不再是难度，这样的好处是为了代码的可维护性，可复用性，可扩展性以及灵</p><p>活性，再者就是这符合了里氏代换原则和开闭原则。看来学习设计模式好处是大大的。</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;地区用List arr = new ArrayList();定义;行业用ArrayList arr = new ArrayList();定义;则说明,行业里用到了ArrayList的</p><p>特殊的方法.</p><p>&nbsp; &nbsp; &nbsp; &nbsp;另外的例子就是,在类的方法中,如下声明:</p><p>&nbsp; &nbsp; &nbsp; &nbsp; private void doMyAction(List list){}</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 这样这个方法能处理所有实现了List接口的类,一定程度上实现了泛型函数.</p><p>&nbsp; &nbsp; &nbsp; &nbsp;如果开发的时候觉得ArrayList,HashMap的性能不能满足你的需要,可以通过实现List,Map(或者Collection)来定制</p><p>你的自定义类.</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;个人见解</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 正因为List是接口，所以它的扩展性是良好的，这是面向对象编程最大的改变，也是它的核心，在这里我是体会</p><p>到了一句话，就是Java集合框架的学习最难体现你学习Java语言的程序都多深，看来我以前学习的程度只是停留在入</p><p>门级别了，这几天可要好好再温习重新认识一番Java集合框架了。</p><p>&nbsp; &nbsp; &nbsp; &nbsp; 可能理解不是很深，对于原文的理解也就这些了，不对的地方自己会及时更正。</p>
]]>
</description>
<link>https://ameblo.jp/uctta/entry-12450612195.html</link>
<pubDate>Sat, 30 Mar 2019 11:15:37 +0900</pubDate>
</item>
<item>
<title>堆和栈的区别</title>
<description>
<![CDATA[ <p><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">一、预备知识—程序的内存分配</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">一个由c/C++编译的程序占用的内存分为以下几个部分</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">1、栈区（stack）—&nbsp;由编译器自动分配释放&nbsp;，存放函数的参数值，局部变量的值等。其操作方式类似于数据结构中的栈。</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">2、堆区（heap）&nbsp;—&nbsp;一般由程序员分配释放，&nbsp;若程序员不释放，程序结束时可能由OS回收&nbsp;。注意它与数据结构中的堆是两回事，分配方式倒是类似于链表，呵呵。</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">3、全局区（静态区）（static）—，全局变量和静态变量的存储是放在一块的，初始化的全局变量和静态变量在一块区域，&nbsp;未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。&nbsp;-&nbsp;程序结束后有系统释放&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">4、文字常量区—常量字符串就是放在这里的。&nbsp;程序结束后由系统释放</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">5、程序代码区—存放函数体的二进制代码。</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">二、例子程序&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">这是一个前辈写的，非常详细&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">//main.cpp&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">int&nbsp;a&nbsp;=&nbsp;0;&nbsp;全局初始化区&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">char&nbsp;*p1;&nbsp;全局未初始化区&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">main()&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">{&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">int&nbsp;b;&nbsp;栈&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">char&nbsp;s[]&nbsp;=&nbsp;"abc";&nbsp;栈&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">char&nbsp;*p2;&nbsp;栈&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">char&nbsp;*p3&nbsp;=&nbsp;"123456";&nbsp;123456\0在常量区，p3在栈上。&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">static&nbsp;int&nbsp;c&nbsp;=0；&nbsp;全局（静态）初始化区&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">p1&nbsp;=&nbsp;(char&nbsp;*)malloc(10);&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">p2&nbsp;=&nbsp;(char&nbsp;*)malloc(20);&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">分配得来得10和20字节的区域就在堆区。&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">strcpy(p1,&nbsp;"123456");&nbsp;123456\0放在常量区，编译器可能会将它与p3所指向的"123456"优化成一个地方。&nbsp;</span><br style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><span style="color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">}</span></p><p>&nbsp;</p><p>&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">二、堆和栈的理论知识&nbsp;<br style="margin: 0px; padding: 0px;">2.1申请方式&nbsp;<br style="margin: 0px; padding: 0px;">stack:&nbsp;<br style="margin: 0px; padding: 0px;">由系统自动分配。&nbsp;例如，声明在函数中一个局部变量&nbsp;int&nbsp;b;&nbsp;系统自动在栈中为b开辟空间&nbsp;<br style="margin: 0px; padding: 0px;">heap:&nbsp;<br style="margin: 0px; padding: 0px;">需要程序员自己申请，并指明大小，在c中malloc函数&nbsp;<br style="margin: 0px; padding: 0px;">如p1&nbsp;=&nbsp;(char&nbsp;*)malloc(10);&nbsp;<br style="margin: 0px; padding: 0px;">在C++中用new运算符&nbsp;<br style="margin: 0px; padding: 0px;">如p2&nbsp;=&nbsp;(char&nbsp;*)malloc(10);&nbsp;<br style="margin: 0px; padding: 0px;">但是注意p1、p2本身是在栈中的。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><br style="margin: 0px; padding: 0px;">2.2&nbsp;<br style="margin: 0px; padding: 0px;">申请后系统的响应&nbsp;<br style="margin: 0px; padding: 0px;">栈：只要栈的剩余空间大于所申请空间，系统将为程序提供内存，否则将报异常提示栈溢出。&nbsp;<br style="margin: 0px; padding: 0px;">堆：首先应该知道操作系统有一个记录空闲内存地址的链表，当系统收到程序的申请时，&nbsp;<br style="margin: 0px; padding: 0px;">会遍历该链表，寻找第一个空间大于所申请空间的堆结点，然后将该结点从空闲结点链表中删除，并将该结点的空间分配给程序，另外，对于大多数系统，会在这块内存空间中的首地址处记录本次分配的大小，这样，代码中的delete语句才能正确的释放本内存空间。另外，由于找到的堆结点的大小不一定正好等于申请的大小，系统会自动的将多余的那部分重新放入空闲链表中。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">2.3申请大小的限制&nbsp;<br style="margin: 0px; padding: 0px;">栈：在Windows下,栈是向低地址扩展的数据结构，是一块连续的内存的区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的，在WINDOWS下，栈的大小是2M（也有的说是1M，总之是一个编译时就确定的常数），如果申请的空间超过栈的剩余空间时，将提示overflow。因此，能从栈获得的空间较小。&nbsp;<br style="margin: 0px; padding: 0px;">堆：堆是向高地址扩展的数据结构，是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的，自然是不连续的，而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见，堆获得的空间比较灵活，也比较大。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><br style="margin: 0px; padding: 0px;">2.4申请效率的比较：&nbsp;<br style="margin: 0px; padding: 0px;">栈由系统自动分配，速度较快。但程序员是无法控制的。&nbsp;<br style="margin: 0px; padding: 0px;">堆是由new分配的内存，一般速度比较慢，而且容易产生内存碎片,不过用起来最方便.&nbsp;<br style="margin: 0px; padding: 0px;">另外，在WINDOWS下，最好的方式是用VirtualAlloc分配内存，他不是在堆，也不是在栈是直接在进程的地址空间中保留一快内存，虽然用起来最不方便。但是速度快，也最灵活。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">2.5堆和栈中的存储内容&nbsp;<br style="margin: 0px; padding: 0px;">栈：&nbsp;在函数调用时，第一个进栈的是主函数中后的下一条指令（函数调用语句的下一条可执行语句）的地址，然后是函数的各个参数，在大多数的C编译器中，参数是由右往左入栈的，然后是函数中的局部变量。注意静态变量是不入栈的。&nbsp;<br style="margin: 0px; padding: 0px;">当本次函数调用结束后，局部变量先出栈，然后是参数，最后栈顶指针指向最开始存的地址，也就是主函数中的下一条指令，程序由该点继续运行。&nbsp;<br style="margin: 0px; padding: 0px;">堆：一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容有程序员安排。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">2.6存取效率的比较&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">char&nbsp;s1[]&nbsp;=&nbsp;"aaaaaaaaaaaaaaa";&nbsp;<br style="margin: 0px; padding: 0px;">char&nbsp;*s2&nbsp;=&nbsp;"bbbbbbbbbbbbbbbbb";&nbsp;<br style="margin: 0px; padding: 0px;">aaaaaaaaaaa是在运行时刻赋值的；&nbsp;<br style="margin: 0px; padding: 0px;">而bbbbbbbbbbb是在编译时就确定的；&nbsp;<br style="margin: 0px; padding: 0px;">但是，在以后的存取中，在栈上的数组比指针所指向的字符串(例如堆)快。&nbsp;<br style="margin: 0px; padding: 0px;">比如：&nbsp;<br style="margin: 0px; padding: 0px;">#include&nbsp;<br style="margin: 0px; padding: 0px;">void&nbsp;main()&nbsp;<br style="margin: 0px; padding: 0px;">{&nbsp;<br style="margin: 0px; padding: 0px;">char&nbsp;a&nbsp;=&nbsp;1;&nbsp;<br style="margin: 0px; padding: 0px;">char&nbsp;c[]&nbsp;=&nbsp;"1234567890";&nbsp;<br style="margin: 0px; padding: 0px;">char&nbsp;*p&nbsp;="1234567890";&nbsp;<br style="margin: 0px; padding: 0px;">a&nbsp;=&nbsp;c[1];&nbsp;<br style="margin: 0px; padding: 0px;">a&nbsp;=&nbsp;p[1];&nbsp;<br style="margin: 0px; padding: 0px;">return;&nbsp;<br style="margin: 0px; padding: 0px;">}&nbsp;<br style="margin: 0px; padding: 0px;">对应的汇编代码&nbsp;<br style="margin: 0px; padding: 0px;">10:&nbsp;a&nbsp;=&nbsp;c[1];&nbsp;<br style="margin: 0px; padding: 0px;">00401067&nbsp;8A&nbsp;4D&nbsp;F1&nbsp;mov&nbsp;cl,byte&nbsp;ptr&nbsp;[ebp-0Fh]&nbsp;<br style="margin: 0px; padding: 0px;">0040106A&nbsp;88&nbsp;4D&nbsp;FC&nbsp;mov&nbsp;byte&nbsp;ptr&nbsp;[ebp-4],cl&nbsp;<br style="margin: 0px; padding: 0px;">11:&nbsp;a&nbsp;=&nbsp;p[1];&nbsp;<br style="margin: 0px; padding: 0px;">0040106D&nbsp;8B&nbsp;55&nbsp;EC&nbsp;mov&nbsp;edx,dword&nbsp;ptr&nbsp;[ebp-14h]&nbsp;<br style="margin: 0px; padding: 0px;">00401070&nbsp;8A&nbsp;42&nbsp;01&nbsp;mov&nbsp;al,byte&nbsp;ptr&nbsp;[edx+1]&nbsp;<br style="margin: 0px; padding: 0px;">00401073&nbsp;88&nbsp;45&nbsp;FC&nbsp;mov&nbsp;byte&nbsp;ptr&nbsp;[ebp-4],al&nbsp;<br style="margin: 0px; padding: 0px;">第一种在读取时直接就把字符串中的元素读到寄存器cl中，而第二种则要先把指针值读到edx中，在根据edx读取字符，显然慢了。&nbsp;</p><p style="margin: 10px auto; padding: 0px; text-indent: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;"><br style="margin: 0px; padding: 0px;">2.7小结：&nbsp;<br style="margin: 0px; padding: 0px;">堆和栈的区别可以用如下的比喻来看出：&nbsp;<br style="margin: 0px; padding: 0px;">使用栈就象我们去饭馆里吃饭，只管点菜（发出申请）、付钱、和吃（使用），吃饱了就走，不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作，他的好处是快捷，但是自由度小。&nbsp;<br style="margin: 0px; padding: 0px;">使用堆就象是自己动手做喜欢吃的菜肴，比较麻烦，但是比较符合自己的口味，而且自由度大。</p>
]]>
</description>
<link>https://ameblo.jp/uctta/entry-12448435579.html</link>
<pubDate>Wed, 20 Mar 2019 23:46:58 +0900</pubDate>
</item>
</channel>
</rss>
