Which is the difference between these two codes? Is there a memory leak in the first case?
no destructor defined
class Library
{
private:
Book books[50];
int index;
public:
Library()
{
index=0;
}
};
or with destructor defined
class Library
{
private:
Book *books;
int index;
public:
Library()
{
books=new Book[50];
index=0;
}
~Library()
{
delete books;
}
};
should be
In the second one:
booksis allocated as an array, so must be deleted as an array. The first version does not have a memory leak.The difference is that in the first case the memory for
booksis is contained within the allocation for each instance ofLibrary, wheras in the second case it is allocated separately, using the heap.