Disclaimer: Yes, I know about std::vector. I'm doing this for the sake of learning.
I'm working on making a dynamic array class, and I'm trying to get add to work.
template <class T>
void Array<T>::add(T value)
{
T * tmp = new T[mCount];
for (int i = 0; i < mCount; i++)
{
tmp[i] = mData[i];
}
mCount++;
delete[] mData;
mData = tmp;
mData[mCount - 1] = value;
}
It works... sort of. The function works in adding the element, but then the program crashes when exiting. No errors, no nothing. It just freezes, and I have to close it using (Shift + F5).
So, what's wrong with this?
Here's my whole class. If I didn't include a function it means there's no code in it.
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;
template <class T>
class Array
{
private:
T * mData;
int mCount;
public:
Array();
~Array();
void add(T value);
void insert(T value, int index);
bool isEmpty();
void display();
bool remove(T value);
bool removeAt(int index);
int size();
T & operator[](const int index);
};
// Constructors / Destructors
// --------------------------------------------------------
template <class T>
Array<T>::Array()
{
mCount = 0;
mData = new T[mCount];
for (int i = 0; i < mCount; i++)
mData[i] = 0;
}
template <class T>
Array<T>::~Array()
{
delete[] mData;
}
// General Operations
// --------------------------------------------------------
template <class T>
void Array<T>::add(T value)
{
T * tmp = new T[mCount];
for (int i = 0; i < mCount; i++)
{
tmp[i] = mData[i];
}
mCount++;
delete[] mData;
mData = tmp;
mData[mCount - 1] = value;
}
template <class T>
void Array<T>::display()
{
if (isEmpty())
{
cout
<< "The array is empty."
<< "\n\n";
return;
}
cout << "(";
for (int i = 0; i < mCount; i++)
{
cout << mData[i];
if (i < mCount - 1)
cout << ", ";
}
cout << ")" << "\n\n";
}
template <class T>
bool Array<T>::isEmpty()
{
return mCount == 0;
}
template <class T>
int Array<T>::size()
{
return mCount;
}
// Operator Overloads
// --------------------------------------------------------
template <class T>
T & Array<T>::operator[](const int index)
{
return mData[index];
}
#endif
If you need any additional info lemme know and I can post it.
Assuming mCount keeps the number of elements in the array, then when adding a new element you really have to allocate at least mCount + 1 elements (assuming of course you want to keep all the old ones and the new one) via:
T * tmp = new T[mCount + 1];
as opposed to:
T * tmp = new T[mCount];
If it's for anything else other than educational purposes, please use std::vector instead. For example your add function is not exception safe.
Related
I'm having problem understanding where my memory leak is located in my project.
The template I have built looks like this:
#pragma once
#include "IHeap.h"
#include <iostream>
using namespace std;
template <typename T>
class dHeap
{
public:
dHeap(T size);
dHeap(T size, int nr);
dHeap(const dHeap &original);
~dHeap();
dHeap<T>& operator=(const dHeap<T> &original);
void deepCopy(const dHeap &original);
void push(const T &item);
T pop();
T peek()const;
int size()const;
int getdValue()const;
void printAll()const;
void heapify(int arr[], int size, int root);
void heapSort(int arr[], int size);
private:
//T nr;
T *arrHeap;
T nrOfItems;
T capacity;
T dValue;
void expandHeap();
};
template<typename T>
inline dHeap<T>::dHeap(T size)
{
capacity = size;
arrHeap = new T[capacity + 1];
nrOfItems = 0;
dValue = size;
}
template<typename T>
inline dHeap<T>::dHeap(T size, int nr)
{
capacity = size;
arrHeap = new T[nr];
nrOfItems = 0;
dValue = size;
}
template<typename T>
inline dHeap<T>::dHeap(const dHeap &original)
{
this->deepCopy(original);
}
template<typename T>
inline dHeap<T>::~dHeap()
{
delete[] arrHeap;
}
template<typename T>
inline dHeap<T>& dHeap<T>::operator=(const dHeap<T>& original)
{
if (this != &original)
{
this->deepCopy(original);
}
return *this;
}
template<typename T>
inline void dHeap<T>::expandHeap()
{
capacity *= 2;
T *temp = new T[capacity];
for (int i = 0; i < nrOfItems; i++)
{
temp[i] = arrHeap[i];
}
delete[] arrHeap;
arrHeap = temp;
}
template<typename T>
inline void dHeap<T>::deepCopy(const dHeap &original)
{
capacity = original.capacity;
nrOfItems = original.nrOfItems;
arrHeap = new T[capacity];
dValue = original.dValue;
for (int i = 0; i < original.nrOfItems; i++)
{
this->arrHeap[i] = original.arrHeap[i];
}
}
template<typename T>
inline void dHeap<T>::push(const T &item)
{
if (nrOfItems >= capacity)
{
expandHeap();
}
arrHeap[nrOfItems] = item;
nrOfItems++;
}
template<typename T>
inline T dHeap<T>::pop()
{
int removed = arrHeap[0];
arrHeap[0] = arrHeap[nrOfItems - 1];
nrOfItems--;
return removed;
}
template<typename T>
inline T dHeap<T>::peek() const
{
return arrHeap[0];
}
template<typename T>
inline int dHeap<T>::size() const
{
return this->nrOfItems;
}
template<typename T>
inline int dHeap<T>::getdValue() const
{
return this->dValue;
}
template<typename T>
inline void dHeap<T>::printAll() const
{
for (int i = 0; i < nrOfItems; i++)
{
cout << "Heap element " << i << ". " << arrHeap[i] << endl;
}
}
template<typename T>
inline void dHeap<T>::heapSort(int arr[], int size)
{
for (int j = 0; j < size; j++)
{
// Build heap - which means rearrange array
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(arrHeap, size, i);
}
for (int i = size - 1; i >= 0; i--)
{
swap(arrHeap[0], arrHeap[i]);
heapify(arrHeap, i, 0);
}
//when re-structured heap, use pop and re-do it again until done
arr[j] = pop();
}
}
template<typename T>
inline void dHeap<T>::heapify(int arr[], int n, int root)
{
int largest = root;
int leftChild = 2 * root + 1;
int rightChild = 2 * root + 2;
// If left child is larger than root
if (leftChild < n && arr[leftChild] > arr[largest])
{
largest = leftChild;
}
// If right child is larger than largest so far
if (rightChild < n && arr[rightChild] > arr[largest])
{
largest = rightChild;
}
// If largest is not root, heapify recursivly until done
if (largest != root)
{
swap(arr[root], arr[largest]);
heapify(arr, n, largest);
}
}
I have a pointer called heapArr which I use to build up a heap. When the program terminates the destructor is called and there have I put a delete[] this->heapArr declaration to remove the pointer when program is done.
And I have also added a delete[] this->heapArr in the expand function in order to free the memory before allocation the new expanded array.
I'm not sure I explained this perfectly but the problem is that I seem to miss to remove something because I get a memory leak warning when I end the program.
What have I missed?
Memory leaks in deepCopy, where you allocate new memory without de-allocating the old.
That being said, don't allocate memory yourself. A good chunk of your code is duplicating the functionality of std::vector, so use std::vector<T> instead of T*.
(If for some reason you cannot use std::vector I would recommend you implement a replacement. Divide-and-conquer by splitting memory management from the heap logic.)
I am in a data structures course right now and I am designing my own array class for use in a project. While testing my methods, the program kept giving me garbage when I attempt to return values. After using visual studio's debugger, I managed to narrow the problem down to the resize function and more specificly the resizeArray. No matter what I enter for its size, it only has one entry and I can't figure out why. I've attempted using flat values as well as the dynamic ones I would be using in the final version.
This is the class I've been writing.
#include "ArrayClass.h";
#include <iostream>
using namespace std;
template <class T>
ArrayClass<T>::ArrayClass() {
thisArray = new T[1];
length = 1;
}
template <class T>
ArrayClass<T>::ArrayClass(T element) {
thisArray = new T[1];
thisArray[0] = element;
length = 1;
currentLastElement = 0;
}
template <class T>
ArrayClass<T>::ArrayClass(T element, int x) {
thisArray = new T[x];
thisArray[0] = element;
length = x;
currentLastElement = 0;
}
template <class T>
void ArrayClass<T>::addElement(T element) {
if (currentLastElement >= (length - 1)) {
resize(length);
length++;
thisArray[currentLastElement + 1] = element;
cout << thisArray[currentLastElement + 1];
currentLastElement += 1;
}
else {
thisArray[currentLastElement + 1] = element;
currentLastElement += 1;
}
}
template <class T>
void ArrayClass<T>::addElement(T element, int x) {
T tempElement;
if (x == currentLastElement + 1) {
this.addElement(element);
}
else {
tempElement = *thisArray[x];
thisArray[x] = element;
addElement(tempElement, x + 1);
}
}
template <class T>
void ArrayClass<T>::removeElement(int x) {
delete[x] thisArray;
for (int i = 0; i < currentLastElement; i++) {
cout << thisArray[1];
thisArray[x] = thisArray[x + 1];
x++;
}
}
template <class T>
T ArrayClass<T>::accessElement(int x) {
return thisArray[x];
}
template <class T>
void ArrayClass<T>::resize(int x) {
T * resizeArray;
resizeArray = new T[x];
for(int i = 0; i < length; i++) resizeArray[i] = thisArray[i];
thisArray = resizeArray;
};
This is what I've been using to test it.
#include <iostream>
#include "ArrayClass.cpp"
using namespace std;
int main() {
ArrayClass<int> intArray(1);
intArray.addElement(5);
cout << intArray.accessElement(0);
cout << intArray.accessElement(1);
intArray.removeElement(0);
cout << intArray.accessElement(0);
return 0;
}
In ArrayClass<T>::addElement(T element), you're calling resize as
resize(length);
which doesn't extends the array. You need
resize(length + 1);
BTW1: You need to delete[] the original array in resize, otherwise it'll be memory leak. Such as,
template <class T>
void ArrayClass<T>::resize(int x) {
...
delete[] thisArray;
thisArray = resizeArray;
};
BTW2: this.addElement(element); and delete[x] thisArray; won't compile.
I'm making a function to resize an array in dynamic memory and it's not working..
here's the code:
template <class Type>
void Array<Type>::Resize(int newSize)
{
if(newSize==size)
return;
if(newSize<=0)
return;
Type *temp = new Type[newSize];
int min=(newSize>size)?size:newSize;
for(int i=0; i<min; i++)
temp[i]=elements[i];
delete []elements;
elements = temp;
temp = NULL;
}
the problem is in these two statements
delete []elements;
elements = temp;
cause when i comment them the program works properly,
but it actually doesn't do what is supposed to do..
I think the problem is something that is being destroyed when getting out of the function scope and I have to call it by reference but I can't actually cause this is a member function.
Here's the whole header file:
#include<string>
#include<iostream>
using namespace std;
#ifndef ARRAY_H
#define ARRAY_H
template <class Type>
class Array
{
public:
Array (int s);
Array (const Array& obj);
~Array ();
const Array& operator= (const Array& obj);
Type GetElement (int index) const;
void SetElement (Type ele, int index);
void Resize (int newSize);
void Print () const;
void Destroy ();
private:
int size;
Type* elements;
};
template <class Type>
Array<Type>::Array(int s)
{
if(s<0)
return;
size=s;
elements = new Type[size];
}
template <class Type>
Array<Type>::Array(const Array &obj)
{
size = obj.size;
elements = new Type[size];
for(int i=0; i<size; ++i)
elements[i]=obj.elements[i];
}
template <class Type>
Array<Type>::~Array()
{
delete [] elements;
elements = NULL;
size = 0;
}
template <class Type>
void Array<Type>::Destroy()
{
delete [] elements;
elements = NULL;
size = 0;
}
template <class Type>
const Array<Type> &Array<Type>::operator=(const Array &obj)
{
if(this != &obj)
{
size = obj.size;
if(elements != NULL)
delete [] elements;
elements = new Type[size];
for(int i=0; i<size; i++)
elements[i] = obj.elements[i];
}
return *this;
}
template <class Type>
Type Array<Type>::GetElement(int index) const
{
if(index<0 || index>=size)
cout << "Sorry, this operation can not be proceeded \n";
else
return elements[index];
}
template <class Type>
void Array<Type>::SetElement(Type ele, int index)
{
if(index<0 || index>=size){
cout << "Sorry, this operation can not be proceeded \n";
return; }
else
elements[index] = ele;
}
template <class Type>
void Array<Type>::Print() const
{
for(int i=0;i<size; ++i)
cout << elements[i] << endl;
}
template <class Type>
void Array<Type>::Resize(int newSize)
{
if(newSize==size)
return;
if(newSize<=0)
return;
Type *temp = new Type[newSize];
int min=(newSize>size)?size:newSize;
for(int i=0; i<min; i++)
temp[i]=elements[i];
delete []elements;
elements = temp;
temp = NULL;
}
#endif
You forgot to do one thing in the Resize() function, and that is to update the member size with the new size after reallocation. This will cause it to access memory beyond the end of the buffer after a resize with a smaller new dimension, e.g.:
Array<int> arr(10);
// set the 10 values
arr.Resize(5);
// here the buffer will have 5 elements, but arr.size is still 10
arr.Print(); // this will read elements 0 - 9, not 0 - 4
Couple of observations
if(newSize==size)
size does not exist
delete []elements;
elements = temp;
You are attempting to copy to an array you have just deleted, which was never created in the first place.
What is meant by "does not work" and "the program works properly,"?
I'm sorry if the following code is long, but I don't know what is relevant to the problem and what is not.
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
template <class T>
class Array;
template <typename T>
ostream& operator<< (ostream &output, Array<T> &obj);
template <class T>
class Array {
private:
int capacity;
public:
T *arr;
Array();
Array(int);
~Array();
void Erase();
T& operator[](int);
friend ostream& operator<< <>(ostream &output, Array &obj);
int GetLength();
};
/* Constructors and Destructors */
template <class T>
Array<T>::Array() {
capacity = 0;
arr = new T [capacity];
}
template <class T>
Array<T>::Array(int n) {
capacity = n;
arr = new T [capacity];
}
template <class T>
Array<T>::~Array() {
delete [] arr;
}
/* End of Constructors and Destructors */
/* Member Functions */
template <class T>
void Array<T>::Erase() {
delete [] arr;
capacity = 0;
arr = new T [capacity];
}
template <class T>
int Array<T>::GetLength() {
return capacity;
}
/* End of Member Functions */
/* Overloaded Operators */
template <class T>
T& Array<T>::operator[](int index) {
/*if (index < 0 || index >= capacity)
cout << "Index out of range!" << endl;
else */
return arr[index];
}
template <class T>
ostream& operator<<(ostream &output, Array<T> &obj) {
for (int i = 0; i < obj.capacity - 1; ++i)
output << "Array[" << i << "] = " << obj[i] << ", ";
output << "Array[" << obj.capacity - 1 << "] = " << obj[obj.capacity - 1];
return output;
}
/* End of Overloaded Operators */
/* Non-member Functions */
template <typename T>
void BubbleSort(Array<T> &t);
template <typename T>
T Sum(Array<T> &t);
template <typename T>
T Average(Array<T> &t);
template <typename T, typename M>
bool Equal(Array<T> &t, Array<M> &m);
template <typename T>
bool Equal(Array<T> &t, Array<double> &d);
/* End of Non-Member Functions */
/* Main Function */
int main()
{
int n;
double r;
cin>>r;
cin>>n;
Array<int> anArray(n);
Array<double> adArray(n);
Array<int> intArray2(n);
for (int nCount = 0; nCount < n; nCount++)
{
cin>>anArray[nCount];
adArray[nCount] = anArray[nCount] + r;
}
BubbleSort(anArray);
intArray2 = anArray;
cout<<"The arrays: "<<endl;
cout<<anArray;
cout<<endl<<"and "<<endl;
cout<<intArray2;
cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
cout<<"The arrays: "<<endl;
cout<<anArray;
cout<<endl<<"and "<<endl;
cout<<adArray;
cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
return 0;
}
/* End of Main */
/* Non-Member Function implementation */
template <typename T>
void BubbleSort(Array<T> &t) {
int j;
bool flag = true;
while (flag) {
flag = false;
for (j = 0; j < t.GetLength() - 1; ++j) {
if (t[j] > t[j + 1]) {
swap(t[j], t[j + 1]);
flag = true;
}
}
}
return;
}
template <typename T>
T Sum(Array<T> &t) {
T sum = 0;
for (int i = 0; i < t.GetLength(); ++i)
sum += t.arr[i];
return sum;
}
template <typename T>
T Average(Array<T> &t) {
return ( Sum(t) / t.GetLength() );
}
template <typename T, typename M>
bool Equal(Array<T> &t, Array<M> &m) {
if ( t.GetLength() != m.GetLength() )
return false;
bool flag = true;
for (int i = 0; i < t.GetLength(); ++i) {
if ( t.arr[i] != m.arr[i] ) {
flag = false;
break;
}
}
return flag;
}
template <typename T>
bool Equal(Array<T> &t, Array<double> &d) {
if ( t.GetLength() != d.GetLength())
return false;
bool flag = true;
for (int i = 0; i < t.GetLength(); ++i) {
if ( abs(t.arr[i] - d.arr[i]) > 0.1 ) {
flag = false;
break;
}
if ( abs(Average(t) - Average(d)) > 0.5 ) {
flag = false;
break;
}
}
return flag;
}
/* End of Non-Member Function Implementation */
When i run this program in Code Blocks, there is no error and it produces the desired output. However, when I use it on our college's browser platform for writing programs (something like CodeSculptor for C++), after the desired output, it gives the following error:
***glibc detected*** double free or corruption (fasttop)
and then proceeds with a Memory map (it's quite long, so I'm not sure if I should post it or not).
My question is, if there is a problem with the code, then why doesn't it show when I run the program in CodeBlocks? In that case, what is the problem with the code? Also, when running the program in the browser platform, the error message is after the desired output. Does this mean there is something wrong with the program termination?
Thank you for your time.
You aren't implementing the rule of three.
This line intArray2 = anArray;, as you haven't provided the copy assignment operator, will use the built-in one, which will blindly copy each member of your object.
Now two object's T *arr point to the same place, and when the destructors gets called.. well, you get the idea... UB. glibc is really your friend here.
It also caused a memory leak (the previous arr pointer was simply overwritten), but compared to the double-freeing, that's a minor issue.
So, I've defined template class and then i tried to overload some operators.
template <typename T> class Set
{
public:
Set(void);
Set(Set&);
~Set(void);
bool contains(T elem);
bool add(T elem);
bool remove(T elem);
bool add(T* tab, int size);
T* getSet();
int size();
Set<T> &operator+(Set<T> &snd);
Set<T> &operator-(Set<T> &snd);
private:
T *elements;
int numOfElem;
};
When I try to add element to the Set by add method everything works fine.
template<typename T>
bool Set<T>::add(T elem)
{
bool found = false;
for(int i =0; !found && i<numOfElem; i++){
if(elem == elements[i]) found = true;
}
if( !found ){
numOfElem++;
T* tmp = new T[numOfElem];
for(int i =0; i<numOfElem-1; i++){
tmp[i] = elements[i];
}
tmp[numOfElem-1] = elem;
delete[] elements;
elements = tmp;
}
return !found;
}
template<typename T>
bool Set<T>::add(T* myArray, int size)
{
bool result = false;
for(int i =0; i<size; i++){
add(myArray[i]);
}
return result;
}
template<typename T>
Set<T>& Set<T>::operator+(Set<T> &snd)
{
Set *temp = new Set(*this);
temp->add(snd.getSet(), snd.size());
return *temp;
}
template<typename T>
void Set<T>::operator=(Set<T> &snd)
{
numOfElem = snd.numOfElem;
elements = new T[numOfElem];
for(int i =0; i < numOfElem; i++){
elements[i] = snd.elements[i];
}
}
template<typename T>
int Set<T>::size()
{
return numOfElem;
}
template<typename T>
T* Set<T>::getSet()
{
return elements;
}
template<typename T>
Set<T>::Set()
{
numOfElem = 0;
elements = nullptr;
}
template<typename T>
Set<T>::Set(Set& old)
{
numOfElem = old.numOfElem;
elements = new T(numOfElem);
for(int i = 0; i< numOfElem; i++){
elements[i] = old.elements[i];
}
}
template<typename T>
Set<T>::~Set()
{
numOfElem = 0;
delete[] elements;
elements = nullptr;
}
But if I use + operator instead (adding two separate sets) the error occurs while trying to delete the array (15 Line). Any ideas?
int main(){
Set <char> set1, set2, set3;
char tab[] = {'a','d','f','g'} ;
set1.add(tab, 4);
char tab2[] = {'a','d','x','y','z'} ;
set2.add(tab2,5);
set3= set1+set2;
}
You have a mistake in your copy constructor:
elements = new T(numOfElem);
It should be
elements = new T[numOfElem];
By writing new T(numOfElem); you allocate only one variable with its value initialized to numOfEllem.
Use a std::vector instead of the array and you will avoid such problems.
Your code is also leaking a memory in the addition operator:
template<typename T>
Set<T>& Set<T>::operator+(Set<T> &snd)
{
Set *temp = new Set(*this);
temp->add(snd.getSet(), snd.size());
return *temp;
}
You are allocating a memory and you never delete it so if you call that function too often you program may run out of its virtual memory and will crash with the uncaught std::bad_alloc exception. Change the function to this:
template<typename T>
Set<T> Set<T>::operator+(Set<T> &snd)
{
Set temp(*this);
temp.add(snd.getSet(), snd.size());
return temp;
}