📄 yolinux c - c++ and dynamic memory allocation.mht
字号:
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>Value strored: 0
Value strored: 1
Value strored: 2
Value strored: 3
Value strored: 4
</PRE></TD></TR></TBODY></TABLE></P></DD></DL>
<P>Manual pages:=20
<UL>
<LI><A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Dmalloc">=
malloc</A>:=20
a memory allocator=20
<LI><A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Dcalloc">=
calloc</A>:=20
a memory allocator=20
<LI><A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Drealloc"=
>realloc</A>:=20
memory reallocator=20
<LI><A=20
=
href=3D"http://node1.yo-linux.com/cgi-bin/man2html?cgi_command=3Dfree&=
;cgi_section=3D3">free</A>:=20
frees the memory space pointed to by ptr </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 "new" and "delete": File: AllocNewTest.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 <iostream>
class CCC
{
public:
CCC(){};
CCC(int);
CCC(int, double);
int ii;
double dd;
};
CCC::CCC(int _ii)
: ii(_ii)
{
};
CCC::CCC(int _ii, double _dd)
: ii(_ii), dd(_dd)
{
};
=20
using namespace std;
main()
{
CCC *cc1 =3D new CCC(4, 5.5); // Pointer. Contructor called.
CCC *cc2 =3D new CCC[5]; // Pointer to an array of objects.
CCC &cc3 =3D *new CCC; // Reference
cc1->ii=3D5;
cc2[3].ii=3D6;
cc3.ii=3D7;
cout << cc1->ii << endl;
cout << cc2[3].ii << endl;
cout << cc3.ii << endl;
delete cc1;
delete [] cc2;
delete & cc3;
}
</PRE></TD></TR></TBODY></TABLE><BR>Note the difference between:=20
<UL>
<LI><TT>new CCC(3)</TT> creates memory for a single object =
who's=20
integer member is set to 3.=20
<LI><TT>new CCC[3]</TT> creates memory for three objects of =
type CCC=20
and no variables are set. </I>
<P>Compile: <TT>g++ -o AllocNewTest AllocNewTest.cpp</TT>=20
</P></LI></UL></DD></DL>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C function returning a pointer vs C++ function =
returning=20
a copy:</BIG></B></TD></TR></TBODY></TABLE>
<P>
<DL>
<DD>C function returning a pointer:=20
<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 <stdlib.h>
char *fnConvert(int _ii)
{
char *str =3D malloc(10); /* Return 10 character string */
if(str =3D=3D NULL)
fprintf(stderr,"Error: Memory allocation failed.\n");
sprintf(str, "%d", _ii);
return str;
}
main()
{
char *s1=3DfnConvert( 34567 );
printf("%s\n", s1);
free(s1);
}
</PRE></TD></TR></TBODY></TABLE>
<P>C++ function returning a copy:=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string fnConvert(int _ii)
{
ostringstream ost;
ost << _ii;
return ost.str();
}
main()
{
cout << fnConvert( 34567 ) << endl;
}
</PRE></TD></TR></TBODY></TABLE>Note: The STL string class copy=20
constructor is employed to return a copy (return by value). Yes =
the=20
variable "ost" is out of scope once we leave the function, but =
the copy=20
of its contents is valid. Do not "return ost.str().c_str()" as =
this=20
pointer is out of scope once the procesing leaves the function =
and the=20
data lost. </P></DD></DL>
<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 allocation exception=20
handling:</BIG></B></TD></TR></TBODY></TABLE>
<P>Use exception handling:=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 <iostream>
using namespace std;
main()
{
int ii;
double *ptr[5000000];
try=20
{
for( ii=3D0; ii < 5000000; ii++)
{
ptr[ii] =3D new double[5000000];
}
}
catch ( <B>bad_alloc</B> &memmoryAllocationException )
{
cout << "Error on loop number: " << ii << endl;
cout << "Memory allocation exception occurred: "
<< memmoryAllocationException.what()=20
<< endl;
}
catch(...)
}
cout << "Unrecognized exception" << endl;
{
}
</PRE></TD></TR></TBODY></TABLE>
<P>Compile: <TT>g++ -o AllocNewTest AllocNewTest.cpp</TT>=20
<P>Run:=20
<UL>
<LI>Observer system limits: <TT>ulimit -a</TT>=20
<LI>Set system limits: <TT>ulimit -m 100</TT>=20
<LI>Run with fewer privileges: <TT>nice -n 19 =
AllocNewTest</TT>=20
</LI></UL></DD></DL>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>C++ Virtual =
Destructors</BIG></B></TD></TR></TBODY></TABLE>
<P>Using polymorphism to delete dynamically allocated objects by =
their=20
base class pointer: Declare base class destructor virtual so that =
the=20
delete operator can be applied to the base class pointer. The =
derived=20
class destructor is called first, before the base class =
destructor.=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 <iostream>
using namespace std;
class Base
{
public:
Base(){};
virtual ~Base(){ cout << "Base class destructor called" =
<< endl; }
};
class Derived : public Base
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -