C 来自SDL_自由曲面的奇怪断层
我有以下简单的SDL代码:C 来自SDL_自由曲面的奇怪断层,c,segmentation-fault,sdl,C,Segmentation Fault,Sdl,我有以下简单的SDL代码: #include <SDL.h> #include <stdbool.h> #include <stdio.h> // helpers bool init(SDL_Window **win, SDL_Surface **surf) { int const width = 800; int const height = 600; if (SDL_Init(SDL_INIT_VIDEO
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
// helpers
bool init(SDL_Window **win, SDL_Surface **surf) {
int const width = 800;
int const height = 600;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
return false;
}
*win = SDL_CreateWindow("Picture test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
width, height, 0);
if (*win == NULL) {
fprintf(stderr,
"Unable to create window: %s\n",
SDL_GetError());
return false;
}
*surf = SDL_GetWindowSurface(*win);
return true;
}
bool load_media(SDL_Surface **surf) {
*surf = SDL_LoadBMP("./sample.bmp");
if (*surf == NULL) {
fprintf(stderr, "Unable to load data: %s\n", SDL_GetError());
return false;
}
return true;
}
void close(SDL_Window **win, SDL_Surface **surf) {
SDL_FreeSurface(*surf);
SDL_DestroyWindow(*win);
SDL_Quit();
}
int main()
{
SDL_Window *win;
SDL_Surface *surf;
SDL_Surface *img;
if (!init(&win, &surf)) {
return EXIT_FAILURE;
}
if (!load_media(&img)) {
return EXIT_FAILURE;
}
SDL_BlitSurface(img, NULL, surf, NULL);
SDL_UpdateWindowSurface(win);
SDL_Delay(2000);
close(&win, &img);
}
即使没有调用该函数,代码仍然在完全相同的位置对故障进行分段。只有删除整个
close
功能,此功能才能正常工作。我完全搞不清楚是什么导致了这种情况。请重命名您的函数
void close(SDL_Window **win, SDL_Surface **surf)
由于
close
是标准的C库函数。请重命名您的函数
void close(SDL_Window **win, SDL_Surface **surf)
因为
close
是标准的C库函数。我可以确认这一点。同样的情况。即使使用-Wall
和-Wextra
编译器也没有发出重新声明警告。同样适用于open()
如果这是一个gcc错误,我需要专家的意见
- 解决方案1:将
声明为静态函数(例如close()
)李>static close()
- 解决方案2:将
函数重命名为其他函数(例如close()
)my\u close\u foo()
- 我可以证实这一点。同样的情况。即使使用
-Wall
和-Wextra
编译器也没有发出重新声明警告。同样适用于open()
如果这是一个gcc错误,我需要专家的意见
- 解决方案1:将
声明为静态函数(例如close()
)李>static close()
- 解决方案2:将
函数重命名为其他函数(例如close()
)my\u close\u foo()
intclose(intfd)代码>?是否存在冲突-编译器警告?@WeatherVane我尝试将close
重命名为其他名称,并修复了它!令人惊讶的是,尽管出现了所有可以想象的警告,编译器还是接受了@此外,KozRoss还可以在没有任何警告的情况下接受它,因为在glibcclose
(以及其他许多)中,它是一个弱符号。使用这种简单的名称(并且范围有限),使用静态
函数可能是合理的代码>?是否存在冲突-编译器警告?@WeatherVane我尝试将close
重命名为其他名称,并修复了它!令人惊讶的是,尽管出现了所有可以想象的警告,编译器还是接受了@此外,KozRoss还可以在没有任何警告的情况下接受它,因为在glibcclose
(以及其他许多)中,它是一个弱符号。对于这种简单的名称(和有限的范围),使用静态
函数可能是合理的。我希望这是一个bug——如果GCC至少没有警告您一些明显愚蠢的事情,那将是相当可怕的。叮当声也是一样。我希望这是一个bug——如果GCC没有至少警告你一些明显愚蠢的事情,那就太可怕了。叮当声也是如此。