亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch08.htm

?? good book for learning c++ standard language
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
	<P>
	<LI>The free store
	<P>
	<LI>Registers
	<P>
	<LI>Code space
	<P>
	<LI>The stack
</UL>

<P>Local variables are on the stack, along with function parameters. Code is in code
space, of course, and global variables are in global name space. The registers are
used for internal housekeeping functions, such as keeping track of the top of the
stack and the instruction pointer. Just about all remaining memory is given over
to the free store, which is sometimes referred to as the heap.</P>
<P>The problem with local variables is that they don't persist: When the function
returns, the local variables are thrown away. Global variables solve that problem
at the cost of unrestricted access throughout the program, which leads to the creation
of code that is difficult to understand and maintain. Putting data in the free store
solves both of these problems.</P>
<P>You can think of the free store as a massive section of memory in which thousands
of sequentially numbered cubbyholes lie waiting for your data. You can't label these
cubbyholes, though, as you can with the stack. You must ask for the address of the
cubbyhole that you reserve and then stash that address away in a pointer.</P>
<P>One way to think about this is with an analogy: A friend gives you the 800 number
for Acme Mail Order. You go home and program your telephone with that number, and
then you throw away the piece of paper with the number on it. If you push the button,
a telephone rings somewhere, and Acme Mail Order answers. You don't remember the
number, and you don't know where the other telephone is located, but the button gives
you access to Acme Mail Order. Acme Mail Order is your data on the free store. You
don't know where it is, but you know how to get to it. You access it by using its
address--in this case, the telephone number. You don't have to know that number;
you just have to put it into a pointer (the button). The pointer gives you access
to your data without bothering you with the details.</P>
<P>The stack is cleaned automatically when a function returns. All the local variables
go out of scope, and they are removed from the stack. The free store is not cleaned
until your program ends, and it is your responsibility to free any memory that you've
reserved when you are done with it.</P>
<P>The advantage to the free store is that the memory you reserve remains available
until you explicitly free it. If you reserve memory on the free store while in a
function, the memory is still available when the function returns.</P>
<P>The advantage of accessing memory in this way, rather than using global variables,
is that only functions with access to the pointer have access to the data. This provides
a tightly controlled interface to that data, and it eliminates the problem of one
function changing that data in unexpected and unanticipated ways.</P>
<P>For this to work, you must be able to create a pointer to an area on the free
store and to pass that pointer among functions. The following sections describe how
to do this.
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">new</FONT></H4>
<P>You allocate memory on the free store in C++ by using the <TT>new</TT> keyword.
<TT>new</TT> is followed by the type of the object that you want to allocate so that
the compiler knows how much memory is required. Therefore, <TT>new unsigned short
int</TT> allocates two bytes in the free store, and <TT>new long</TT> allocates four.</P>
<P>The return value from <TT>new</TT> is a memory address. It must be assigned to
a pointer. To create an <TT>unsigned</TT> <TT>short</TT> on the free store, you might
write</P>
<PRE><FONT COLOR="#0066FF">unsigned short int * pPointer;
pPointer = new unsigned short int;
</FONT></PRE>
<P>You can, of course, initialize the pointer at its creation with</P>
<PRE><FONT COLOR="#0066FF">unsigned short int * pPointer = new unsigned short int;
</FONT></PRE>
<P>In either case, <TT>pPointer</TT> now points to an <TT>unsigned short int</TT>
on the free store. You can use this like any other pointer to a variable and assign
a value into that area of memory by writing</P>
<PRE><FONT COLOR="#0066FF">*pPointer = 72;
</FONT></PRE>
<P>This means, &quot;Put 72 at the value in <TT>pPointer</TT>,&quot; or &quot;Assign
the value <TT>72</TT> to the area on the free store to which <TT>pPointer</TT> points.&quot;</P>
<P>If <TT>new</TT> cannot create memory on the free store (memory is, after all,
a limited resource) it returns the null pointer. You must check your pointer for
null each time you request new memory.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>Each time you allocate memory
	using the <TT>new</TT> keyword, you must check to make sure the pointer is not null.
	
<HR>


</BLOCKQUOTE>

<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">delete</FONT></H4>
<P>When you are finished with your area of memory, you must call <TT>delete</TT>
on the pointer. <TT>delete</TT> returns the memory to the free store. Remember that
the pointer itself--as opposed to the memory to which it points--is a local variable.
When the function in which it is declared returns, that pointer goes out of scope
and is lost. The memory allocated with <TT>new</TT> is not freed automatically, however.
That memory becomes unavailable--a situation called a memory leak. It's called a
memory leak because that memory can't be recovered until the program ends. It is
as though the memory has leaked out of your computer.</P>
<P>To restore the memory to the free store, you use the keyword <TT>delete</TT>.
For example,</P>
<PRE><FONT COLOR="#0066FF">delete pPointer;
</FONT></PRE>
<P>When you delete the pointer, what you are really doing is freeing up the memory
whose address is stored in the pointer. You are saying, &quot;Return to the free
store the memory that this pointer points to.&quot; The pointer is still a pointer,
and it can be reassigned. Listing 8.4 demonstrates allocating a variable on the heap,
using that variable, and deleting it.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>When you call <TT>delete</TT>
	on a pointer, the memory it points to is freed. Calling <TT>delete</TT> on that pointer
	again will crash your program! When you delete a pointer, set it to zero (null).
	Calling <TT>delete</TT> on a null pointer is guaranteed to be safe. For example:</P>
	<PRE><FONT COLOR="#0066FF">Animal *pDog = new Animal; delete pDog; //frees the memory    </FONT></PRE>

	<BLOCKQUOTE>
		<PRE><FONT COLOR="#0066FF">pDog = 0; //sets pointer to null    //... delete pDog; //harmless</FONT></PRE>

	</BLOCKQUOTE>
	<P>
<HR>


</BLOCKQUOTE>

<PRE></PRE>
<P><A NAME="Heading24"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.4. Allocating,
using, and deleting pointers.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:     // Listing 8.4
2:     // Allocating and deleting a pointer
3:
4:     #include &lt;iostream.h&gt;
5:     int main()
6:     {
7:        int localVariable = 5;
8:        int * pLocal= &amp;localVariable;
9:        int * pHeap = new int;
10:       if (pHeap == NULL)
11:        {
12:            cout &lt;&lt; &quot;Error! No memory for pHeap!!&quot;;
13:            return 0;
14:        }
15:       *pHeap = 7;
16:       cout &lt;&lt; &quot;localVariable: &quot; &lt;&lt; localVariable &lt;&lt; &quot;\n&quot;;
17:       cout &lt;&lt; &quot;*pLocal: &quot; &lt;&lt; *pLocal &lt;&lt; &quot;\n&quot;;
18:       cout &lt;&lt; &quot;*pHeap: &quot; &lt;&lt; *pHeap &lt;&lt; &quot;\n&quot;;
19:       delete pHeap;
20:       pHeap = new int;
21:       if (pHeap == NULL)
22:       {
23:            cout &lt;&lt; &quot;Error! No memory for pHeap!!&quot;;
24:            return 0;
25:       }
26:       *pHeap = 9;
27:       cout &lt;&lt; &quot;*pHeap: &quot; &lt;&lt; *pHeap &lt;&lt; &quot;\n&quot;;
28:       delete pHeap;
29:       return 0;
<TT>30: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: localVariable: 5
*pLocal: 5
*pHeap: 7
*pHeap: 9
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis: </B></FONT>Line 7 declares and initializes
a local variable. Line 8 declares and initializes a pointer with the address of the
local variable. Line 9 declares another pointer but initializes it with the result
obtained from calling <TT>new int</TT>. This allocates space on the free store for
an <TT>int</TT>. Line 10 verifies that memory was allocated and the pointer is valid
(not null). If no memory can be allocated, the pointer is null and an error message
is printed.<BR>
To keep things simple, this error checking often won't be reproduced in future programs,
but you must include some sort of error checking in your own programs.</P>
<P>Line 15 assigns the value <TT>7</TT> to the newly allocated memory. Line 16 prints
the value of the local variable, and line 17 prints the value pointed to by <TT>pLocal</TT>.
As expected, these are the same. Line 19 prints the value pointed to by <TT>pHeap</TT>.
It shows that the value assigned in line 15 is, in fact, accessible.</P>
<P>In line 19, the memory allocated in line 9 is returned to the free store by a
call to <TT>delete</TT>. This frees the memory and disassociates the pointer from
that memory. <TT>pHeap</TT> is now free to point to other memory. It is reassigned
in lines 20 and 26, and line 27 prints the result. Line 28 restores that memory to
the free store.</P>
<P>Although line 28 is redundant (the end of the program would have returned that
memory) it is a good idea to free this memory explicitly. If the program changes
or is extended, it will be beneficial that this step was already taken care of.
<H3 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Memory Leaks</FONT></H3>
<P>Another way you might inadvertently create a memory leak is by reassigning your
pointer before deleting the memory to which it points. Consider this code fragment:</P>
<PRE><FONT COLOR="#0066FF">1:   unsigned short int * pPointer = new unsigned short int;
2:   *pPointer = 72;
3:   pPointer = new unsigned short int;
4:   *pPointer = 84;
</FONT></PRE>
<P>Line 1 creates <TT>pPointer</TT> and assigns it the address of an area on the
free store. Line 2 stores the value <TT>72</TT> in that area of memory. Line 3 reassigns
<TT>pPointer</TT> to another area of memory. Line 4 places the value <TT>84</TT>
in that area. The original area--in which the value <TT>72</TT> is now held--is unavailable
because the pointer to that area of memory has been reassigned. There is no way to
access that original area of memory, nor is there any way to free it before the program
ends.</P>
<P>The code should have been written like this:</P>
<PRE><FONT COLOR="#0066FF">1: unsigned short int * pPointer = new unsigned short int;
2: *pPointer = 72;
3: delete pPointer;
4: pPointer = new unsigned short int;
5: *pPointer = 84;
</FONT></PRE>
<P>Now the memory originally pointed to by <TT>pPointer</TT> is deleted, and thus
freed, in line 3.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>For every time in your program that
	you call <TT>new</TT>, there should be a call to <TT>delete</TT>. It is important
	to keep track of which pointer owns an area of memory and to ensure that the memory
	is returned to the free store when you are done with it. 
<HR>


</BLOCKQUOTE>

<H3 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Creating Objects
on the Free Store</FONT></H3>
<P>Just as you can create a pointer to an integer, you can create a pointer to any
object. If you have declared an object of type <TT>Cat</TT>, you can declare a pointer
to that class and instantiate a <TT>Cat</TT> object on the free store, just as you
can make one on the stack. The syntax is the same as for integers:</P>
<PRE><FONT COLOR="#0066FF">Cat *pCat = new Cat;
</FONT></PRE>
<P>This calls the default constructor--the constructor that takes no parameters.
The constructor is called whenever an object is created (on the stack or on the free
store).
<H3 ALIGN="CENTER"><A NAME="Heading27"></A><FONT COLOR="#000077">Deleting Objects</FONT></H3>
<P>When you call <TT>delete</TT> on a pointer to an object on the free store, that
object's destructor is called before the memory is released. This gives your class
a chance to clean up, just as it does for objects destroyed on the stack. Listing
8.5 illustrates creating and deleting objects on the free store.</P>
<P><A NAME="Heading28"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 8.5. Creating
and deleting objects on the free store.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1:    // Listing 8.5
2:    // Creating objects on the free store
3:
4:     #include &lt;iostream.h&gt;
5:
6:     class SimpleCat
7:     {
8:     public:
9:            SimpleCat();
10:             ~SimpleCat();
11     private:
12             int itsAge;
13        };
14
15        SimpleCat::SimpleCat()
16        {
17               cout &lt;&lt; &quot;Constructor called.\n&quot;;
18               itsAge = 1;
19        }
20
21        SimpleCat::~SimpleCat()
22        {

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美综合欧美视频| 久久久电影一区二区三区| 伊人一区二区三区| 欧美午夜寂寞影院| 久久精工是国产品牌吗| 久久精子c满五个校花| www.亚洲人| 亚洲国产美女搞黄色| 亚洲视频在线观看一区| 美女视频第一区二区三区免费观看网站| 成人av资源在线观看| 一区二区久久久久久| 日韩视频免费观看高清完整版在线观看 | 日本一区二区三区在线不卡| 国产91对白在线观看九色| 99re在线精品| 国产香蕉久久精品综合网| 国产成都精品91一区二区三| 樱桃视频在线观看一区| 亚洲精品一区二区三区精华液| 国产成人亚洲综合a∨婷婷图片| 亚洲免费观看在线视频| 久久尤物电影视频在线观看| 欧美日韩亚洲综合在线 欧美亚洲特黄一级| 美美哒免费高清在线观看视频一区二区| 国产日韩高清在线| 欧美mv和日韩mv的网站| 51精品视频一区二区三区| 久久久久久久网| 99精品视频一区| 一区二区三区资源| 亚洲欧美日韩一区| 亚洲欧洲无码一区二区三区| 国产亚洲综合色| 久久久亚洲精品一区二区三区| 欧美另类高清zo欧美| 欧美精品vⅰdeose4hd| 欧洲一区在线电影| 日本电影欧美片| 欧美色欧美亚洲另类二区| 91麻豆免费视频| 99久久精品国产毛片| 日本一区二区成人| 欧美乱熟臀69xxxxxx| 欧美日韩亚洲综合一区| 91精品福利在线一区二区三区| 在线电影一区二区三区| 日韩一区二区三区视频| 久久色在线观看| 国产精品色婷婷| 丝袜诱惑制服诱惑色一区在线观看 | 99久久精品免费看| av一区二区三区四区| 麻豆精品在线观看| 国产一区不卡在线| av在线播放一区二区三区| 欧美三级电影网站| 国产精品色在线| 紧缚奴在线一区二区三区| 成人app网站| 久久先锋影音av| 婷婷国产在线综合| 色婷婷久久久久swag精品| 国产人妖乱国产精品人妖| 奇米色一区二区三区四区| 91高清在线观看| 国产日本欧美一区二区| 日韩精品中文字幕一区| 亚洲午夜成aⅴ人片| 成人激情图片网| 国产精品久久一卡二卡| 国产麻豆一精品一av一免费| 91精品国产乱码| 日本一不卡视频| 日韩一区二区影院| 免费在线视频一区| 精品国产乱码久久久久久免费| 婷婷六月综合亚洲| 日韩免费高清av| 国产在线观看免费一区| 久久久综合视频| 日本视频一区二区| 欧美变态凌虐bdsm| 成人av电影观看| 一区二区三区加勒比av| 欧美日韩久久一区二区| 日韩精品一区第一页| 日韩欧美一级二级三级久久久| 免费精品99久久国产综合精品| 日韩女优制服丝袜电影| 成人看片黄a免费看在线| 亚洲精品乱码久久久久| 制服丝袜激情欧洲亚洲| 国产成人综合视频| 亚洲成人动漫精品| 国产精品看片你懂得| 成人伦理片在线| 亚洲视频在线观看一区| 91精品国产综合久久久久久漫画| 蜜臀va亚洲va欧美va天堂 | 欧美日韩午夜影院| 国产成人在线影院| 免费黄网站欧美| 亚洲1区2区3区4区| 亚洲男人天堂av| 国产精品免费久久久久| 久久网站热最新地址| 日韩欧美亚洲国产另类| 欧美三日本三级三级在线播放| 国产成人精品三级麻豆| 国内精品伊人久久久久av一坑 | 亚洲a一区二区| 亚洲成a人片在线观看中文| 老司机一区二区| 美女性感视频久久| 国产欧美综合在线| 欧美午夜影院一区| 久久超级碰视频| 国产精品日日摸夜夜摸av| 717成人午夜免费福利电影| 成人免费看的视频| 中文字幕一区二区三区不卡| 精品国产凹凸成av人网站| 日韩欧美国产综合一区| 欧美一级欧美一级在线播放| 在线成人av网站| 欧美一区二区视频在线观看2020| 欧美在线短视频| 日韩视频在线永久播放| www国产亚洲精品久久麻豆| 精品国免费一区二区三区| 久久一日本道色综合| 国产日韩欧美亚洲| 国产亚洲自拍一区| 国产精品久久久久久久久晋中| 中文字幕+乱码+中文字幕一区| 亚洲三级电影全部在线观看高清| 亚洲色图制服丝袜| 香蕉影视欧美成人| 国产成人综合自拍| 91精品一区二区三区久久久久久 | 国产精品久久久久久户外露出| 亚洲精品国产视频| 丁香婷婷综合激情五月色| 国产精品系列在线观看| 成人激情免费电影网址| 欧美一区二区在线免费播放| 国产精品视频观看| 国产高清精品久久久久| 欧美亚洲国产一区在线观看网站| 精品国产一区二区三区久久影院| 国产精品国产三级国产三级人妇 | 国产裸体歌舞团一区二区| 欧美亚洲综合在线| 国产精品久久久久一区二区三区共| 午夜a成v人精品| 精品污污网站免费看| 玉米视频成人免费看| 99re热这里只有精品免费视频| 欧美精品日韩一区| 日韩 欧美一区二区三区| 欧美日韩国产另类不卡| 亚洲国产美国国产综合一区二区| 国产91精品露脸国语对白| 国产欧美日韩视频一区二区| 另类专区欧美蜜桃臀第一页| 欧美大度的电影原声| 国产麻豆精品在线观看| 国产日韩欧美电影| 成人短视频下载| 亚洲妇熟xx妇色黄| 欧美一区二区三区在线观看视频| 日欧美一区二区| 欧美精品一区二| 一本色道久久综合亚洲91| 伊人一区二区三区| 精品国产一区久久| 成人免费的视频| 天天综合网天天综合色| 久久色中文字幕| 欧美日韩一区二区在线视频| 久久成人久久鬼色| 一区二区不卡在线播放| 久久久久久久久久久久久女国产乱| 99re热视频精品| 欧美aaaaa成人免费观看视频| 国产精品美女久久福利网站| 欧美男女性生活在线直播观看| 国产一区二区在线观看免费| 亚洲美女电影在线| 国产精品久久久久久久久快鸭 | 一区二区三区四区不卡视频| 91精品国产一区二区三区香蕉| 粉嫩av亚洲一区二区图片| 日韩电影一二三区| 手机精品视频在线观看| 亚洲国产色一区| 亚洲精品午夜久久久| 1000精品久久久久久久久| 国产夜色精品一区二区av|