我有以下代码,取自我正在开发的OpenGL应用程序的一部分。当调用glfwInit()
时,GDB报告代码立即发生段错误。奇怪的是,如果我将height
的值更改为256(或至少如此,则任何事情都会如此),segfault消失。SegForm与GLFW和GLEW
#include <GL/glew.h>
#include <GL/glfw.h>
static const size_t width = 512;
static const size_t height = 512;
int main(int argc, char const *argv[])
{
glfwInit();
glfwOpenWindow(1080, 720, 8, 8, 8, 0, 32, 0, GLFW_WINDOW);
glewInit();
float heightmap[width * height * 3];
for (size_t i = 0, ix = 0; i < width; i++) {
for (size_t j = 0; j < height; j++) {
float noise = 0.0f;
heightmap[ix++] = (float)i;
heightmap[ix++] = noise;
heightmap[ix++] = (float)j;
}
}
const int numIndices = (width - 1) * (height - 1) * 6;timd
GLuint indices[numIndices];
for (size_t i = 0, ix = 0; i < width - 1; i++) {
for (size_t j = 0; j < height - 1; j++) {
indices[ix++] = (i + 0) + (j + 0) * width;
indices[ix++] = (i + 1) + (j + 0) * width;
indices[ix++] = (i + 0) + (j + 1) * width;
indices[ix++] = (i + 0) + (j + 1) * width;
indices[ix++] = (i + 1) + (j + 0) * width;
indices[ix++] = (i + 1) + (j + 1) * width;
}
}
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, width * height * 3 * sizeof(float), heightmap, GL_STATIC_DRAW);
GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(GLuint), indices, GL_STATIC_DRAW);
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, NULL);
glfwSwapBuffers();
} while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
glfwCloseWindow();
glfwTerminate();
return 0;
}
从GDB回溯显示
#0 0x00000000004009d7 in main (argc=<error reading variable: Cannot access memory at address 0x7fffff6fe41c>, argv=<error reading variable: Cannot access memory at address 0x7fffff6fe410>) at segfault.c:8
我与gcc -g -o segfault segfault.c -lGL -lGLEW -lglfw
编译。
我不明白为什么改变height
的值会影响段错误。
编辑:发布了更多的代码。段错误仍与宽度/高度发生在512,但在256
好吧,'512 * 512 * 3',再乘以3,每个浮点4个字节,为您提供3145728个字节。或3.14 MB(PI ?!)。这也是唯一的一个数组,你以后也会创建另一个索引数组。你确定你没有完全消除你的堆栈,可能堆吗?也就是说,尝试在堆上动态分配这些数组。应该有点安全。噢,也许检查GCC的标准堆栈大小(我不使用GCC,所以我不能在那里帮忙)?我认为堆栈/堆损坏是一个问题。 – Hydronium 2013-04-26 20:00:38
'4每个浮动4个字节'*完全忘了解决这个问题。 – Hydronium 2013-04-26 20:15:08
@Hydronium:我在调用'glBufferData()'时犯了一个错误,但你在堆上分配的建议解决了我的问题。随意添加一个答案,我会接受它。 – cdk 2013-04-26 23:56:23