3

Being a noob to OOP and dynamic allocation, I am not sure if I am framing the question correctly. Anyway, here is the code. Note the constructor and destructor.

class Queue
{
    int *array;
    unsigned int siz;//Indicates how many elements are there right now
    public: 
    void push(int);
    void clear();
    void print() const;
    Queue(unsigned int len):array(new int[len]), siz(0) {} /*Constructor
    indicating the maximum length*/
    ~Queue(){delete[] array;}   //Destructor to free the memory
    };
/*End of class definition */
    void somefunction()
    {
        Queue sample(10);  //Allocates memory from heap
        .........
    }
}

The Queue class contains an array and an unsigned integer indicating how many elements are there. The constructor takes an integer argument (indicating the maximum number of elements) and allocates space in the memory. The code works for my purpose whether I write the destructor or not, but I want to understand a bit of what is going on behind the scene when somefunction creates a queue.

sample itself is a local object, but it takes memory from the heap. So when sample goes out of scope at the end of somefunctionwhat happens to the allocated memory if I

  1. write the destructor
  2. do not write the destructor

Do I need to write the destructor to avoid a memory leak? Also, if I do write it, does it get called automatically or do I need to somehow call it to ensure the memory is not leaked?

Also, what would be the role of the destructor if the class contained, for example, int array[10]; instead of int *array? Could I just skip writing it then?

Della
  • 1,264
  • 2
  • 15
  • 32
  • 2
    The destructor is called automatically as soon as the object goes out of scope. If you don't write a destructor, the memory will not be deallocated and you will have a memory leak. – DeiDei Jan 06 '17 at 03:24
  • The answer is: You need to write a destructor and deallocate the memory in it. But that's not all you need. See [What is the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – R Sahu Jan 06 '17 at 03:24
  • @DeiDei Is writing an explicit destructor necessary only when a local object takes up space from the heap? What if the class contained, for example, int array[10] instead of int *array? Would I need the destructor then? – Della Jan 06 '17 at 03:26
  • use `std::unique_ptr` – Guillaume Racicot Jan 06 '17 at 03:37
  • @Della static arrays do not need explicit freeing. Dynamically allocated arrays do. So, if the class contains `int array[10]` then it does not need an explicit destructor, but if it contains `int *array = new int[len]` then it does. Or, you can switch to `std::vector array(len)` instead and get the best of both worlds - dynamic allocation and automatic implicit cleanup. – Remy Lebeau Jan 06 '17 at 07:26

2 Answers2

3

Do I need to write the destructor to avoid a memory leak?

Dynamic allocation uses Heap memory and it is not freed unless you have a destructor that clears that memory. So Yes, you do need to write a destructor when you use heap memory.

if I do write it, does it get called automatically or do I need to somehow call it to ensure the memory is not leaked?

Destructor is automatically called when the object goes out of the scope.

what happens to the allocated memory if I write the destructor

When you have a destructor in your code, the dynamically allocated memory is cleared and made available to the operating system as soon as the object goes out of the scope.

what happens to the allocated memory if I do not write the destructor

When you don't have a destructor in your code, the dynamically allocated memory is not cleared and is not available to the operating system until you close the program.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
3

Look out, we have a leak! For every new, a program needs a delete. Good work on knowing to use the array delete!

The short course answer is that classes are assigned a default destructor if you do not take action. The compiler just slip one in there on you when you are looking the other way. The same is true for constructors and copy assignment operators ... and with C++11 this is also true for move assignment operators too. In this case, the default destructor does not deallocate the array and so there is a leak ... bad news, right?!

This type of code is largely obsolete but you are asking exactly the right questions to dig deep and learn more! Good for you and keep working at it. There are reams of excellent reference material on the subject and if you want to learn the depths of C++ you should definitely get your hands on a copy of any and all of Scott Meyers' Effective C++ series. These books cover loads of territory but definitely cover default constructors/destructor, delete vs array delete, and I'm guessing that Effective C++11 covers the reference counting techniques in std::unique_ptr and std::shared_ptr.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Ben
  • 1,234
  • 2
  • 8
  • 5