我已经[尝试]为zBuffer实现二维数组,如下所示:
struct Properties {
....
double** zBuffer;
....
}
以下是使用它的地方:
void initializeZBuffer(Properties* props){
//Destroy old zBuffer 2D array (if it's already been initialized)
if (sizeof props->zBuffer[0] >= 0){
for (int i = 0; i < props->clientRect.Height(); i++){
delete[] props->zBuffer[i];
}
delete[] props->zBuffer;
}
//Create new zBuffer 2D array
props->zBuffer = new double*[props->clientRect.Height()]; //zBuffer height x width
for (int i = 0; i < props->clientRect.Height(); i++){
props->zBuffer[i] = new double[props->clientRect.Width()];
}
}
我的目标是为屏幕上的每个z
x
像素创建一个包含y
值的数组。
我的代码中的问题是:我检查数组中是否有任何数据 - 它不应该在第一次迭代中,但确实如此。出于某种原因,每个插槽的大小为4。
例如,在此时进行调试:
sizeof props->zBuffer[1] -----> returns 4
sizeof props->zBuffer[100] -----> returns 4
sizeof props->zBuffer[1000000] -----> returns 4
sizeof props->zBuffer[10000000000] -----> returns 4
和
sizeof props->zBuffer[1][1] -----> returns 4
sizeof props->zBuffer[100][100] -----> returns 4
sizeof props->zBuffer[1000000][1000000] -----> returns 4
sizeof props->zBuffer[10000000000][10000000] -----> returns 4
由于它的大小为4,我自然会尝试查看props->zBuffer[3]
(最后一个插槽)中的内容,但是我收到错误
ds->zBuffer[3]
CXX0030: Error: expression cannot be evaluated
有没有人知道发生了什么? 我完全感到困惑和沮丧:(
答案 0 :(得分:1)
sizeof运算符产生其操作数的对象表示中的字节数。
让我们看一下sizeof(props->zBuffer[1])
。首先,props->zBuffer[1]
相当于*(props->zBuffer + 1)
。如果我们将1
添加到double**
,我们仍会有double**
,如果我们取消引用它,我们会得到double*
。然后你拿sizeof
那个。在您的计算机上,double*
占用4个字节。这是double*
的对象表示 - 存储double
地址所需的字节数。