1. // identify all the memory leaks int main() { int *p1, *p2, *p3; p1 = new int
ID: 3553780 • Letter: 1
Question
1. // identify all the memory leaks
int main()
{
int *p1, *p2, *p3;
p1 = new int;
p2 = new int;
p3 = p1;
p1 = new int;
p2 = p1;
p1 = new int;
p1 = new int;
return 0;
}
2.// fill in the details of the myGetDataP function to effectively do what myGetData does
class MyClass
{
public:
MyClass()
{
number1 = 40;
number2 = 90;
}
void myGetData(int &num1, int &num2)
{
num1 = number1;
num2 = number2;
}
void myGetDataP( int *num1, int *num2)
{
// do effectively the same as above
}
private:
int number1, number2;
};
int main()
{
int *x1, *x2;
int x3, x4;
x1 = new int;
x2 = new int;
MyClass mc;
mc.myGetData(x3, x4);
cout << "the two numbers are " << x3 << ", and " << x4 << endl;
mc.myGetDataP(x1, x2);
cout << "the two numbers are " << *x1 << ", and " << *x2 << endl;
return 0;
}
3. Take the ToolBox2 class from code below and modify it so that it can accomodate any number of tool types. It currently can have only three types of tools.
#include <iostream>
#include <string>
using namespace std;
class Tool
{
public:
string getType()
{
return type;
}
int getQuant()
{
return quantity;
}
void set(string s, int q)
{
type = s;
quantity = q;
}
private:
string type;
int quantity;
};
class ToolBox1
{
public:
void setFirst(string s, int q)
{
first.set(s, q);
}
void setSecond(string s, int q)
{
second.set(s, q);
}
void setThird(string s, int q)
{
third.set(s, q);
}
void display()
{
cout << "the tools are "
<< first.getType() << ": " << first.getQuant() << endl
<< second.getType() << ": " << second.getQuant() << endl
<< third.getType() << ": " << third.getQuant() << endl;
}
private:
Tool first, second, third;
};
class ToolBox2
{
public:
void set(int index, string s, int q)
{
t[index].set(s, q);
}
void display()
{
cout << "the tools are ";
for (int i=0; i<3; i++)
cout << t[i].getType() << ": " << t[i].getQuant() << endl;
}
private:
Tool t[3];
};
int main()
{
ToolBox1 tb1;
tb1.setFirst("screwdrivers", 8);
tb1.setSecond("pliers", 5);
tb1.setThird("wrenches", 20);
tb1.display();
ToolBox2 tb2;
tb2.set(0, "screwdrivers", 8);
tb2.set(1, "pliers", 5);
tb2.set(2, "wrenches", 20);
tb2.display();
return 0;
}
Explanation / Answer
int main()
{
int *p1, *p2, *p3;
p1 = new int;
p2 = new int;
p3 = p1;
p1 = new int;
p2 = p1; // memory leak
p1 = new int;
p1 = new int; // memory leak
// p1 not free memory leak
// p2 not free memory leak
// p3 not free memory leak
// total 5 leaks
return 0;
}
2.
void myGetDataP( int *num1, int *num2)
{
*num1 = number1;
*num2 = number2;
}
3.
class ToolBox2
{
public:
ToolBox2()
{
t = new Tool[10];
size = 10;
}
ToolBox2(int num)
{
t = new Tool[num];
size = num;
}
~ToolBox2()
{
delete[] t;
}
void set(int index, string s, int q)
{
if (index < size && index >= 0)
t[index].set(s, q);
}
void display()
{
cout << "the tools are ";
for (int i=0; i<size; i++)
cout << t[i].getType() << ": " << t[i].getQuant() << endl;
}
private:
Tool* t;
int size;
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.