Question: please help in fixing memory leaks in below classes shelf.cpp #include Shelf.h Shelf::Shelf(string n, int s) { name = n; capacity = s; foodRow =

please help in fixing memory leaks in below classes shelf.cpp

#include "Shelf.h"

Shelf::Shelf(string n, int s) { name = n; capacity = s; foodRow = new const Food*[s]; size = 0; } void Shelf::AddFood(const Food *f) { if(size < capacity) { foodRow[size++] = f; } } void Shelf::ShowInventory() const { cout << "Food in " << name << endl; for( int i=0; iDisplay(); } } unsigned int Shelf::GetCapacity() const { return capacity; } unsigned int Shelf::GetCount() const{ return size; } int Shelf::getTotalPrice() const { int p = 0; for(int i=0; iGetPrice(); } return p; } const Food** Shelf::GetFoodList() const { return foodRow; }

const char* Shelf::GetName() { return name.c_str(); }

Cabinet.cpp

#include "Cabinet.h"

Cabinet::Cabinet(string n, int s) { name = n; capacity = s; shelvesRow = new Shelf*[s]; size = 0; } void Cabinet::AddShelf(Shelf *f) { if(size < capacity) { shelvesRow[size++] = f; } } void Cabinet::ShowInventory() { //cout << "+++++++++++++++++++++++++++++++++" << endl; cout << "Inventory of " << name << endl;

for(int i=0; iShowInventory(); }

//cout << "+++++++++++++++++++++++++++++++++" << endl; }

double Cabinet::GetAveragePrice() { double totalPrice = 0; int count = 0; for(int i=0; igetTotalPrice(); count += shelvesRow[i]->GetCount(); }

return totalPrice/count; }

am getting below leaks

LEAKER: errors found! Leaks found: 3 allocations (72 bytes). unknown:unknown():0 memory leak: memory was not deallocated. unknown:unknown():0 memory leak: memory was not deallocated. unknown:unknown():0 memory leak: memory was not deallocated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!