问题描述
如何仅使用Qt-Objects创建3D阵列?该数组应为3D整数数组.我试图在堆上创建一个标准的3D数组.在堆上分配内存可以正常工作.如果我想重新分配内存,则会出错.
How can I create a 3D-array only with Qt-Objects? The array should be a 3D-integer-array. I have tried to create a standard 3D-array on the heap. To allocate the memory on the heap works fine. I got an error if i want to deallocate the memory.
const int scalefaktor = 16;
int*** anzPixel3d = new int**[256/scalefaktor];
for (int i = 0; i <= 256/scalefaktor ; i++)
{
anzPixel3d[i] = new int*[256/scalefaktor];
for (int k = 0; k <= 256/scalefaktor ; k++)
{
anzPixel3d[i][k] = new int[256/scalefaktor];
}
}
for (int j = 0; j <= 256/scalefaktor ; j++)
{
for (int m = 0; m <= 256/scalefaktor ; m++)
{
delete [] anzPixel3d[j][m];
}
delete [] anzPixel3d[j];
}
delete [] anzPixel3d;
对于这个项目,我使用Qt4.8和Qt-Creator 2.7.0.我使用MSVC2010编译器.
For this project I use Qt4.8 and the Qt-Creator 2.7.0.I use the MSVC2010 compiler.
错误消息是:检测到堆损坏:在0x006E3C0的正常块(#31715)之后. CRT检测到应用程序在堆缓冲区结束后写入了内存.
The error-message is: HEAP CORRUPTION DETECTED: after Normal block (#31715) at 0x006E3C0. CRT detected that the application wrote to memory after end of heap buffer.
推荐答案
#include <QCoreApplication>
#include <QVector>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int dim_0 = 3;
int dim_1 = 3;
int dim_2 = 3;
int default_val = 0;
QVector < QVector < QVector< int > > > vec(dim_0,
QVector < QVector <int > > (dim_1,
QVector < int > (dim_2, default_val)));
for( int i = 0; i < dim_0; i++)
{
for ( int j = 0; j < dim_1; j++)
{
for ( int k = 0; k < dim_2; k++)
{
vec[i][j][k] = i*100 + j*10 + k;
}
}
}
qDebug() << vec;
return a.exec();
}
希望有帮助.
这篇关于具有Qt-Objekts的Qt 3D阵列,例如QVector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!