【发布时间】:2013-10-26 00:42:26
【问题描述】:
我正在尝试通过嵌套类为Uint8 类型数组分配内存。当我这样做时,我认为它会运行到堆错误并提示显示 Debug Assertion Error 的窗口。表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)。我的代码如下:
#include <iostream>
#include "dcmtk/dcmdata/dctk.h"
using namespace std;
class test
{
public:
int a;
Uint8 * b;
test()
{}
test(int c)
{
a=c;
b = new Uint8[a];
}
~test()
{
delete [] b;
}
};
class subTest
{
public:
test h;
subTest()
{}
subTest(test g)
{
h=g;
}
~subTest()
{
}
};
int main()
{
subTest f(test(5));
for(int i=0; i<5; i++)
{
f.h.b[i] = Uint8((i*2)+1);
cout<< int(f.h.b[i])<<endl;
}
return 0;
}
你能找出我做错了什么吗?
谢谢。
【问题讨论】:
-
从rule of three 开始。更好的是,使用
std::vector<Uint8>。 -
我的错误我忘了提到错误。问题现已更新。
-
也许该规则必须升级为四规则,使用移动构造函数...
标签: c++ arrays dynamic-arrays dynamic-allocation