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
  • 17,051
  • 45
  • 159
  • 315

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
  • 120,187
  • 20
  • 207
  • 252
4

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

Foo *data;
delete [] data;
Andrew
  • 24,218
  • 13
  • 61
  • 90
0

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

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35