在C ++中,我可以保留一些内存,然后删除这个内存,如:
float *myFloat;
myFloat = new float[10];
delete myFloat; --> Works fine
但是,如果类型不是float *
而是MTransformationMatrix *
(Maya类型),那么我无法删除:
MTransformationMatrix *myTransformationMatrixes;
myTransformationMatrixes = new MTransformationMatrix[10];
delete myTransformationMatrixes; --> Crash
为了释放记忆,我需要为特殊类型做些什么吗?
答案 0 :(得分:3)
这两个分配的对象都是数组,您应该使用delete[]
语法删除它们:
float *myFloat = new float[10];
delete[] myFloat;
MTransformationMatrix *myTransformationMatrixes;
myTransformationMatrixes = new MTransformationMatrix[10];
delete[] myTransformationMatrixes;
你的两个例子都会调用未定义的行为,你很幸运,第一个没有引起明显的伤害。
答案 1 :(得分:1)
您应该使用delete[]
运算符删除数组。
仅在分配单个对象时使用delete
,而不是数组。
当您使用错误的时,它将导致未定义的行为。