//The value item is inserted into the Container at the location
// determined by index
void Container::insert(int item, int index){
if (index < 0 || index > capacityC - 1 || index > sizeC) {
cout<<"*** Illegal location to insert -- "<<index;
cout<<". Container unchanged. ***"<<endl;
return;
}
// Move all elements up one position. i is destination.
int *dest = elements+index;
int *tail = elements+sizeC;
while (tail > dest){
*tail = *(tail-1);
tail--;
}
*dest = item; // put value in vacated position
dest = NULL;
tail = NULL;
//delete dest;
//delete tail;
sizeC++; // update count of items
return;
}
with my class private
1 2 3 4
private:
int sizeC; // current size of Container
int capacityC; // currant capacity of dynamic array
int * elements; // pointer to dynamic array
I have asked my lecturer about this code and she said. that this has memory leak.
the function should be changed such that it will allocate more momory when it is needed.
I know its part of my assingment and Im suppost to do it. But im really stuck on it and any help would be really appreciated.
Thank you.
My code just crashes at comand window and i dont know what im doing wrong spent time googling and reading thought meterials but couldnt find answer :(
my class file.
#include <iostream>
usingnamespace std;
constint DEFAULT=32;
class Container{
friendbooloperator==(const Container &rhs,const Container &lhs);
public:
Container(int maxCapacity = DEFAULT);
~Container();
Container(const Container & origCont);
const Container & operator=(const Container & rightCont);
bool empty() const;
void insert(int item, int index);
void erase(int index);
int size()const;
void display(ostream & out) const;
private:
int sizeC; // current size of Container
int capacityC; // currant capacity of dynamic array
int * elements; // pointer to dynamic array
}; //--- end of Container class
//---test for equal elements values in the two containers rhs and lhs
booloperator==(const Container &rhs,const Container &lhs){
if (rhs.sizeC == lhs.sizeC) {
returntrue;
}
returnfalse;
}
//---Default constructor:
Container::Container(int maxCapacity){
int * intPtr = elements;
intPtr = NULL;
capacityC = maxCapacity;
sizeC = 0;
intPtr = newint[maxCapacity];
for (int i=0; i<maxCapacity; i++) {
intPtr[i] = sizeC; // Initialize all elements to zero.
}
assert(intPtr);
}
//----Destructor:
Container::~Container() {
delete [] elements; // Free memory allocated for the a array.
}
//Copy constructor: constructs a copy of origCont.
Container::Container(const Container & origCont){
elements = newint[origCont.capacityC];
capacityC = origCont.capacityC;
sizeC = origCont.sizeC;
memcpy(elements, origCont.elements, sizeof(int) * sizeC);
}
// Assignment operator : assigns a copy of the rightCont to
//the current object. A const reference to this Container is returned.
const Container & Container::operator=(const Container & rightCont){
if(this == &rightCont) return *this; // handling of self assingment
delete[] elements; // freeing previouslt used memory
elements = newint[rightCont.capacityC];
capacityC = rightCont.capacityC;
sizeC = rightCont.sizeC;
memcpy(elements, rightCont.elements, sizeof(int) * sizeC);
return *this;
}
// Returns true if the Container is empty, false otherwise.
bool Container::empty() const {
if (sizeC == 0 ) {
returntrue;
}
returnfalse;
}
//The value item is inserted into the Container at the location
// determined by index
void Container::insert(int item, int index){
if (index < 0 || index > capacityC - 1 || index > sizeC) {
cout<<"*** Illegal location to insert -- "<<index;
cout<<". Container unchanged. ***"<<endl;
return;
}
// Move all elements up one position. i is destination.
int *dest = elements+index;
int *tail = elements+sizeC;
while (tail > dest){
*tail = *(tail-1);
tail--;
}
*dest = item; // put value in vacated position
sizeC++; // update count of items
return;
}
//The value at the location determined by index is removed,
// provided index is a legal location
void Container::erase(int index){
if (index < 0 || index > capacityC || index > sizeC) {
cout<<"*** Illegal location to erase -- "<<index;
cout<<". Container unchanged. ***"<<endl;
return;
}
int* dest = elements+index;
int* tail = elements+sizeC;
// Move all elements up down position. i is destination.
while ( tail < dest){
*tail = *(tail+1);
tail++;
}
sizeC--; // update count of items
return;
}
/*Returns the number of elements currently stored in this Container*/
int Container::size()const {
return sizeC;
}
//The Container represented by this Container object has been
// inserted into out.
void Container::display(ostream & out) const{
for (int i = 0; i < sizeC; i++) {
out<<elements[i];
out<<" ";
}
}
//------ Prototype of oveloaded output operator
ostream & operator<< (ostream & out, const Container & aCont){
aCont.display(out);
return out;
}