📄 c-c++ memory corruption and memory leaks.mht
字号:
<TD><PRE>struct ABC abc;
struct ABC *abc_ptr =3D &abc;
...
free(abc_ptr);
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<P>
<HR>
<P><B>Incorrect use of delete: The delete must match the use of =
new.</B>=20
<P>The pairing is new/delete and new [] / delete[]=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>ClassABC *abc_ptr =3D new ClassABC[100];
...
delete [] abc_ptr;
</PRE></TD></TR></TBODY></TABLE></DD></DL>Use of "delete abc_ptr" is an=20
error.=20
<P>Do not use malloc()/free() with a C++ class as it will not call =
the=20
constructor or destructor. Also malloc()/free() can not be mixed =
with=20
new/delete. i.e. Free() can not be used to free memory allocated =
with new=20
and delete can not be used to free memory allocated with malloc(). =
<P>
<HR>
<P><B>Exception Errors:</B>=20
<P>Freeing memory never allocated. If you use a constructor to =
allocate=20
memory but an exception is thrown before all is allocated, the =
destructor=20
needs to be aware that fact or else it may try to free memory =
which was=20
never allocated.=20
<P>Also the converse is true. If the destructor throws an =
exception,=20
subsequent steps which free memory may not be executed. This =
applies to=20
the destructor and all nested destructors which handle/re-throw =
the=20
exception while the stack unwinds.=20
<P>
<HR>
<P><B>Pointer persistence:</B>=20
<P>Function returning a pointer from the stack which can get =
overwritten=20
by the calling function (in this case main()):=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>int *get_ii()
{
int ii; // Local stack variable
ii =3D 2;
return &ii;
}
main()
{
int *ii;
ii =3D get_ii(); // After this call the stack is given up by the =
routine=20
// get_ii() and its values are no longer safe.
... Do stuff
.. ii may be corrupt by this point.
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>This is also true for a local=20
variable within the scope of only { and } as well as for the scope =
of a=20
function.=20
<P>
<HR>
<P><B>Incorrect passing of a function argument:</B>=20
<P>If the pointer is passed around as an argument and does not get =
passed=20
correctly, one may try to free the incorrect pointer.=20
<P>
<HR>
<P><B>Mixing the object base class and derived class:</B>=20
<P>If mixing the object base class and derived class when passing =
an=20
object by value as a function parameter, make sure that you =
understand=20
what may be lost.=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>function_A(BaseClass baseClass_ptr)
{
...
}
// Call to function
function_A(derivedClass_ptr); // Note that much of the information =
contained=20
// in the derived class will not be passed =
into
// the function including virtual =
destructors.
</PRE></TD></TR></TBODY></TABLE></DD></DL>
<P>
<HR>
<P><B>Copying an object:</B>=20
<P>Don't use memcpy() or any bit for bit copy function to copy an =
object.=20
It will not execute the class constructor. What kind of person =
would do=20
this?? Passing an object in a va_arg() list will result in a bit =
for bit=20
copy and will not use the default copy constructor.=20
<P><A name=3DLINKS></A>
<HR>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>Links:</BIG></B></TD></TR></TBODY></TABLE>
<P>
<UL>
<LI><A=20
=
href=3D"http://www.informit.com/articles/article.asp?p=3D30642&seqNum=
=3D9">C++=20
Memory and Resource Management: Improper Use of auto_ptr</A>=20
<LI><A=20
=
href=3D"http://www.eventhelix.com/RealtimeMantra/Basics/debugging_softwar=
e_crashes_2.htm">Debugging=20
Software Crashes</A>=20
<LI><A=20
=
href=3D"http://users.actcom.co.il/~choo/lupg/tutorials/unix-memory/unix-m=
emory.html">Unix=20
And C/C++ Runtime Memory Management For Programmers</A>=20
<LI><A=20
=
href=3D"http://www.linuxdevcenter.com/pub/a/linux/2003/05/08/cpp_mm-1.htm=
l?page=3D1">C++=20
Memory Management: From Fear to Triumph</A> </LI></UL>
<P>Related YoLinux Tutorials:=20
<UL>
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment=
.html#MEMORYTOOLS">Software=20
Development Tools: Memory Debugging Development Tools</A>=20
<LI><A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++.html">C++=20
programming on Linux</A>=20
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
">C++=20
Coding styles and practices </A>
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++Structures.html"=
>C++=20
structures</A>=20
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/index.html#PROGRAMMING">Programm=
ing=20
Tutorials</A> </LI></UL>
<P><A name=3DBOOKS></A>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD vAlign=3Dtop><IMG=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/book40.gif"><B><BIG>Books:=
</BIG></B></TD></TR></TBODY></TABLE>
<P>
<DL>
<DD>
<TABLE cellPadding=3D5 width=3D"100%" border=3D1>
<TBODY>
<TR>
<TD><IMG=20
=
src=3D"http://www.yolinux.com/BOOKS/0321125185.01.MZZZZZZZ.jpg"></TD>
<TD vAlign=3Dtop>C++ Gotchas: Avoiding Common Problems in =
Coding and=20
Design <BR>by Stephen C. Dewhurst <BR>ISBN #0321125185,=20
Addison-Wesley Professional=20
<P></P>
<TD vAlign=3Dtop><A=20
=
href=3D"http://www.amazon.com/exec/obidos/ASIN/0321125185/yolinux-20"><IM=
G=20
alt=3DAmazon.com=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/Amazon-BuyABook88x31.gif">=
</A>=20
<BR><IMG height=3D1=20
=
src=3D"http://service.bfast.com/bfast/serve?bfmid=3D2181&sourceid=3D3=
9358384&bfpid=3D0321125185&bfmtype=3Dbook"=20
width=3D1 border=3D0 NOSAVE><A=20
=
href=3D"http://service.bfast.com/bfast/click?bfmid=3D2181&sourceid=3D=
39358384&bfpid=3D0321125185&bfmtype=3Dbook"=20
target=3D_top><IMG height=3D60=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/BarnesNobles.gif"=20
width=3D75></A> </TD></TR>
<TR>
<TD><IMG=20
=
src=3D"http://www.yolinux.com/BOOKS/0201924889.01.MZZZZZZZ.jpg"></TD>
<TD vAlign=3Dtop>Effective C++: 50 Specific Ways to Improve =
Your=20
Programs and Design (2nd Edition) <BR>by Scott Meyers =
<BR>ISBN=20
#0201924889, Addison-Wesley Professional=20
<P></P>
<TD vAlign=3Dtop><A=20
=
href=3D"http://www.amazon.com/exec/obidos/ASIN/0201924889/yolinux-20"><IM=
G=20
alt=3DAmazon.com=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/Amazon-BuyABook88x31.gif">=
</A>=20
<BR><IMG height=3D1=20
=
src=3D"http://service.bfast.com/bfast/serve?bfmid=3D2181&sourceid=3D3=
9358384&bfpid=3D0201924889&bfmtype=3Dbook"=20
width=3D1 border=3D0 NOSAVE><A=20
=
href=3D"http://service.bfast.com/bfast/click?bfmid=3D2181&sourceid=3D=
39358384&bfpid=3D0201924889&bfmtype=3Dbook"=20
target=3D_top><IMG height=3D60=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/BarnesNobles.gif"=20
width=3D75></A> </TD></TR>
<TR>
<TD><IMG=20
=
src=3D"http://www.yolinux.com/BOOKS/020163371X.01.MZZZZZZZ.jpg"></TD>
<TD vAlign=3Dtop>More Effective C++: 35 New Ways to improve =
your=20
Programs and Designs <BR>by Scott Meyers <BR>ISBN =
#020163371X,=20
Addison-Wesley Professional=20
<P></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -