tags:

views:

346

answers:

7
#include <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.

A: 

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

Laurent Etiemble
Isn't the term "by value" used when passing parameters around? Maybe you mean the right thing but I find your answer unclear.
mxp
I mean "by value" when the access it not "by reference", i.e. a direct access to the value of the field.
Laurent Etiemble
+2  A: 

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.

Naveen
is there something wrong in the answer? Would love to hear the reason for the downvote.
Naveen
That is technically incorrect. The array object is created within the memory block allocated for the instance of the Heap class. Thus, the array object will be created on the stack only when the Heap instance is allocated on the stack. If the instance is allocated on the heap, the array object will be in that block.
Franci Penov
May be I use the term stack a bit loosely..what I meant is that the memory for the array object is not allocated dynamically and hence no explicit memory management is necessary..It will automatically cleanup when the encapsulating object is freed.
Naveen
It seems to happen quite frequently on this site, that the first or second answer to a question is either accepted or voted up more than others - seemingly on the virtue that it arrived faster than the others.
Swiss
What does "on the stack" doing there? Why "on the stack"? The array member is an integral part of `Heap` object. It will be created wherever the `Heap` object is created, which is not necessarily "on the stack" (whatever that means).
AndreyT
Naveen, edit it acording to comments and I'll vote up.
the_drow
+6  A: 

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.

Alok
A: 

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
HyLian
And if you change the implementation to dynamically allocate the array using "new", then you would need to do this afterward:delete[] array;
SapphireSun
+1  A: 

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.

Daniel G
"your array is allocated statically on the stack" - that is technically incorrect. The array object is created within the memory block allocated for the instance of the Heap class. Thus, the array object will be created on the stack only when the Heap instance is allocated on the stack. If the instance is allocated on the heap, the array object will be in that block.
Franci Penov
Ah, good point. It doesn't really change the meat of the answer though - the array will be destroyed when the object is destroyed (by going out of scope if the object is allocated statically, or by being explicitly deleted/free()ed if it is allocated dynamically), so there's no need for a destructor.
Daniel G
A static array would have the lifetime of the program. Just remember that static allocation is different from automatic and dynamic allocation.
Thomas Matthews
+2  A: 

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.
}
Swiss
create/construct/allocate arrays, not "assign". You can't assign arrays in C++
jalf
oh, and the array isn't static. It's a nonstatic class member.
jalf
Clarifying, there is only one kind of array in C++. There is more than one kind of storage. The functionality of the array does not depend on where it is located; only its lifetime. An array declared in a function is alive until execution leaves the function. A dynamically array is alive until it is deallocated or the program terminates.
Thomas Matthews
+1  A: 

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.

AndreyT