Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. delete Operator?

delete Operator?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anju
    wrote on last edited by
    #1

    Hi,all In my application I am using the Class objects ass follows //In MainOne.h Class CMainOne { public: class CSomeA { public: int nXA; int nYA; }; class CSomeC { public: int nXC; int nYC; SomeA *objSomeA; }; SomeC * objSomeC; };//Class CMainOne //In MainOne.cpp CMainOne::MyFunction { objSomeC=new CSomec[10]; for(int nIndex=0;nIdex<10;nIndex++) { objSomeC[nIndex].objSomeA=new CSomeA[10]; } } can any one help me how can I delete these created objects with new operator by using delete operator. Note:I tried like this delete [] objSomec->objSomeA (which was "objSomeC[nIndex].objSomeA=new CSomeA[10];" delete [] objSomeC (which was objSomeC=new CSomec[10];) still i am getting memory leak; please help me regrads:rose: anju

    J H 2 Replies Last reply
    0
    • A anju

      Hi,all In my application I am using the Class objects ass follows //In MainOne.h Class CMainOne { public: class CSomeA { public: int nXA; int nYA; }; class CSomeC { public: int nXC; int nYC; SomeA *objSomeA; }; SomeC * objSomeC; };//Class CMainOne //In MainOne.cpp CMainOne::MyFunction { objSomeC=new CSomec[10]; for(int nIndex=0;nIdex<10;nIndex++) { objSomeC[nIndex].objSomeA=new CSomeA[10]; } } can any one help me how can I delete these created objects with new operator by using delete operator. Note:I tried like this delete [] objSomec->objSomeA (which was "objSomeC[nIndex].objSomeA=new CSomeA[10];" delete [] objSomeC (which was objSomeC=new CSomec[10];) still i am getting memory leak; please help me regrads:rose: anju

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #2

      for(int nIndex=0;nIndex<10;nIndex++)
      {
      delete [] objSomeC[nIndex].objSomeA; // =new CSomeA[10];
      }
      delete [] objSomeC;

      If you want to do it at the object's destruction, you might use this variant:

      class CMainOne
      {

      public:
      class CSomeA
      {
      public:
      int nXA;
      int nYA;
      };

      class CSomeC
      {
      public:
      ~CSomeC()
      {
      delete [] objSomeA;
      }
      int nXC;
      int nYC;
      CSomeA *objSomeA;
      };

      CMainOne();
      ~CMainOne()
      {
      delete [] objSomeC;
      }
      CSomeC * objSomeC;

      };//Class CMainOne

      //In MainOne.cpp

      CMainOne::CMainOne()
      {
      objSomeC=new CSomeC[10];

      for(int nIndex=0;nIndex<10;nIndex++)
      {
      objSomeC[nIndex].objSomeA=new CSomeA[10];
      }
      }

      with the usual checkins i've omitted for brevity. rechi

      A 1 Reply Last reply
      0
      • A anju

        Hi,all In my application I am using the Class objects ass follows //In MainOne.h Class CMainOne { public: class CSomeA { public: int nXA; int nYA; }; class CSomeC { public: int nXC; int nYC; SomeA *objSomeA; }; SomeC * objSomeC; };//Class CMainOne //In MainOne.cpp CMainOne::MyFunction { objSomeC=new CSomec[10]; for(int nIndex=0;nIdex<10;nIndex++) { objSomeC[nIndex].objSomeA=new CSomeA[10]; } } can any one help me how can I delete these created objects with new operator by using delete operator. Note:I tried like this delete [] objSomec->objSomeA (which was "objSomeC[nIndex].objSomeA=new CSomeA[10];" delete [] objSomeC (which was objSomeC=new CSomec[10];) still i am getting memory leak; please help me regrads:rose: anju

        J Offline
        J Offline
        Johnny
        wrote on last edited by
        #3

        If you are just doing: delete []objSomec->objSomeA delete []objSomec then you are missing out deleting the other 9 'new CSomeA's. That is, you allocate 10 CSomec's, and for each of these, you allocate 10 CSomeA's - you need to delete these 10 as well, the delete[] will correctly delete the 10 CSomeCs, but it wont delete the 10 CSomeA's that are embedded in each CSomeC. To do this, you should add a destructor to CSomeC that has the same loop as above, but calls delete[] objSomeC[nIndex].objSomeA

        1 Reply Last reply
        0
        • H Hans Ruck

          for(int nIndex=0;nIndex<10;nIndex++)
          {
          delete [] objSomeC[nIndex].objSomeA; // =new CSomeA[10];
          }
          delete [] objSomeC;

          If you want to do it at the object's destruction, you might use this variant:

          class CMainOne
          {

          public:
          class CSomeA
          {
          public:
          int nXA;
          int nYA;
          };

          class CSomeC
          {
          public:
          ~CSomeC()
          {
          delete [] objSomeA;
          }
          int nXC;
          int nYC;
          CSomeA *objSomeA;
          };

          CMainOne();
          ~CMainOne()
          {
          delete [] objSomeC;
          }
          CSomeC * objSomeC;

          };//Class CMainOne

          //In MainOne.cpp

          CMainOne::CMainOne()
          {
          objSomeC=new CSomeC[10];

          for(int nIndex=0;nIndex<10;nIndex++)
          {
          objSomeC[nIndex].objSomeA=new CSomeA[10];
          }
          }

          with the usual checkins i've omitted for brevity. rechi

          A Offline
          A Offline
          anju
          wrote on last edited by
          #4

          Hi rechi, thnaks now Code working fine.. regards:rose: anju

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups