Removing a pointer to a class that creates an array

Basically, I have a class A that creates an array when built.

class A
{
public:
A (float height, float width, BYTE* data)
{
    setsize(height,width);
    mydata = CreateNewBuffer(sizes);
    setdata(data);     
}


~A() 
{

}
void setsize(float height, float width)
{
    sizes.cx = width;
    sizes.cy = height;
}

BYTE* CreateNewBuffer (SIZE  sImage)
{
    return new BYTE [sImage.cx * sImage.cy];
}

void setdata(BYTE* data)
{
    memcpy(threatdata,data,sImage.cx * sImage.cy);
}

}

I am creating a pointer to this class in a larger class:

A* mypointer;

which I initialize in the 3rd class after passing through the function:

3rdClass::instance()->myfunction(mypointer)

and inside the myfunction () function, I install a bool indicating that the class was built

mypointer = new A(height,width,data);
wasconstructed = true;

Now, the next time I go to the function pointer myfunction(), I will check if the class has already been constructed, if it was, I want to delete it so that it creates a new one without losing memory.

What is the correct way to do this:

I tried basic things like <

if (3rdClass::instance()->checkifconstructed()){
    delete mypointer; //(or even delete [] mypointer but then i get heap corruption)
    mypointer = NULL;
}

But that does not work.

+3
source share
2 answers

, mydata ( , mydata 7- ?)

, . , , : . - :

~A() 
{
  delete [] mydata;
}

, delete mypointer, , .

+4

new [] , [] .

, , . ?

+1

Source: https://habr.com/ru/post/1796386/


All Articles