开发者

how to destruct an array

开发者 https://www.devze.com 2022-12-13 13:22 出处:网络
#includ开发者_开发技巧e <cstdlib> #include <iostream> using namespace std; const unsigned long MAX_SIZE = 20;
#includ开发者_开发技巧e <cstdlib>
#include <iostream>

using namespace std;

const unsigned long MAX_SIZE = 20;
typedef int ItemType;

class Heap {
private:
        ItemType array[MAX_SIZE];
        int elements; //how many elements are in the heap
public:
      Heap( )
       ~Heap( )
       bool IsEmpty( ) const
      bool IsFull( ) const
      Itemtype Retrieve( ) 
      void Insert( const Itemtype& )
};

Let's say I have this as my header file. In my implementation for this, what is the best way to do Heap() constructor and ~Heap() destructor.

I have

Heap::Heap()
{
   elements = 0;
}

Heap::~Heap()
{
   array = NULL;
}

I am wondering if this is the proper way to destruct and construct an array in this case.


array is not dynamically allocated, so the storage for it goes away when the object no longer exists in scope. In fact, you can't reassign to array; it's an error to do so.


There are two types of arrays in C++: static and dynamic. The main difference between them lies in how the memory for them is allocated. Static arrays are more or less automatically created and destroyed by your compiler. Dynamic arrays need to be created and destroyed by the programmer. Your object currently uses a static array, so there is no need for you to worry about its creation or destruction.

However, if you want to switch your array to a dynamic array, you could change your code as follows:

typedef int ItemType;
ItemType *array;  // Pointer to location in memory where the array will reside.

Heap::Heap()
{
    array = new ItemType[MAX_SIZE];  // Assign memory for the array to use.
    elements = 0;
}

Heap::~Heap()
{
   delete[] array;  // Clear the memory used by the array.
}


There is nothing needs to be done in the dtor, hence you need not write one. The memory for array object is not allocated dynamically. Hence when the Heap object goes out of scope the memory allocated for array is automatically released.


Array like in your original example is a subobject of your Heap class. It is constructed automatically by Heap constructors and destructed automatically by Heap destructor. You don't need to do anything to construct and/or destruct it.


Since your array is allocated statically (ie, without using new), the destructor doesn't actually need to do anything - as soon as the created heap goes out of scope (or is explicitly deleted if it's created dynamically) the array will disappear.

Only when allocating memory dynamically (eg, with new or malloc() in the case of C code) do you need to explicitly delete (or free()) it.


You don't have to destroy your array, as it is used by value and only uses values (int).


If you change the typedef to pointer based type you should delete every ItemType of the array.

So, you can iterate through the array and delete them

for ( int i = 0; i < elements; i++ )
  delete array[i] // Supposing you created them with new
0

精彩评论

暂无评论...
验证码 换一张
取 消