I have Memory Leak in my code and not sure how to diagnose it. There are 3 files and the main.cpp is the test file therefore cannot be altered. The program is using a Mem Checker and it displays that ==159804== Memcheck, a memory error detector, definitely lost: 280 bytes in 2 blocks.
Main.cpp (Cannot be altered)
#include<iostream>
#include<cstring>
#include"Basket.h"
#include"Basket.h" //intentional
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
int main()
{
sdds::Fruit fruits[]{
{"apple", 0.65},
{"banana", 1.25},
{"pear", 0.50},
{"mango", 0.75},
{"plum", 2.00},
};
{
printHeader("T1: Default Constructor");
Basket aBasket;
cout << aBasket;
// conversion to bool operator
if (aBasket)
cout << "Test failed: the basket should be empty!\n";
else
cout << "Test succeeded: operator said the basket is empty!\n";
cout << endl;
}
{
printHeader("T2: Custom Constructor");
Basket aBasket(fruits, 2, 6.99);
cout << aBasket;
// conversion to bool operator
if (aBasket)
cout << "Test succeeded: operator said the basket has content!\n";
else
cout << "Test failed: the basket should NOT be empty!\n";
cout << endl;
}
{
printHeader("T3: += operator");
Basket aBasket;
aBasket += fruits[2];
(aBasket += fruits[0]) += fruits[4];
aBasket.setPrice(12.234);
cout << aBasket;
cout << endl;
}
{
printHeader("T4: Copy Constructor");
Basket b1;
Basket b2(b1);
cout << "Basket #1 -> " << b1;
cout << "Basket #2 -> " << b2;
b1 += fruits[3];
b1.setPrice(3.50);
Basket b3(b1);
cout << "Basket #3 -> " << b3;
cout << endl;
}
{
printHeader("T5: Copy Assignment");
Basket b1, b2, b3(fruits, 5, 19.95);
b1 = b2;
cout << "Basket #1 -> " << b1;
cout << "Basket #2 -> " << b2;
b1 = b3;
cout << "Basket #1 -> " << b1;
b3 = b2;
cout << "Basket #3 -> " << b3;
}
return 0;
}
Basket.h
#ifndef Basket_h
#define Basket_h
#include <stdio.h>
#include <iomanip>
#include <iostream>
namespace sdds{
struct Fruit
{
char m_name[30 + 1];
double m_qty;
};
class Basket{
private:
Fruit *m_fruits;
int m_cnt;
double m_price;
public:
Basket();
Basket(Fruit* fruits, int cnt, double price);
Basket(Basket &d);
Basket& operator = (Basket &d);
~Basket();
void setPrice(double price);
operator bool() const;
Basket& operator+=(Fruit d);
friend std::ostream& operator << (std::ostream& output, Basket test);
};
}
#endif /* Basket_h */
Basket.cpp
#include "Basket.h"
using namespace sdds;
namespace sdds {
Basket::Basket(){
m_fruits = nullptr;
m_cnt = 0;
m_price = 0;
}
Basket::Basket(Fruit* fruits, int cnt, double price){
if (cnt > 0 && fruits != nullptr) {
m_cnt = cnt;
m_price = price;
m_fruits = new Fruit[cnt + 1];
for (int i = 0; i < cnt; i++) {
m_fruits[i] = fruits[i];
}
}else{
m_fruits = nullptr;
m_cnt = 0;
m_price = 0;
}
}
Basket::Basket(Basket &d){
m_price = d.m_price; // Shallow Copying
m_cnt = d.m_cnt;
m_fruits = new Fruit[m_cnt + 1];
for (int i = 0; i < m_cnt; i++) { // Deep Copying
m_fruits[i] = d.m_fruits[i];
}
}
Basket& Basket::operator = (Basket &d){
m_price = d.m_price;
m_cnt = d.m_cnt;
m_fruits = new Fruit[m_cnt + 1];
for (int i = 0; i < m_cnt; i++) {
m_fruits[i] = d.m_fruits[i];
}
return *this;
}
Basket::~Basket(){
delete [] m_fruits;
}
void Basket::setPrice(double price){
m_price = price;
}
Basket::operator bool() const {
// returning true if the Basket is valid
return m_fruits != nullptr;
}
Basket& Basket::operator+=(Fruit d){
Fruit* tmp = new Fruit[m_cnt + 1];
for (int i = 0; i < m_cnt; i++) {
tmp[i] = m_fruits[i];
}
tmp[m_cnt++] = d;
delete [] m_fruits;
m_fruits = tmp;
return *this;
}
std::ostream& operator << (std::ostream& output, Basket test){
if (test.m_cnt == 0 || test.m_price == 0 || test.m_fruits == nullptr) {
output << "The basket is empty!" << std::endl;
}else{
output << "Basket Content:" << std::endl;
std::cout << std::fixed;
std::cout << std::setprecision(2);
for (int i = 0 ; i < test.m_cnt; i++) {
output << std::setw(10) << test.m_fruits[i].m_name << ": " <<test.m_fruits[i].m_qty << "kg" << std::endl;
}
output << "Price: " << test.m_price << std::endl;
}
return output;
}
}