?? c-c++ memory corruption and memory leaks.mht
字號:
From: <Saved by Microsoft Internet Explorer 5>
Subject: C/C++ Memory Corruption And Memory Leaks
Date: Wed, 4 Apr 2007 11:10:06 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0198_01C776A9.CD55A080"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
This is a multi-part message in MIME format.
------=_NextPart_000_0198_01C776A9.CD55A080
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>C/C++ Memory Corruption And Memory Leaks</TITLE><LINK =
href=3D"http://www.yolinux.com/TUTORIALS/yolinux.css" type=3Dtext/css=20
rel=3DSTYLESHEET>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META content=3DGNOME,DESKTOP,Linux name=3DKEYWORDS>
<META=20
content=3D"GNOME desktop basics. Overview of the processes at work which =
control the GNOME desktop. The YoLinux portal covers topics from desktop =
to servers and from developers to users"=20
name=3DDESCRIPTION>
<META content=3D"Greg Ippolito" name=3Dauthor>
<META content=3D"MSHTML 6.00.2900.3059" name=3DGENERATOR></HEAD>
<BODY bgColor=3D#cccccc>
<HR SIZE=3D5>
<TABLE width=3D"100%" callpadding=3D"4">
<TBODY>
<TR>
<TD vAlign=3Dtop><A href=3D"http://www.yolinux.com/"><IMG=20
alt=3D"Yolinux.com Tutorial"=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/YoLinux_Tutorial_logo.png"=
=20
border=3Dnone></A> </TD>
<TD vAlign=3Dtop>
<H1>C/C++ Memory Corruption And Memory Leaks</H1>This tutorial =
will=20
discuss examples of memory leaks and code constructs which lead to =
memory=20
corruption.=20
<TABLE width=3D"100%">
<TBODY>
<TR>
<TD vAlign=3Dtop align=3Dright><!-- BEGIN RICH-MEDIA BURST! =
CODE -->
<SCRIPT language=3DJavaScript>=0A=
rnum=3DMath.round(Math.random() * 100000);=0A=
=
=0A=
document.write('<scr'+'ipt =
src=3D"http://www.burstnet.com/cgi-bin/ads/ad9427a.cgi/v=3D2.0S/sz=3D468x=
60A|728x90A/'+rnum+'/NI/RETURN-CODE/JS/"></scr'+'ipt>');=0A=
=
=0A=
</SCRIPT>
<NOSCRIPT><A=20
=
href=3D"http://www.burstnet.com/ads/ad9427a-map.cgi/ns/v=3D2.0S/sz=3D468x=
60A|728x90A/"=20
target=3D_top><IMG alt=3D"Click Here"=20
=
src=3D"http://www.burstnet.com/cgi-bin/ads/ad9427a.cgi/ns/v=3D2.0S/sz=3D4=
68x60A|728x90A/"=20
border=3D0></A>=20
</NOSCRIPT><!-- END BURST CODE =
--></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<HR>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" bgColor=3D#000000 =
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD vAlign=3Dtop width=3D160 bgColor=3D#c0c0c0>
<P><FONT size=3D-1><B>Related YoLinux tutorials:</B>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/Cpp-DynamicMemory.html">C=20
and C++ Dynamic Memory Allocation</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment=
.html">Software=20
development tools</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html">STL:=20
vector, list</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html=
">STL:=20
string</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html">A=
dvanced=20
VI</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialXemacs.html">Emacs =
and=20
C/C++</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/MicrosoftVisualC++Tips.html">MS/=
Visual=20
C++ tips</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++.html">C++=20
Info, links</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/ClearcaseCommands.html">Clearcas=
e=20
Commands</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/index.html#PROGRAMMING">YoLinux =
Tutorials Index</A> </FONT>
<P>
<HR>
<P>
<SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 160;=0A=
google_ad_height =3D 600;=0A=
google_ad_format =3D "160x600_as";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D ["000000","6699CC","2D5893","191933"];=0A=
google_color_bg =3D ["F0F0F0","003366","99AACC","333366"];=0A=
google_color_link =3D ["0000FF","FFFFFF","000000","99CC33"];=0A=
google_color_url =3D ["008000","AECCEB","000099","FFCC00"];=0A=
google_color_text =3D ["000000","AECCEB","003366","FFFFFF"];=0A=
//--></SCRIPT>
<SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
type=3Dtext/javascript>=0A=
</SCRIPT>
<P>
<HR>
<P><A href=3D"http://yolinux.tradepub.com/">Free Information =
Technology=20
Magazine Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P><A =
href=3D"http://yolinux.tradepub.com/?pt=3Dcat&page=3DInfosoft">Free=20
Information Technology <B>Software and Development</B> Magazine=20
Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P></P></TD>
<TD><A name=3DCORRUPTIONTYPES></A>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C/C++ software memory corruption=20
types:</BIG></B></TD></TR></TBODY></TABLE>
<P>There are two forms of Linux Memory accessible to the =
programmer:=20
<OL>
<LI>User's virtual memory space in which application is run.=20
<LI>Register memory. </LI></OL>
<P>The most obvious memory errors result in a "Segmentation =
violation"=20
message. This may alert the programmer to the location of the =
memory error=20
when the program is run in gdb. The following errors discussed are =
the not=20
so obvious errors.=20
<P>Memory errors:=20
<UL>
<LI>Heap memory errors:=20
<UL>
<LI>Attempting to free memory already freed.=20
<LI>Freeing memory that was not allocated.=20
<LI>Attempting to write to memory already freed.=20
<LI>Attempting to write to memory which was never allocated.=20
<LI>Memory allocation error.=20
<LI>Reading/writing to memory out of the bounds of a =
dynamically=20
allocated array </LI></UL>
<P></P>
<LI>stack (local variables) memory errors:=20
<UL>
<LI>Reading/writing to memory out of the bounds of a static =
array.=20
(array index overflow - index too large/underflow - negative =
index)=20
<LI>Function pointer corruption: Invalid passing of function =
pointer=20
and thus a bad call to a function. </LI></UL></LI></UL>
<P><A name=3DMEMORYLEAKS></A>
<HR>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Memory Leaks:</BIG></B></TD></TR></TBODY></TABLE>
<P><B>Memory leak description:</B> Memory is allocated but not =
released=20
causing an application to consume memory reducing the available =
memory for=20
other applications and eventually causing the system to page =
virtual=20
memory to the hard drive slowing the application or crashing the=20
application when than the computer memory resource limits are =
reached. The=20
system may stop working as these limits are approached.=20
<P>
<HR>
<P><B>Many C library functions malloc's memory which MUST be =
freed:</B>=20
i.e.: strdup(),=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <STDLIB.H>
#include <STRING.H>
...
char *oldString =3D "Old String";
char newStrig =3D strdup(oldString);
if(newString =3D=3D ENOMEM) ... // Fail!!!!
...
free(newString);
</PRE></TD></TR></TBODY></TABLE></DD></DL>Note: You can NOT use the C++=20
delete call. The strdup() function is part of the C library and =
you must=20
use free().=20
<P>Any routine which is supplied by the C libraries or ones =
written within=20
an application which allocate memory must have the memory freed. =
Comments=20
on this need should be included in the include file to make users =
of the=20
function aware of their duties to free the memory and the =
mechanism by=20
which it is to be freed (free() or delete).=20
<P>
<HR>
<P><B>Programmer must free() malloc()'ed memory:</B>=20
<P>Also for calloc(), malloc() and realloc();=20
<DL>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -