Can't delete a heap instance of my custom class (C++) - c++

Here's what I've got:
class MyClass {
int holder;
public:
MyClass() {
holder = 5;
}
};
template<class T>
class First {
std::vector<T> items;
public:
First() {
T* tmp;
for (int i = 0; i < 20; i++) {
tmp = new T();
items.push_back(*tmp);
}
};
~First() {
for (int i = 0; i < 20; i++) {
delete items.at(i);
}
};
};
class Second {
std::vector<std::deque<First<MyClass>>> items;
public:
Second() {
std::deque<First<MyClass>>* tmp;
for (int i = 0; i < 10; i++) {
tmp = new std::deque<First<MyClass>>;
items.push_back(*tmp);
}
};
~Second() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < items.at(i).size(); j++) {
delete items.at(i).at(j); // this deletes the "First" instances
}
delete items.at(i); // this deletes the deque
}
};
};
In my main, I'm create an instance of Second and adding First instances to it (through methods that aren't included). At the end of main, I delete the instance of Second, which should delete all the instances of First and the deques. However, I'm getting the following errors:
error: cannot delete expression of type 'value_type' (aka 'MyClass')
error: cannot delete expression of type 'value_type' (aka 'First<MyClass>')
error: cannot delete expression of type 'value_type' (aka 'std::deque<First<MyClass>>')
Essentially, all my delete commands are throwing errors. What am I missing here? I need to manually implement a destructor because I created a bunch of stuff on the heap - correct?

You are never storing the result of the new expression. Your code should perhaps look like this:
First() {
T* tmp;
for (int i = 0; i < 20; i++) {
tmp = new T();
items.push_back(*tmp);
delete tmp; // result of "new" is still accessible here
}
}
~First() { }
Or like this:
First() {
for (int i = 0; i < 20; i++) {
items.push_back(T());
}
}
Or just like this:
First() : items(20) {}

Related

Dynamic Array resizing in C++ Doesn't work properly

i have this reSize function in my Array header
void reSize(int newsize) {
T* old = items;
size = newsize;
items = new T[newsize];
for (int i = 0;i < length;i++)
items[i] = old[i];
delete[]old;
}
and my main code:
struct User{
string name;
Array<int> data;
};
int main() {
Array<User> x(3);
x.get(0).name = "Kmal";
x.get(0).data.push_back(2); x.get(0).data.push_back(3);
x.reSize(10);
cout << x.get(0).data.get(0) <<endl;
return 0;
}
the problem is after resizing, my values that were stored in "data" variable are gone.
when i commented the code.
//delete[] old
in the reSize function
it worked fine...so i guess the problem is when i delete the pointer it deletes also the pointer inside the struct object which i don't want it to happen..
i don't want to comment the command becuz a leak in the memory will happen...how to fix this problem ?.
Update: My Array Class .
#include <iostream>
using namespace std;
template <class T>
class Array {
private :
T* items;
int size;
int length;
public :
Array() {
this->size = 0;
items = new T[this->size];
length = 0;
}
Array(int size) {
this->size = size;
items = new T[this->size];
length = 0;
}
int getsize() {
return this->size;
}
template <class T> void push_back(T x) {
if ((length+1) <= size) {
items[length] = x;
length++;
}
else {
this->reSize(size+1);
items[length] = x;
length++;
}
}
template <class T> void Insert(int index, T x) {
if (length + 1 <= size) {
for (int i = length;i > index;i--) {
items[i] = items[i - 1];
}
items[index] = x;
length++;
}
else {
this->reSize(size+1);
for (int i = length;i > index;i--) {
items[i] = items[i - 1];
}
items[length] = x;
length++;
}
}
template <class T> int Find(T x) {
int index = -1;
for (int i = 0;i < length;i++) {
if (items[i] ==x) {
index = i;
break;
}
}
return index;
}
void remove(int index) {
items[index] = "";
if(index+1 < length)
for (int i = index;i < length-1;i++) {
items[i] = items[i + 1];
items[i + 1] = "";
}
length--;
}
void reSize(int newsize) {
T* old = items;
size = newsize;
items = new T[newsize];
for (int i = 0;i < length;i++)
items[i] = old[i];
delete[]old;
}
void Merge(Array<T> x){
T* old = items; int oldlength = length;
items = new T[size + x.size];
size = size + x.size;
length += x.length;
for (int i = 0;i < length;i++) {
if(i< oldlength)
items[i] = old[i];
else
items[i] = x.items[i-oldlength];
}
delete[] old;
}
T& get(int index) {
return items[index];
}
}
struct User{
string name;
Array<int> data;
};
int main() {
Array<User> x(3);
// this line causes some problems
x.get(0).name = "Kmal";
x.get(0).data.push_back(2); x.get(0).data.push_back(3);
x.reSize(10);
cout << x.get(0).data.get(0) <<endl;
return 0;
}
In your code, declaring Array<User> x(3) declares an empty array with 3 elements that are preallocated. The length property of the array is 0. When the array is copied, length(0) elements are copied over into the resized storage. When you access the 0th element, it won't be copied on resize. What you actually need to do is call push_back() to add an element to the array so that length becomes 1 and the element is copied on resize.
Also, your array class is lacking a proper copy constructor and move constructor, which means copying it won't work at all. This means that User cannot be copied properly since it contains an array, which means that resizing an array of User won't work. You need to implement a copy constructor and copy assignment operator to be able to copy the array. You also need a destructor since, right now, the array is leaking memory when it goes out of scope.

I can't add a new element to dynamic array

There is something wrong with the expand() method.
#include <iostream>
struct obj
{
int fInt;
float fFloat;
};
template <typename T>
class dynamicArray {
private:
T* myArray;
int elements;
int size;
public:
dynamicArray();
void add(T dane);
void expand();
void init(int el);
T get(int index);
};
template <typename T>
dynamicArray<T>::dynamicArray()
{
this->size = 1;
this->elements = 0;
this->myArray = new T[this->size];
init(this->elements);
}
template <typename T>
T dynamicArray<T>::get(int index)
{
return this->myArray[index];
}
template <typename T>
void dynamicArray<T>::init(int el)
{
for (size_t i = el; i < this->size; i++)
{
this->myArray[this->elements] = nullptr;
}
}
template <typename T>
void dynamicArray<T>::expand()
{
this->size *= 2;
T* tempArr = new T[this->size];
for (int i = 0; i < this->elements; i++)
{
tempArr[i] = this->myArray[i];
//tempArr[i] = new T(*this->myArray[i]);
}
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
delete this->myArray;
this->myArray = tempArr;
init(this->elements);
}
template <typename T>
void dynamicArray<T>::add(T dane)
{
if (this->size == this->elements)
this->expand();
this->myArray[this->elements] = dane;
this->elements++;
}
int main()
{
dynamicArray<obj*>* arr = new dynamicArray<obj*>();
obj* so = new obj;
so->fInt = 2;
so->fFloat = 2;
arr->add(so);
obj* so2 = new obj;
so2->fInt = 3;
so2->fFloat = 3;
arr->add(so2);
so = arr->get(0);
so2 = arr->get(1);
std::cout << so->fInt << std::endl;
std::cout << so->fInt;
}
In this for loop I would like to assign to temporary array elements of myArray but they are not the copies
for (int i = 0; i < this->elements; i++)
{
tempArr[i] = this->myArray[i];
//tempArr[i] = new T(*this->myArray[i]);
}
and when I delete them they disappear from tempArr too.
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
I tried couple things but I can't find the solution.
tempArr[i] = new T(*this->myArray[i]);
I am not sure if this is the right track, but it's giving me a
'initializing': cannot convert from 'obj' to 'T'
and
'=' cannot convert from 'T*' to 'T'
You got yourself confused, you have a pointer to an array of T, not a pointer to an array of T*, but some of your code is written as if you had the latter.
This (in expand)
for (int i = 0; i < this->elements; i++)
{
delete this->myArray[i];
}
delete this->myArray;
should simply be
delete[] this->myArray;
You can't delete individual array elements because they are not (necessarily) pointers. And delete[] this->myArray; will invoke the destructor for all elements in your array.
And init can simply be deleted, because again it assumes that your array elements are pointers.
Try writing some code with dynamicArray<int>, so that your T is definitely not a pointer. That will find all the places where you've incorrectly assumed that T is a pointer (in case I've missed any).

Still having an Unhandled Exception after editing it a bit

Inventory::Inventory()
{
this->cap = 10;
this->nrOfItems = 0;
this->itemArr = new Item* [cap]();
}
Inventory::~Inventory()
{
for (size_t i = 0; i < this->nrOfItems; i++)
{
delete this->itemArr[i];
}
delete[] this->itemArr;
}
void Inventory::expand()
{
this->cap *= 2;
Item **tempArr = new Item*[this->cap]();
for (size_t i = 0; i < this->nrOfItems, i++;)
{
tempArr[i] = new Item(*this->itemArr[i]);
}
for (size_t i = 0; i < this->nrOfItems, i++;)
{
delete this->itemArr[i];
}
delete[] this->itemArr;
this->itemArr = tempArr;
this->initialize(this->nrOfItems);
}
void Inventory::initialize(const int from)
{
for (size_t i = from; i < cap, i++;)
{
this->itemArr[i] = nullptr;
}
}
void Inventory::addItem(const Item& item)
{
if (this->nrOfItems >= this->cap)
{
expand();
}
this->itemArr[this->nrOfItems++] = new Item(item);
}
void Inventory::removeItem(int index)
{
}
Above is my code from Inventory.cpp and the issue is that I keep getting an Unhandled Exception from the line that has:
this->itemArr[i] = nullptr;
I have no idea where I'm messing up in the code. Below I am posting from Inventory.h:
class Inventory
{
private:
int cap;
int nrOfItems;
Item **itemArr;
void expand();
void initialize(const int from);
public:
Inventory();
~Inventory();
void addItem(const Item &item);
void removeItem(int index);
inline void debugPrint() const
{
for (size_t i = 0; i < this->nrOfItems; i++)
{
std::cout << this->itemArr[i]->debugPrint() << std::endl;
}
}
};
This is where itemArr should be housed but for some reason, it is not pulling. I am new to coding so I don't know all of the tips and tricks involved with it.
In the loop i < cap, i++;, you will first check if i is in bounds, and then increase it by one before the loop body executes. Thus if i=cap-1 the check will pass, i will become cap and you're out of bounds.
Instead write (size_t i = from; i < cap;i++), so that the increment of i is executed after the loop body.
A see several problems with your code:
In Inventory::expand(), since you are working with an array of pointers (why not an array of objects?), there is no need to make clones of the existing Item objects to the new array, just copy the existing pointers as-is. You are just expanding the array to fit more pointers, so the clones are wasted overhead. Not a fatal issue, but it is something you should be aware of.
In both Inventory::initialize() and Inventory::expand(), your loops are setup incorrectly. You are performing the i++ in the wrong place. Given this definition of a loop:
for ( <init-statement> <condition> ; <iteration_expression> )
You are performing the i++ as part of the <condition>, not the <iteration_expression>. A simply typo caused by you using the comma operator instead of ;, but an important typo nonetheless.
Inventory is violating the Rule of 3/5/0, by not implementing copy/move constructors and copy.move assignment operators.
With that said, try this instead:
class Inventory
{
private:
size_t cap;
size_t nrOfItems;
Item **itemArr;
void expand();
public:
Inventory();
Inventory(const Inventory &src);
Inventory(Inventory &&src);
~Inventory();
Inventory& operator=(Inventory rhs);
void addItem(const Item &item);
void removeItem(size_t index);
inline void debugPrint() const
{
for (size_t i = 0; i < nrOfItems; ++i)
{
std::cout << itemArr[i]->debugPrint() << std::endl;
}
}
};
Inventory::Inventory()
{
cap = 10;
nrOfItems = 0;
itemArr = new Item*[cap]();
}
Inventory::Inventory(const Inventory &src)
{
cap = src.cap;
nrOfItems = src.nrOfItems;
itemArr = new Item*[cap]();
for(size_t i = 0; i < nrOfItems; ++i)
{
itemArr[i] = new Item(*(src.itemArr[i]));
}
}
Inventory::Inventory(Inventory &&src)
{
cap = src.cap; src.cap = 0;
nrOfItems = src.nrOfItems; src.nrOfItems = 0;
itemArr = src.itemArr; src.itemArr = nullptr;
}
Inventory::~Inventory()
{
for (size_t i = 0; i < nrOfItems; ++i)
{
delete itemArr[i];
}
delete[] itemArr;
}
Inventory& Inventory::operator=(Inventory rhs)
{
Inventory temp(std::move(rhs));
std::swap(cap, temp.cap);
std::swap(nrOfItems, temp.nrOfItems);
std::swap(itemArr, temp.itemArr);
return *this;
}
void Inventory::expand()
{
size_t newCap = cap * 2;
Item **tempArr = new Item*[newCap]();
for (size_t i = 0; i < nrOfItems; ++i)
{
tempArr[i] = itemArr[i];
}
delete[] itemArr;
itemArr = tempArr;
cap = newCap;
}
void Inventory::addItem(const Item& item)
{
if (nrOfItems >= cap)
{
expand();
}
itemArr[nrOfItems] = new Item(item);
++nrOfItems;
}
void Inventory::removeItem(size_t index)
{
if (index < nrOfItems)
{
Item *item = itemArr[index];
for(size_t i = index + 1; i < nrOfItems; ++i)
{
itemArr[i-1] = itemArr[i];
}
--nrOfItems;
itemArr[nrOfItems] = nullptr;
delete item;
}
}
That being said, you really should be using std::vector instead, let it handle these details for you, eg:
#include <vector>
class Inventory
{
private:
std::vector<Item> itemVec;
public:
Inventory();
void addItem(const Item &item);
void removeItem(size_t index);
inline void debugPrint() const
{
for (Item &item : items)
{
std::cout << item.debugPrint() << std::endl;
}
}
};
Inventory::Inventory()
{
itemVec.reserve(10);
}
void Inventory::addItem(const Item& item)
{
itemVec.emplace_back(item);
}
void Inventory::removeItem(size_t index)
{
if (index < itemVec.size())
{
itemVec.erase(itemVec.begin() + index);
}
}
See how much simpler that is? :)

Something is causing heap corruption when calling delete[] but I have dimensions of array set up correctly

I've found out that heap corruption error on delete[] is caused by writing somewhere where I didn't allocate anything (according to these posts: First post, Second post), the problem is though that even after trying to find the issue in this case, I am still unable to find it.
I am to create a simple database table where as a parameter of the function I get a row with data that has to correspond to the column data type.
So basically I created an array of pointers on other arrays (2d array) so that each array is row.
This is declaration for Table class:
class DLL_SPEC Table {
public:
void insert(Object** row);
void remove(int rowid);
Iterator* select();
void commit();
void close();
int getRowCount() const;
FieldObject** getFields() const;
int getFieldCount() const;
void setName(std::string name) { _nameOfTable = name; };
void setNumberOfFields(int numberOfFields) { _numberOfFields = numberOfFields; }
void setNumberOfRows(int numberOfRows) { _numberOfRows = numberOfRows; }
void setFields(FieldObject** fields) { _fields = fields; }
Iterator* select(Condition* condition) { throw 0; }
int findRowId(Condition* condition) { throw 0; }
void update(Condition* condition, std::function<void(Object**)> callback) { throw 0; }
private:
std::string _nameOfTable;
int _numberOfFields;
int _numberOfRows;
FieldObject** _fields;
Object*** _rowValues;
};
and here is the method for inserting that is causing the heap corruption error at delete[] tmpArray.
void Table::insert(Object** row)
{
Object*** tmpArray = new Object**[_numberOfRows + 1];
Object** checkedRow = new Object*[_numberOfFields];
// extend the array by 1 new row
for (size_t i = 0; i < _numberOfRows; i++)
{
tmpArray[i] = new Object*[_numberOfFields];
}
for (size_t i = 0; i < _numberOfRows; i++)
{
for (size_t j = 0; j < _numberOfFields; j++)
{
tmpArray[i][j] = _rowValues[i][j];
}
}
// check if the new row contains correct values for table
for (size_t i = 0; i < _numberOfFields; i++)
{
if (StringObject* v = dynamic_cast<StringObject*>(row[i]))
{
if (v->isType(_fields[i]->getType()))
{
checkedRow[i] = v;
}
continue;
}
if (IntObject* v = dynamic_cast<IntObject*>(row[i]))
{
if (v->isType(_fields[i]->getType()))
{
checkedRow[i] = v;
}
continue;
}
if (DoubleObject* v = dynamic_cast<DoubleObject*>(row[i]))
{
if (v->isType(_fields[i]->getType()))
{
checkedRow[i] = v;
}
continue;
}
throw std::invalid_argument("The fields of this table don't match with the data you want to enter.");
}
tmpArray[_numberOfRows + 1] = checkedRow;
_numberOfRows++;
_rowValues = tmpArray;
delete[] tmpArray;
delete[] checkedRow;
}
You allocate some space for tmpArray with
Object*** tmpArray = new Object**[_numberOfRows + 1];
at the end of the function you write to
tmpArray[_numberOfRows + 1] = checkedRow;
which writes past the end of the allocated space (subscripts run from 0 to _numberOfRows) resulting in Undefined Behavior.
Also, even if it didn't crash here, it would later because you assign tmpArray to _rowValues then delete tmpArray, leaving _rowValues dangling resulting in future issues when you dereference that pointer.

templated placement new and destructor

why that does not compile?
template <typename T>
class Pool{
char Buff[sizeof(T)*256];
public:
Pool(){
T* item = reinterpret_cast<T*>(&Buff[0]);
for(int i =0 ; i<256;i++)
item[i] = new(&item[i]) T();
}
~Pool(){
T* item = reinterpret_cast<T*>(&Buff[0]);
for(int i =0 ; i<256;i++)
item[i] -> ~ T();
}
void reset(unsigned int i){
T* item = reinterpret_cast<T*>(&Buff[0]);
item[i]->~T();
item[i]->T();
}
}
What i obviously want to achieve is calling placement new on a raw memory array (that should call constructor ok). Then I want to call destructor and constructor of items in the array. The problem is that Item is template and so if I use
Pool<FooBar>
the compiler expect to find "FooBar()" and "~FooBar()" instead of "T()" and "~T()".
is there any particular syntax to do that?
I'm using C++03 not C++11
Your syntax isn't quite right. The following should do the trick:
Pool() {
T* item = reinterpret_cast<T*>(&Buff[0]);
for(int i = 0; i < 256; i++)
new(&item[i]) T();
}
~Pool() {
T* item = reinterpret_cast<T*>(&Buff[0]);
for (int i = 0; i < 256; i++)
item[i].~T();
}
void reset(unsigned int i) {
T* item = reinterpret_cast<T*>(&Buff[0]);
item[i].~T();
new(&item[i]) T();
}