How should be delete for this allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pavan734@gmail.com

    #1

    How should be delete for this allocation

    Hello, I have a array of double pointers as shown below

    dummy_class **ptr[10] ; //assume dummy class to be some c++ class
    I have allocated it like this:

    for(int i = 0 ; i<10 ; i++)
    {
    ptr[i] = new dummy_class* [5] ;
    for(int j=0 ; j<5 ; j++) // This 5 is because of previous
    allocation of 5
    {
    ptr[i][j] = new dummy_class ;
    }
    }

    1. Whether the above allocation is valid
    2. How will be delete statements for this.

  • Ron Natalie

    #2
    Re: How should be delete for this allocation

    pavan734@gmail. com wrote:[color=blue]
    > Hello, I have a array of double pointers as shown below
    >
    > dummy_class **ptr[10] ; //assume dummy class to be some c++ class
    > I have allocated it like this:
    >
    > for(int i = 0 ; i<10 ; i++)
    > {
    > ptr[i] = new dummy_class* [5] ;
    > for(int j=0 ; j<5 ; j++) // This 5 is because of previous
    > allocation of 5
    > {
    > ptr[i][j] = new dummy_class ;
    > }
    > }
    >
    > 1. Whether the above allocation is valid
    > 2. How will be delete statements for this.
    >[/color]

    1, Looks OK to me.
    2. The deallocation needs to mirror the allocations.

    for(i = 0; i < 10; ++i) {
    for(int j = 0; j < 5; ++j)
    delete ptr[i][j];
    delete ptr[i];
    }

    Comment

    • S. I. Becker

      #3
      Re: How should be delete for this allocation

      pavan734@gmail. com wrote:[color=blue]
      > Hello, I have a array of double pointers as shown below
      >[/color]
      <snip code>[color=blue]
      >
      > 1. Whether the above allocation is valid
      > 2. How will be delete statements for this.
      >[/color]

      Is this homework?

      Stewart

      Comment

      • pavan734@gmail.com

        #4
        Re: How should be delete for this allocation


        Ron Natalie wrote:
        [color=blue][color=green]
        > >[/color]
        >
        > 1, Looks OK to me.
        > 2. The deallocation needs to mirror the allocations.
        >
        > for(i = 0; i < 10; ++i) {
        > for(int j = 0; j < 5; ++j)
        > delete ptr[i][j];
        > delete ptr[i];
        > }[/color]

        Thank you. But is it not possible to delete using delete[] ?

        Comment

        • Victor Bazarov

          #5
          Re: How should be delete for this allocation

          pavan734@gmail. com wrote:[color=blue]
          > Ron Natalie wrote:
          >[color=green][color=darkred]
          >>>[/color]
          >>
          >> 1, Looks OK to me.
          >> 2. The deallocation needs to mirror the allocations.
          >>
          >> for(i = 0; i < 10; ++i) {
          >> for(int j = 0; j < 5; ++j)
          >> delete ptr[i][j];
          >> delete ptr[i];
          >> }[/color]
          >
          > Thank you. But is it not possible to delete using delete[] ?[/color]

          Ron made a mistake. If you allocated using 'new[]', you should
          deallocate using 'delete[]'. The proper deallocation code would
          be

          for(int i = 0 ; i<10 ; i++) {
          for(int j=0 ; j<5 ; j++)
          delete ptr[i][j];
          delete[] ptr[i];
          }

          When my OCD is acting up, I even rewrite the loops so that the
          deallocations are in the reverse order of allocations:


          for(int i = 10 ; i-- >0;) {
          for(int j=5 ; j-- >0;)
          delete ptr[i][j];
          delete[] ptr[i];
          }

          (I hope I got the indexing correctly :-])

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • Tom Widmer

            #6
            Re: How should be delete for this allocation

            pavan734@gmail. com wrote:[color=blue]
            > Ron Natalie wrote:
            >
            >[color=green]
            >>1, Looks OK to me.
            >>2. The deallocation needs to mirror the allocations.
            >>
            >>for(i = 0; i < 10; ++i) {
            >> for(int j = 0; j < 5; ++j)
            >> delete ptr[i][j];
            >> delete ptr[i];
            >>}[/color]
            >
            >
            > Thank you. But is it not possible to delete using delete[] ?[/color]

            Ron's code contained an error - it should be:

            for(i = 0; i < 10; ++i) {
            for(int j = 0; j < 5; ++j)
            delete ptr[i][j];
            delete[] ptr[i];
            }

            Every new you write should have a matching delete, and every new[] and
            matching delete[].

            Tom

            Comment

            • Victor Bazarov

              #7
              Re: How should be delete for this allocation

              Ron Natalie wrote:[color=blue]
              > pavan734@gmail. com wrote:[color=green]
              >> Hello, I have a array of double pointers as shown below
              >>
              >> dummy_class **ptr[10] ; //assume dummy class to be some c++ class
              >> I have allocated it like this:
              >>
              >> for(int i = 0 ; i<10 ; i++)
              >> {
              >> ptr[i] = new dummy_class* [5] ;
              >> for(int j=0 ; j<5 ; j++) // This 5 is because of previous
              >> allocation of 5
              >> {
              >> ptr[i][j] = new dummy_class ;
              >> }
              >> }
              >>
              >> 1. Whether the above allocation is valid
              >> 2. How will be delete statements for this.
              >>[/color]
              >
              > 1, Looks OK to me.
              > 2. The deallocation needs to mirror the allocations.
              >
              > for(i = 0; i < 10; ++i) {
              > for(int j = 0; j < 5; ++j)
              > delete ptr[i][j];
              > delete ptr[i];[/color]

              delete[] ptr[i];
              [color=blue]
              > }[/color]

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • pavan734@gmail.com

                #8
                Re: How should be delete for this allocation

                Thank you everyone for your kind help.

                Comment

                • tragomaskhalos

                  #9
                  Re: How should be delete for this allocation


                  8 posts already in this thread and no-one has suggested that the OP use
                  std::vector. You guys must be going soft !

                  Comment

                  • Ron Natalie

                    #10
                    Re: How should be delete for this allocation

                    pavan734@gmail. com wrote:[color=blue]
                    > Ron Natalie wrote:
                    >[color=green]
                    >> 1, Looks OK to me.
                    >> 2. The deallocation needs to mirror the allocations.
                    >>
                    >> for(i = 0; i < 10; ++i) {
                    >> for(int j = 0; j < 5; ++j)
                    >> delete ptr[i][j];
                    >> delete ptr[i];
                    >> }[/color]
                    >
                    > Thank you. But is it not possible to delete using delete[] ?
                    >[/color]

                    Actually the last one should be delete[] my mistake.

                    Comment

                    Working...