gpt4 book ai didi

c++ - 基本的 c 指针返回

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:07 25 4
gpt4 key购买 nike

以下应用因空指针而崩溃。我想知道出了什么问题...

#include "stdio.h"
#include "string.h"

char* getFileData(char* fileName);
bool createShader( int shaderType, const char* shader, const char* shaderFile ) ;
void glShaderSource(int shader, int count, const char** string, const int* length);

int main(int argc, char* argv[])
{
char* shader = getFileData("filea.csd");
bool success = createShader(1, shader, "filea.csd");
return 0;
}

char* getFileData(char* fileName) {
if(!strcmp(fileName, "filea.csd"))
return "this is the content of the file a\0";
else if(!strcmp(fileName, "fileb.csd"))
return "this is the content of the file b\0";
else if(!strcmp(fileName, "filec.csd"))
return "this is the content of the file c\0";
else
return "";
}

bool createShader( int shaderType, const char* shader, const char* shaderFile )
{
int shaderHandle = 122;

glShaderSource( shaderHandle, 1, &shader, NULL ); ////This line is where it crashes.
return true;
}

void glShaderSource(int shader, int count, const char** string, const int* length) {
}

最佳答案

由于 Pointer 为 NULL,尝试传递双指针。形式的东西

bool createShader( GLenum shaderType, const char** shader, const char* shaderFile )//<-- change 
{
GLuint shaderHandle = glCreateShader( shaderType );

glShaderSource( shaderHandle, 1, shader, NULL ); //<--change
glCompileShader( shaderHandle );

int status = 0;

glGetShaderiv( shaderHandle, GL_COMPILE_STATUS, &status );
}

void main () {
char* shader = strdup(getFileData("filea.csd"));
bool success = createShader(shaderType, &shader, "filea.csd"); //<--change
delete shader;
return success;
}

关于c++ - 基本的 c 指针返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com