我制作了一个包含固定大小数组的头文件(17),但有些根本不正确,我找不到。
ScreenArray.h
class ScreenArray
{
public:
ScreenArray();
~ScreenArray();
unsigned long getColour(int arrayPointer);
void setColour(int arrayPointer, unsigned long pixelColour);
private:
unsigned long pixelArray[17];
};
ScreenArray.cpp
#include "PixelScreenArray.h"
ScreenArray::ScreenArray()
{
}
ScreenArray::~ScreenArray()
{
delete[] pixelArray;
}
unsigned long ScreenArray::getColour(int arrayPointer)
{
return pixelArray[arrayPointer];
}
void ScreenArray::setColour(int arrayPointer, unsigned long pixelColour)
{
pixelArray[arrayPointer] = pixelColour;
}
Source.cpp
...
ScreenArray sA;
sA.setPixel(0, 3242);
...
答案 0 :(得分:1)
如果您没有使用new[]
分配数组,则不应该delete[]
,这是错误的:
class ScreenArray
{
public:
ScreenArray();
~ScreenArray() {
delete[] pixelArray; // Nope
}
private:
unsigned long pixelArray[17];
};
该数组是该类的成员,是否在您的堆栈中分配整个对象:
ScreenArray sA;
sA.setPixel(0, 3242);
或使用new ScreenArray();
在堆上分配,您不能“只删除对象的一部分”。
在您的特定情况下,您正试图释放堆栈内存和that's UB