📄 yolinux c - c++ and dynamic memory allocation.mht
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: YoLinux: C / C++ and Dynamic memory allocation
Date: Wed, 4 Apr 2007 11:06:48 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_016F_01C776A9.5742E1A0"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
This is a multi-part message in MIME format.
------=_NextPart_000_016F_01C776A9.5742E1A0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/Cpp-DynamicMemory.html
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>YoLinux: C / C++ and Dynamic memory =
allocation</TITLE><LINK=20
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=3D"C++,dynamic memory allocation,GNU C++,linux" =
name=3DKEYWORDS>
<META=20
content=3D"C and C++ dynamic memory allocation. 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++ and Dynamic memory allocation</H1>This C / C++ =
tutorial covers=20
dynamic memory allocation, memory allocation (on heap), exception =
handling=20
and some examples. Examples are included of "C" malloc() and =
free() as=20
well as "C++" new and delete.=20
<P>
<DIV 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> </NOSCRIPT><!-- END BURST CODE =
--></DIV></TD></TR></TBODY></TABLE>
<P>
<HR SIZE=3D5>
<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/LinuxTutorialC++.html">C++=20
Info, links</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html=
">C++=20
String Class</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html">C++ =
STL=20
vector, list</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html">Fork=20
and exec with C/C++</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/CORBA.html">CORBA (OmniORB)=20
with C++</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/LinuxTutorialAdvanced_vi.html">A=
dvanced=20
VI</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CGI.html">CGI =
in=20
C++</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/MicrosoftVisualC++Tips.html">MS/=
Visual=20
C++ Practices</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeak=
s.html">C++=20
Memory corruption and leaks</A>=20
<P>
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/index.html">YoLinux=20
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 vAlign=3Dtop>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C malloc/free vs C++=20
new/delete:</BIG></B></TD></TR></TBODY></TABLE>
<P>C malloc()/free() do not call constructors/destructors. C++ =
new/delete=20
are "class aware" and will call class constructors when a class is =
allocated with new and a destructor when delete is called. [<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/src/CppExampleNewDeleteConstruct=
orDestructor.cpp">example</A>]=20
<P>Mixing malloc with delete or new with free is undefined. In =
addition,=20
C++ new/delete were introduced with the C++ practice of allocating =
memory=20
in constructors and deallocating the same memory with a =
destructor.=20
<P>new/delete advantages:=20
<UL>
<LI>new/delete invokes constructor/destructor. Malloc/free will =
not.=20
<LI>new does not need typcasting. Malloc requires typcasting the =
returned pointer.=20
<LI>new/delete operators can be overloaded, malloc/free can not. =
<LI>new does not require you to explicitly calculate the =
quantity of=20
memory required. (Unlike malloc) </LI></UL>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C dynamic memory=20
allocation:</BIG></B></TD></TR></TBODY></TABLE>
<P>Use "malloc", "calloc" and "free": File: MallocTest.cpp=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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <error.h>
typedef struct
{
int ii;
double dd;
} SSS;
int main()
{
int kk, jj;
char *str1=3D"This is a text string";
char *str2 =3D malloc(strlen(str1));
SSS *s1 =3D calloc(4, sizeof(SSS));
if(s1 =3D=3D ENOMEM) printf("Error ENOMEM: Insufficient memory =
available\n");
strcpy(str2,str1); /* Make a copy of the string */
for(kk=3D0; kk < 5; kk++)
{
s1[kk].ii=3Dkk;
}
for(jj=3D0; jj < 5; jj++)
{
printf("Value strored: %d\n",s1[jj].ii);
}
free(s1);
free(str2);
}
</PRE></TD></TR></TBODY></TABLE>
<P>Compile: <TT>gcc -o MallocTest MallocTest.cpp</TT>=20
<P>Run: <TT>MallocTest</TT> <BR>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -