📄 4memory.html
字号:
<html>
<head>
<title>Pointers and Dynamic Memory Allocation</title>
<meta name="description" content="Operator new and delete">
<meta name="keywords" content="memory, new, delete, allocation, pointer">
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78">
<td>
<h3>Pointers and Dynamic Memory Allocation</h3>
<p>Despite all the negative advertising in this book, pointers are invaluable whenever dynamic data structures are involved. Dynamic data structures have to be able to grow and shrink. Growing involves acquiring new areas of memory and cultivating them; shrinking means recycling them. How do we acquire new areas of memory? We have to ask the system to give us memory for a particular type of variable or object. We do it using <b><i>operator new</i></b>. The memory returned by <var>new</var> is allocated from the so called <i>heap</i>梩he area of memory managed by the C++ runtime. How do we access this new memory? Using a <b>pointer</b> to that particular type of variable or object. For instance, to get memory enough to store a single integer, we'd use the following syntax:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>int * pNumber = new int;</pre>
</table><!-- End Code -->
<p>Even better, we could (and presumably should) immediately initialize this memory with a particular value we want to store there:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>int * pNumber = new int (2001);</pre>
</table><!-- End Code -->
<p><img src="Image3-1.gif" tppabs="http://www.relisoft.com/book/lang/pointer/images/Image3.gif" width=294 height=102 alt=" ">
<p class=caption>Figure 3. New memory for an integer was allocated and initialized with the value of 2001. The pointer <var>pNumber</var> contains the address of this new memory.</p>
<p>The initialization is mandatory when allocating an object that has a constructor. There are two cases: When there is a constructor that takes no arguments you don't have to do anything--such constructor is called by default. An argument-less constructor may, for instance, initialize member variables to some pre-defined values (e.g., zeros).
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>IStack* pStack = new IStack; // default constructor is called</pre>
</table><!-- End Code -->
<p>When the object has a constructor that requires arguments, we specify them in the call to <var>new</var>:
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>Star* pStar = new Star (1234.5, 10.2);</pre>
</table><!-- End Code -->
<p>If we want to store <var>n</var> integers, we need to allocate an array of <var>n</var> integers
<!-- Code --><table width="100%" cellspacing=10><tr> <td class=codetable>
<pre>int* pNumbers = new int [n];</pre>
</table><!-- End Code -->
<p><img src="Image4-2.gif" tppabs="http://www.relisoft.com/book/lang/pointer/images/Image4.gif" width=468 height=96 alt=" ">
<p class=caption>Figure 4. The result of allocating an array.</p>
<p>There is no direct way to initialize the contents of a dynamically allocated array. We just have to iterate through the newly allocated array and set the values by hand.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>for (int i = 0; i < n; ++i)
pNumbers [i] = i * i;</pre>
</table>
<!-- End Code -->
<p>Consequently, if we are allocating an array of <i>objects</i>, there is no way to pass arguments to objects
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -