📄 lib0032.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Chapter 4: Manual Memory Management</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.books24x7.css">
</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0031.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0033.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
<div class="chapter">
<a name="ch04"></a>
<h1 class="chapter-title"><span class="chapter-titlelabel">Chapter 4: </span>Manual Memory Management</h1><a name="419"></a><a name="IDX-207"></a>
<p class="para">Managing memory in the heap is defined by the requirement that services be provided to allocate and deallocate arbitrary size blocks of memory in an arbitrary order. In other words, the heap is a free-for-all zone, and the heap manager has to be flexible enough to deal with a number of possible requests. There are two ways to manage the heap: manual and automatic memory management. In this chapter, I will take an in-depth look at manual memory management and how it is implemented in practice.</p>
<div class="section">
<h2 class="sect2-title">
<a name="420"></a><a name="ch04lev1sec1"></a>Replacements for <span class="fixed">malloc()</span> and <span class="fixed">free()</span>
</h2>
<p class="first-para">Manual memory management dictates that the engineer writing a program must keep track of the memory allocated. This forces all of the bookkeeping to be performed when the program is being designed instead of while the program is running. This can benefit execution speed because the related bookkeeping instructions are not placed in the application itself. However, if a programmer makes an accounting error, they could be faced with a memory leak or a dangling pointer. Nevertheless, properly implemented manual memory management is lighter and faster than the alternatives. I provided evidence of this in the <a href="LiB0026.html#290" target="_parent" class="chapterjump">previous chapter</a>.</p>
<p class="para">In ANSI C, manual memory management is provided by the <span class="fixed">malloc()</span> and <span class="fixed">free()</span> standard library calls. There are two other standard library functions (<span class="fixed">calloc()</span> and <span class="fixed">realloc()</span>), but as we saw in <a href="LiB0026.html#290" target="_parent" class="chapterjump">Chapter 3</a>, they resolve to calls to <span class="fixed">malloc()</span> and <span class="fixed">free()</span>.</p>
<p class="para">I thought that the best way to illustrate how manual memory management facilities are constructed would be to offer several different implementations of <span class="fixed">malloc()</span> and <span class="fixed">free()</span>. To use these alternative implementations, all you will need to do is include the <a name="421"></a><a name="IDX-208"></a>appropriate source file and then call <span class="fixed">newMalloc()</span> and <span class="fixed">newFree()</span> instead of <span class="fixed">malloc()</span> and <span class="fixed">free()</span>. For example:</p>
<div class="informalexample">
<pre class="literallayout">
#include<mallocV1.cpp>
void main()
{
char *cptr;
initMemMgr();
cptr = newMalloc(10);
if(cptr==NULL){ printf("allocation failed!\n"); }
newFree(cptr);
closeMemMgr();
return;
}
</pre>
</div>
<p class="last-para">The remainder of this chapter will be devoted to describing three different approaches. In each case, I will present the requisite background theory, offer a concrete implementation, provide a test driver, and look at associated trade-offs. Along the way, I will also discuss performance measuring techniques and issues related to program simulation.</p>
<a></a>
</div>
</div>
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td valign="top" class="v2" align="right"><div STYLE="MARGIN-RIGHT: 0.15in"><a href="LiB0031.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0033.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -