c++中创建数组时抛出的异常
exception thrown when creating an array c++
我基本上是使用这个算法在游戏中生成一个平面数组,但我无法让它工作,因为当我运行程序时存在一个异常。(GLuint在opengl中就是unsigned int)
const GLuint planeDimension = 30.0f;
const GLuint half = planeDimension / 2;
const GLuint verticesCount = planeDimension * planeDimension;
int counter = 0;
GLfloat planeVertices[verticesCount];
for (GLuint length = 0; length < planeDimension; length++) {
for (GLuint width = 0; width < planeDimension; width++) {
planeVertices[counter++] = length - half;
planeVertices[counter++] = 0.0f;
planeVertices[counter++] = width - half;
}
}
您正在访问循环中的数组外部。您的循环对planeVertices
中的每个元素进行一次迭代。但是你每次循环都要增加3次counter
。因此,在所有循环的1/3处,counter
将到达数组的末端,然后您将开始在数组外写入,这将导致未定义行为。
我不知道你想干什么。为什么每次循环都要写3个不同的数组元素?所以目前还不清楚如何解决这个问题。您可以简单地将它声明为3倍大:
GLfloat planeVertices[verticesCount * 3];
或者您可以将其声明为二维数组:
GLfloat planeVertices[verticesCount][3];
那么你的循环会做:
planeVertices[counter][0] = length - half;
planeVertices[counter][1] = 0.0f;
planeVertices[counter][2] = width - half;
counter++;
相关文章:
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 您应该在什么时候创建自己的异常类型
- 我正在尝试创建一个使用 c++ 中的参数包构造函数的异常类
- C++ 异常从字符串转换为c_str会创建垃圾字符
- 如果一个对象是在本地创建的,并在C++中作为异常抛出,那么本地对象如何在其范围之外有效,即在 catch 块中?
- Windows错误:异常:使用从C++到Python的ctypes创建DLL时出现访问冲突或Windows错误193
- 如何防止构造函数在引发异常时创建对象
- 挂钩创建文件抛出异常:读取访问冲突
- 为什么要创建自己的自定义异常类
- 尝试创建 vkCreateImageView 会导致异常
- 创建自己的异常(2种方法)C
- 无法创建两个从 std::logic_error 继承的自定义异常类
- 在C++中创建自定义异常
- 异常表达式创建的异常对象的类型
- 在QT中创建我自己的异常并在函数中抛出异常
- 是否可以对构造函数抛出异常的对象进行异常处理,该对象的异常处理接近其基于堆栈的代码创建
- C++通过继承 std::exception 来创建新的异常
- 创建继承自C++中io类的异常类
- 创建CFrameWnd会给出第一次机会的异常——为什么
- Boost::interprocess_library_error异常创建shared_memory_object