0

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?

vico
  • 15,367
  • 39
  • 141
  • 262

3 Answers3

7

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.

Community
  • 1
  • 1
hmjd
  • 117,013
  • 19
  • 199
  • 247
4

You have not initialized the data pointer. So you are making something like this:

Foo *data;
delete [] data;
Andrew
  • 23,640
  • 12
  • 59
  • 90
0

"data" is a pointer who's value was never initialized.

Torsten Robitzki
  • 2,979
  • 1
  • 20
  • 34