我有点在为 C++ 学习 SDL OpenGL(我的错误),我不得不将它移植到 C。因为 C++ 对我来说有点困惑(顺便说一句。是的,我可以在网上搜索功能替代方案)。所以运行这个给了我一个错误,似乎是在 NVIDIA 驱动程序中(顺便说一句。卡是 GeForce 105m)。这是我的错还是驱动程序中的错误(我认为是我因为它上面的每个游戏似乎都运行良好) ?
这是 gdb 回溯:
Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106 ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0 strlen () at ../sysdeps/x86_64/strlen.S:106
#1 0x00007ffff59cf699 in ?? ()
from /usr/lib/nvidia-340-updates/libnvidia-glcore.so.340.76
#2 0x00007ffff59d1d89 in ?? ()
from /usr/lib/nvidia-340-updates/libnvidia-glcore.so.340.76
#3 0x0000000000401f86 in compileShader ()
#4 0x0000000000401ca6 in compileShaders ()
#5 0x00000000004018b9 in initShaders ()
#6 0x0000000000401a02 in Initilize ()
#7 0x00000000004015ae in main ()
这里是 compileShader 函数(我不会完成整个代码,因为它太长了;),如果你愿意,我仍然可以发布它):
void compileShader(char* filePath, GLuint id) {
//Open the file
FILE *shaderFile = fopen(filePath, "rw");
if (shaderFile == NULL) {
char *str;
sprintf(str,"Failed to open %s", &filePath);
fatalError(str);
}
//File contents stores all the text in the file
char * fileContents = "";
char symbol;
//Get all the lines in the file and add it to the contents
while ((symbol = fgetc(shaderFile)) != EOF ) {
fileContents += symbol;
}
fileContents += EOF;
fclose(shaderFile);
glShaderSource(id, 1, &fileContents, NULL);
glCompileShader(id);
GLint success = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE)
{
glDeleteShader(id);
char *str;
sprintf(str,"Shader %s failed to compile", filePath);
fatalError(str); //Don't worry this just prints out the error
}
}
最佳答案
这是您代码中的错误。
您提供给驱动程序的 fileContents
指针完全无效,因此在取消引用该指针时驱动程序崩溃。
您在 C 中没有原生字符串数据类型,您只需使用 char
数组。 C 不会为你做任何类型的内存管理。因此,char 指针上的 += 运算符不执行字符串连接。这只是指针算术。你只是在内存中有一个 emoty 字符串,并且 fileContent
最初指向它。顺带一提
fileContents += symbol;
您将该指针增加 symbol
的数值,因此指向该空字符串之外的一些内存。
我不想听起来很粗鲁,所以请不要误会我的意思。但我真的建议您先学习您想要使用的编程语言,然后再继续使用 OpenGL。
关于c - 在 nvidia-340-updates 中获取段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31224263/