⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yolinux c - c++ and dynamic memory allocation.mht

📁 linux下c++编程的好文章
💻 MHT
📖 第 1 页 / 共 5 页
字号:
          <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&amp=
;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 &lt;iostream&gt;

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 &amp;cc3 =3D *new CCC;          // Reference

   cc1-&gt;ii=3D5;
   cc2[3].ii=3D6;
   cc3.ii=3D7;

   cout &lt;&lt; cc1-&gt;ii   &lt;&lt; endl;
   cout &lt;&lt; cc2[3].ii &lt;&lt; endl;
   cout &lt;&lt; cc3.ii    &lt;&lt; endl;

   delete cc1;
   delete [] cc2;
   delete &amp; 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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 &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

using namespace std;

string fnConvert(int _ii)
{
   ostringstream ost;
   ost &lt;&lt; _ii;
   return ost.str();
}

main()
{
    cout &lt;&lt; fnConvert( 34567 ) &lt;&lt; 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 &lt;iostream&gt;

using namespace std;

main()
{
   int ii;
   double *ptr[5000000];

   try=20
   {
      for( ii=3D0; ii &lt; 5000000; ii++)
      {
         ptr[ii] =3D new double[5000000];
      }
   }

   catch ( <B>bad_alloc</B> &amp;memmoryAllocationException )
   {
      cout &lt;&lt; "Error on loop number: " &lt;&lt; ii &lt;&lt; endl;
      cout &lt;&lt; "Memory allocation exception occurred: "
           &lt;&lt; memmoryAllocationException.what()=20
           &lt;&lt; endl;
   }
   catch(...)
   }
      cout &lt;&lt; "Unrecognized exception" &lt;&lt; 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 &lt;iostream&gt;
using namespace std;

class Base
{
public:
   Base(){};
   virtual ~Base(){ cout &lt;&lt; "Base class destructor called" =
&lt;&lt; endl; }
};

class Derived : public Base
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -