I have class cAuthorisation that manages array of strAuthorisation
function resize prepares aray for next record.
During resize , on line delete [] data; I have crash.
struct strAuthorisation
{
double Amount;
}Authorisation;
class cAuthorisation {
public:
int size;
strAuthorisation *data;
cAuthorisation ()
{
size=0;
}
~cAuthorisation()
{
};
void Add(strAuthorisation )
{
resize();
data[size]=*value;
}
void resize()
{
strAuthorisation *a = new strAuthorisation[5];
size_t newSize = size + 1 ;
strAuthorisation *newArr = new strAuthorisation[newSize];
memcpy( newArr, data, size * sizeof(strAuthorisation) );
size = newSize;
delete [] data;
data = newArr;
}
} Authorisations;
Why it is not possible delete class array?
It crashes because data is an unitialised pointer. delete[] on a null pointer is safe (no-op) so initialise data in the constructor:
cAuthorisation() : size(0), data(0) {}
Note that the class as it stands is violating the rule of three.
Unsure if this is a learning exercise, if it is not use std::vector<strAuthorisation>. The std::vector will dynamically grow as required and automatically destructed when it goes out of scope.
You have not initialized the data pointer. So you are making something like this:
Foo *data;
delete [] data;
"data" is a pointer who's value was never initialized.
Related
So I am trying to resize an array by making a function call ResizeArray(). However, I don't know what is the correct way to use "delete" in this case. (I make a new int * and copy the value from the original to it and then I make the original pointer points to the new one, now I don't know what to "delete"
class Base
{
private:
int sizeInClass;
int *arrayy=nullptr;
public:
Base(int s)
{
sizeInClass=s;
arrayy = new int[s]{};
setValue();
};
void setValue()
{
for(int x=0;x<sizeInClass;x++)
{
arrayy[x]=x;
}
}
void print()
{
int countter=0;
for(int x=0;x<sizeInClass;x++)
{
countter++;
cout<<arrayy[x]<<endl;
}
cout<<"The size of the array is : "<<countter<<endl;
}
void ResizeArray(int newSize)
{
int *newArray = nullptr;
newArray = new int[newSize];
for(int x=0;x<sizeInClass;x++)
{
newArray[x]=arrayy[x];
}
delete [] arrayy; /////////////////////////////////// should i use deleate here ?
arrayy = newArray;
delete [] newArray; /////////////////////////////////// or should I use deleate here ?
sizeInClass = newSize;
}
~Base()
{
delete [] arrayy; /////////////////////////////////// or just use delete here
arrayy=nullptr;
}
};
int main()
{
Base b(5);
b.print();
b.ResizeArray(8);
b.setValue();
b.print();
return 0;
}
The first and the 3rd of your suggested delete are correct.
Regarding handling resources,
for sure you need de-allocation in destructor, to free resources when your container class
is destroyed. When you want to resize contained array, you are handling it in ResizeArray function, so below is basic proposal for it, with clarification comments:
void ResizeArray(int newSize)
{
int *newArray = new int[newSize];
if (nullptr != newArray) { // we take action only if allocation was successful
for(int x=0;x<sizeInClass;x++)
{
newArray[x]=arrayy[x];
}
delete [] arrayy; // good, here you delete/free resources allocate previously for an old array
arrayy = newArray; // good, you redirect member ptr to newly allocated memory
/* delete [] newArray; ups, we have member ptr point to this location
and we cannot delete it, after this, accessing it would be UB,
beside in dtor we would have double, second deletion */
sizeInClass = newSize;
}
}
Your destructor is fine.
There could be further improvements in your code, but this is related to your question.
I have buggy behaviour in C++ code that seems to be caused by the incorrect freeing of dynamically created structs. The structs are in the form:
typedef struct
{
char *value;
} Element;
typedef struct
{
int num;
Element **elements;
int *index;
} Container;
They are created like this:
Element *new_element(int size)
{
Element *elem = new Element;
elem->value = new char[size];
return elem;
}
Container *new_container(int num)
{
Container *cont = new Container;
cont->num = num;
cont->elements = new Element*[num];
cont->index = new int[num];
}
What is the correct way to free these?
You can easily fix all freeing problems by outsourcing resource management to the compiler:
#include <cstdlib>
#include <string>
#include <vector>
struct Element {
Element() = default;
explicit Element(std::size_t size) : value(size, '\0') { }
std::string value;
};
struct Container {
explicit Container(std::size_t size) : elements(size), index(size) { }
std::vector<Element> elements;
std::vector<int> index;
};
For all your future resource management problems, see The Definitive C++ Book Guide and List.
You should be doing this a sensible way. As others have pointed out, it can be made automatic. Failing that you should clean up in destructors. Failing that, however, and to answer the actual question, the syntax for deleting a new array is:
int x = new x[20];
delete [] x;
You will of course have to delete everything you new, so you will need to loop through the elements deleting each one in turn before deleting the elements array itself. You'd think someone would have wrapped that up in a convenience type cough vector cough.
i have a template array consist Cells that holds the data, described in the code:
template <class T>
class Array
{
private:
//the array is consist cellss that holds the data
template<class S>
class Cell
{
public:
//members:
S* m_data;
//methods:
//C'tor:(inline)
Cell(S* data=NULL): m_data(data){};
//D'tor:(inline)
~Cell(){delete m_data;};
//C.C'tor:(inlnie)
Cell(const Cell<S>& cell): m_data(cell.m_data){};
};
private:
//members of Array:
Cell<T>* m_head,*m_last;
unsigned int m_size;
public:
/*******************C'tors and D'tors************************/
//C'tor:(inline)
Array():m_head(NULL),m_last(NULL), m_size(0){};
//D'tor:
~Array(){delete[] m_head;};
//C.C'tor:
Array(const Array& array): m_head(array.m_head),m_last(array.m_last),m_size(array.m_size){};
/****************Adding********************/
//add an element to the end of the Array:
void add(const T added);
/*******************Returning and deleting***********************/
T& operator[](const unsigned int index)const {return *(m_head[index].m_data);};
//delete the last element:
void remove();
/*********************Size*****************************/
//returning the number of elements:(inline)
const unsigned int size()const{return m_size;};
//check if the Array is empty:
bool isEmpty()const {return (m_size==0);};
};
now this is the implementaion of add:(after tests look like it works fine but just for case i write it here also)
template <class T>void Array<T>::add(const T added)
{
//allocating memory for the new array:
Cell<T>* newarray=new Cell<T>[m_size+1];
//copy all the elements from the old array:
unsigned int i;
for (i=0; i<m_size;i++)
newarray[i].m_data=m_head[i].m_data;
//put the added in the last index:
T* newelement= new T(added);
newarray[i].m_data=newelement;
//change the ptrs:
m_head=newarray;
m_last=&newarray[m_size];
//increase the counter:
m_size++;
}
and this is the implementaion of remove:
template <class T>void Array<T>::remove()
{
//if there is only 1 element:
if(m_size==1)
{
delete[] m_head;
m_head=m_last=NULL;
}
//change the last the previus cell
else
{
delete m_last;
m_last=m_last-1;
}
//and decrease the counter:
m_size--;
}
now when do:
Array<int> a;
a.add(3);//work fine
a.add(4);//work fine
a.remove();//fail
i get a runtime error from the line delete m_last; even though m_last point to a cell that actually hold data (m_last point to a cell holds 4).
what am i missing here? why cant i delete a pointer to a cell in the Array?
the error VS2012 give me:_BLOCK_TYPE_IS_VAILED(pHead->nBlockUse)
another important thing i forgot to say: when debug it doesnt enter the D'tor of Cell at all, it just get out when going to the delete.
You can't delete one element of array.
int *x = new int[10];
delete &x[2] ; // It is incorrect!
You can only delete whole of array:
delete [] x;
The destructor ~Cell calls delete. That's a sure sign that the constructor should call new.
I want to deep copy an array of int. I get an Assertion Error: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) when it goes through the destructor. Which I've been told is because It's trying to delete something that is not there. Please let me know if I am on the right track and just need to change something small, or if I am completely lost and don't know it. I can add more code if needed.
Thanks for the answers.
.h
private:
int* _myArray;
int _size;
int _capacity;
.cpp
MyVector::MyVector()
{
_myArray = new int[2];
_size = 0;
_capacity = 2;
}
MyVector::MyVector(int aSize)
{
_myArray = new int[aSize];
_size = 0;
_capacity = aSize;
}
MyVector::~MyVector()
{
if(_myArray != NULL)
{
delete[] _myArray;
_myArray = NULL;
}
}
MyVector::MyVector(const MyVector& mVector)
{
_capacity = mVector._capacity;
_size = mVector._size;
// if(mVector._myArray)
// {
// _myArray = new int[_capacity];
// copy(mVector._myArray, mVector._myArray+_capacity, _myArray);
// }
}
MyVector& MyVector::operator=(MyVector& setterVect)
{
delete [] _myArray;
if(setterVect._myArray)
{
_myArray = new int[_capacity];
copy(setterVect._myArray, setterVect._myArray+_capacity, _myArray);
}
return *this;
}
You need to make sure you are following the "Rule of Three".
Apart from copy constructor & destructor You should also provide a copy assignment operator which should do a deep copy of dynamically allocated pointer member.
On a side note, the best solution is to simply drop the dynamically allocated member and use a std::vector it saves you all the hassles of manual memory management.
Hey I'm having trouble figuring out how to get my template header to work. I have to get my init constructor to take in an array and reverse it. So for example if I have [1,2,3,4] it takes it in [4,3,2,1]
this is my template class:
#pragma once
#include <iostream>
using namespace std;
template<typename DATA_TYPE>
class Reverser
{
private:
// Not sure to make this DATA_TYPE* or just DATA_TYPE
DATA_TYPE Data;
public:
// Init constructor
Reverser(const DATA_TYPE& input, const int & size)
{
// This is where I'm getting my error saying it's a conversion error (int* = int), not sure
// What to make Data then in the private section.
Data = new DATA_TYPE[size];
for(int i=size-1; i>=0; i--)
Data[(size-1)-i] = input[i];
}
DATA_TYPE GetReverse(){
return Data;
}
~Reverser(){
delete[] Data;
}
};
So yea if you could tell me what I'm doing wrong that'd be great.
That's because when you pass array to function it converts to pointer. You must use DATA_TYPE as pointer:
template<typename DATA_TYPE>
class Reverser
{
private:
// Not sure to make this DATA_TYPE* or just DATA_TYPE
DATA_TYPE* Data; //pointer
public:
// Init constructor
Reverser(const DATA_TYPE* input, const int & size) //pointer
{
// This is where I'm getting my error saying it's a conversion error (int* = int), not sure
// What to make Data then in the private section.
Data = new DATA_TYPE[size];
for(int i=size-1; i>=0; i--)
Data[(size-1)-i] = input[i];
}
DATA_TYPE* GetReverse(){ //Returns Pointer
return Data;
}
~Reverser(){
delete[] Data;
}
};
It seems to me like you are declaring an instance of this class with int, like
Reverser<int> myVar;
Then the Data member will be of type int. In the constructor you then try to allocate memory (with new returning a int*) and assign it to the Data member, but you can't assign a pointer to a non-pointer.
So as you write in your comment, it should be
DATA_TYPE* Data;