Loading ...
Sorry, an error occurred while loading the content.

C++ destructor related problem.

Expand Messages
  • Pardeep Kumar, Goyal (Goyal)** CTR **
    Hi All, I am posting a small program which I ran on Linux machine. The program code is: ============================================== class ofNoUse { private:
    Message 1 of 3 , Oct 24, 2006
    • 0 Attachment
      Hi All,
      I am posting a small program which I ran on Linux machine. The program code
      is:

      ==============================================
      class ofNoUse
      {
      private: int val;
      public:

      ofNoUse()
      {
      cout<< "\nCreatingofNoUseObjectByDefault";
      cout.flush();
      }

      ofNoUse(int i)
      {
      val=i;
      cout<<"\nCreatingofNoUseObjectNotByDefault";
      cout.flush();
      }

      ~ofNoUse()
      {
      cout<<"\nDestroyingofNoUse";
      cout.flush();
      }
      };


      int main()
      {

      int **mArr1;
      int nRows,nCols;

      cout<<"Rows:";
      cin>>nRows;
      cout<<"Cols:";
      cin>>nCols;
      cout.flush();

      ofNoUse **myofNoUse;
      myofNoUse = new ofNoUse*[nRows];
      for(int i=0;i<nRows;i++)
      myofNoUse[i]=new ofNoUse[nCols];

      (cout<<"\nofNoUse Object Created").flush();



      //Deleting ofNoUse Objects.

      for ( int i=nRows-1; i=0; i-- ) // NOTE: Go Reverse.
      delete []myofNoUse[i];
      delete []myofNoUse;
      (cout<<"\nIS myofNoUse deleted? ").flush();
      }

      ========================================


      I have two problems in this program. I ran this program in Linux Fedora.
      Here I am initializing a 2-dimensional array using new. I can see
      constructor calls and it is happening fine.

      1) But while deallocating the array the default destructor is not getting
      called. Please let me know why ?
      2) Also If I want to initialize the array objects with other than default
      constructor, how to do so ?

      Please let me know if you need any other info.

      Thanks
      Pardeep
    • Philip Tellis
      ... Note: go nowhere. Unless nRows==1, this loop will never execute. ie, you re not deleting anything. If nRows==1, the loop will execute infinitely. You
      Message 2 of 3 , Oct 24, 2006
      • 0 Attachment
        Sometime on Oct 24, Pardeep Kumar, Goyal (Goyal)** CTR ** assembled some...:

        > for ( int i=nRows-1; i=0; i-- ) // NOTE: Go Reverse.

        Note: go nowhere. Unless nRows==1, this loop will never execute. ie,
        you're not deleting anything. If nRows==1, the loop will execute
        infinitely. You probably wanted to say i>=0;

        > 2) Also If I want to initialize the array objects with other than default
        > constructor, how to do so ?

        You can't. At least not the way you want to do it.

        You can do something like this though:

        foo f[3] = {foo(1), foo(), foo(3)};

        to create an array of 3 foos each with a different value.
      • Om Narasimhan
        ... Wrong logic it seems. you have i=0 (I think you meant 0 == i) Assume nRows = 10 before the loop, then, for ( i = 9, 0 == i, i --) {...} Won t execute,
        Message 3 of 3 , Oct 24, 2006
        • 0 Attachment
          > //Deleting ofNoUse Objects.
          >
          > for ( int i=nRows-1; i=0; i-- ) // NOTE: Go Reverse.
          > delete []myofNoUse[i];
          > delete []myofNoUse;
          > (cout<<"\nIS myofNoUse deleted? ").flush();
          > }
          > I have two problems in this program. I ran this program in Linux Fedora.
          > Here I am initializing a 2-dimensional array using new. I can see
          > constructor calls and it is happening fine.
          >
          > 1) But while deallocating the array the default destructor is not getting
          > called. Please let me know why ?
          Wrong logic it seems.
          you have i=0 (I think you meant 0 == i)
          Assume nRows = 10 before the loop, then,
          for ( i = 9, 0 == i, i --) {...}
          Won't execute, since i != 0.
          Fix the logic first.

          Regards,
          Om.
        Your message has been successfully submitted and would be delivered to recipients shortly.