Below is the .h file where I am having trouble defining the reserve funciton. I
ID: 3784856 • Letter: B
Question
Below is the .h file where I am having trouble defining the reserve funciton. I have kept my input below but am getting an error.
template <class dynElem>
class dynarr {
private:
int capacity;
dynElem *A;
public:
dynarr() : capacity(0), A(NULL){};
dynarr(int N): capacity(N), A(new dynElem[N]){}
dynarr(const dynarr<dynElem> &other);
~dynarr();
dynarr<dynElem> & operator=( const dynarr<dynElem> &other);
dynElem & operator[](int ndx) throw(InvalidIndex);
int getCapacity();
void reserve(int newcap);
// if newcap <= capacity, does nothing;
// if capacity is 0, allocates a dynamic array of
// capacity newcap and makes A point to that array;
// otherwise allocates a new dynamic array newA of capacity
// newcap, copies values in A to newA, deletes A and sets
// A equal to newA
};
Reserve Function what I have so far. Getting malloc error for object pointer being freed was not allocated
template <class dynElem>
void dynarr<dynElem>::reserve(int newcap)
{
if (newcap <= capacity)
{
//does nothing
}
else if (capacity == 0)
{
//new dynamic array and A points to it.
A = new dynElem[newcap];
}
else
{
//allocates new dynamic array newA w capacity newcap
//copies all values in A to newA, deletes A and sets A equal to newA
dynElem *newA = new dynElem[newcap];
for (int i = 0; i < capacity; i++)
{
newA[i] = A[i];
delete A;
A = newA;
capacity = newcap;
}
}
The output is supposed to be that an array can reserve a capacity of a previous array's amount. Test file with main funciton is below
#include "dynarr.h"
#include <iostream>
#include <string>
int doubleIt(dynarr<int> A)
{
int newsize = 2*A.getCapacity();
A.reserve(newsize);
return newsize;
}
int main()
{
dynarr<int> D(15);
std::cout << "The capacity of D is " << D.getCapacity() << std::endl;
dynarr<int> E(D);
std::cout << "The capacity of E is " << E.getCapacity() << std::endl;
D.reserve(25);
std::cout << "The capacity of D is now " << D.getCapacity() << std::endl;
E.reserve(2*E.getCapacity());
std::cout << "The capacity of E is now " << E.getCapacity() << std::endl;
D[0] = 11;
std::cout << "D[0] = " << D[0] << std::endl;
try {
std::cout << D[-1] << std::endl;
}
catch(InvalidIndex &ex) {
std::cout << ex.getMessage() << std::endl;
}
try {
std::cout << D[30] << std::endl;
}
catch(InvalidIndex &ex) {
std::cout << ex.getMessage() << std::endl;
}
return 0;
}
Explanation / Answer
Hi, I have fixed the issue.
Please test now and let me know in case of any problem.
template <class dynElem>
void dynarr<dynElem>::reserve(int newcap)
{
if (newcap <= capacity)
{
//does nothing
}
else if (capacity == 0)
{
//new dynamic array and A points to it.
A = new dynElem[newcap];
}
else
{
//allocates new dynamic array newA w capacity newcap
//copies all values in A to newA, deletes A and sets A equal to newA
dynElem *newA = new dynElem[newcap];
for (int i = 0; i < capacity; i++)
{
newA[i] = A[i];
}
delete A;
A = newA;
capacity = newcap;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.