📄 ememoryc.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Memory Management</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT>
var dingbase = "EMEM_DIR.HTM";
var dingtext = "EC++ Mem Mgmt, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="EC++ Chapter Intro: Memory Management" -->
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI4_FR.HTM" TARGET="_top">Item 4: Prefer C++-style comments.</A> <BR> Continue to <A HREF="./EI5_FR.HTM" TARGET="_top">Item 5: Use the same form in corresponding uses of new and delete.</A></FONT></DIV>
<P><A NAME="dingp1"></A><A NAME="p22"></A><FONT ID="egtitle">Memory Management</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="1866"></A>
<A NAME="1867"></A>
<P><A NAME="dingp2"></A>
Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently. Good programmers understand that these concerns should be addressed in that order, because a program that is dazzlingly fast and astoundingly small is of little use if it doesn't behave the way it's supposed to. For most programmers, getting things right means calling memory allocation and deallocation routines correctly. Making things perform efficiently, on the other hand, often means writing custom versions of the allocation and deallocation routines. Getting things right there is even more <NOBR>important.<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="1868"></A>
<P><A NAME="dingp3"></A>
On the correctness front, C++ inherits from C one of its biggest headaches, that of potential memory leaks. Even virtual memory, wonderful invention though it is, is finite, and not everybody has virtual memory in the first <NOBR>place.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="29114"></A>
<P><A NAME="dingp4"></A>
In C, a memory leak arises whenever memory allocated through <CODE>malloc</CODE> is never returned through <CODE>free</CODE>. The names of the players in C++ are <CODE>new</CODE> and <CODE>delete</CODE>, but the story is much the same. However, the situation is improved somewhat by the presence of destructors, because they provide a convenient repository for calls to <CODE>delete</CODE> that all objects must make when they are destroyed. At the same time, there is more to worry about, because <CODE>new</CODE> implicitly calls constructors and <CODE>delete</CODE> implicitly calls destructors. Furthermore, there is the complication that you can define your own versions of <CODE>operator</CODE> <CODE>new</CODE> and <CODE>operator</CODE> <CODE>delete</CODE>, both inside and outside of classes. This gives rise to all kinds of opportunities to make mistakes. The following Items (as well as <A HREF="../MEC/MI8_FR.HTM#33985" TARGET="_top">Item M8</A>) should help you avoid some of the most common <NOBR>ones.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI4_FR.HTM" TARGET="_top">Item 4: Prefer C++-style comments.</A> <BR> Continue to <A HREF="./EI5_FR.HTM" TARGET="_top">Item 5: Use the same form in corresponding uses of new and delete.</A></FONT></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -