1

In the following code:

#include <iostream>
using namespace std ;

class TypeA {
 public:
  int a[10] ;
  TypeA () {
    int i ;
    for ( i = 0; i != 9; i++) {
      a[i] = double(i) ;
    }
  }
  void printVal() {
    int i ;
    for ( i = 0; i != 9; i++) {
      cout << a[i] << endl ; 
    }
  }
  ~TypeA() {
    //delete[] a;
    cout << "TypeA destructor called"<<endl;
  }

} ;


int main() {
  TypeA * ptrA = new TypeA() ;
  ptrA->printVal();
  delete ptrA ;

  return 0 ;
}

By new TypeA(), an object is instantiated on heap and a pointer is returned and kept in main(). I wonder if a[] of TypeA is stored on heap? If so, what is the appropriate way to free it ?

2
  • Can you explain what led you to such an impression (it's not, BTW)? Commented Mar 23, 2021 at 1:42
  • 2
    You don't need to free it - C++ puts everything into the Class'es memory .. so allocating a new TypeA gives space for the int[10] array within the memory space for the rest of class. When you delete the Class, you are automatically freeing the memory that is used for the int[10] array. No memory leak there at all ... Commented Mar 23, 2021 at 1:44

2 Answers 2

3

Generally unless your class contains pointers then the entire object is contained in one allocation. As such you don't need to do anything special to clear it.

You can only call delete[] on things you've previously called new[] on, and it must be exactly the same pointer you were given. Since the a property was never directly instantiated, calling delete on it is undefined behaviour.

The way new works conceptually isn't that exotic, it just allocates sizeof(X) bytes and calls the constructor on that object. new[] differs only in that it allocates sizeof(X) * N bytes and calls the constructor N times.

Sign up to request clarification or add additional context in comments.

1 Comment

new[] also stores how many objects where allocated.
0

Try this code:

#include <iostream>
#include <type_traits>
#include <cassert>
struct A{
    A() {
        std::cout<<"Called A() for "<<std::hex<<this<<'\n';
    }
};

struct B {
    A arr[10];
    B() {
        std::cout<<"Called B() for "<<std::hex<<this<<'\n';
    }
};

int main() {
    B* ptr = new B;
    static_assert(std::is_standard_layout<B>::value); //Sanity check for this example's case
    std::cout<<"Got ptr = "<<std::hex<<ptr<<std::dec<<" + sizeof(B)  = "<<sizeof(B)<<'\n';
    delete ptr;
    return 0;
}

Output is:

Called A() for 0x10d4eb0
Called A() for 0x10d4eb1
Called A() for 0x10d4eb2
Called A() for 0x10d4eb3
Called A() for 0x10d4eb4
Called A() for 0x10d4eb5
Called A() for 0x10d4eb6
Called A() for 0x10d4eb7
Called A() for 0x10d4eb8
Called A() for 0x10d4eb9
Called B() for 0x10d4eb0
Got ptr = 0x10d4eb0 + sizeof(B)  = 10

If you see the code and the corresponding output, you can correlate that the object ptr of type B is allocated on the heap at 0x10d4eb0 and it has a size of 10 (corresponding to 10 bytes, 1 byte each for the object A). So the memory address from 0x10d4eb0 till 0x10d4eb9 is all on the heap. And if you see the address of the objects of A, they all fall in this address range. Hence, the objects of A do lie in the heap memory area.

Now coming to how to handle arr, one needs to call delete/delete[], as the case may be, only on objects that have been allocated using new/new[]. Since that's not the case for arr, one doesn't need to call delete[] on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.