flo*_*oat 2 c++ opengl pointers
如果我运行此代码,我得到错误"可能未初始化的本地指针变量'ptrNames'使用":
void processHits (GLint hits, GLuint buffer[]) //Some prints
{
unsigned int i, j;
GLuint names, *ptr, minZ,*ptrNames, numberOfNames;
if (hits == 0)
noSelected = true;
else
noSelected = false;
ptr = (GLuint *) buffer;
minZ = 0xffffffff;
for (i = 0; i < hits; i++) {
names = *ptr;
ptr++;
if (*ptr < minZ) {
numberOfNames = names;
minZ = *ptr;
ptrNames = ptr+2;
}
ptr += names+2;
}
ptr = ptrNames; //Error at this line!
for (j = 0; j < numberOfNames; j++,ptr++) {
if (hits > 0)
LastSelected = *ptr;
}
}
Run Code Online (Sandbox Code Playgroud)
sim*_*onc 10
错误是正确的. ptrNames仅在测试if (*ptr < minZ)成功时初始化.
最简单的修正可能会初始化ptrNames到NULL第一后,再检查它的价值for循环,恢复(因为没有命中处理),如果它没有被更新.
void processHits (GLint hits, GLuint buffer[]) //Some prints
{
unsigned int i, j;
GLuint names, *ptr, minZ,*ptrNames=NULL, numberOfNames;
...
if (ptrNames == NULL)
return;
ptr = ptrNames;
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18400 次 |
| 最近记录: |