基本上,我正在用 SDL 编写程序。
这是部分代码:
// create game window
game.window = SDL_CreateWindow(
"Game",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
160,
144,
0
);
if (game.window == NULL)
{
fprintf(stderr, "Window error: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_DestroyWindow());
atexit(SDL_Quit());
return 0;
}
我的问题如下。考虑到 C 的过程性质,如果 (game.window == NULL)
会发生什么?是这样,return 1
执行。程序是否以 return 1
终止无需调用 atexit()
if
之后的函数-声明?
程序是否仍然调用SDL_DestroyWindow()
和 SDL_Quit()
, 它们都在 atexit()
中尽管有 atexit()
功能return 1
之后的函数在if (game.window == NULL)
-声明?
据我了解,C 是从上到下执行代码的,那么它怎么知道调用atexit()
中的函数呢?最后如果return 1
以前发生过吗?
最佳答案
你是对的,它不会调用那些用atexit()注册的函数。
为了展示这种行为,我编写了一个小代码片段。
#include <stdlib.h>
#include <stdio.h>
void bye()
{
printf("Bye!\n");
}
int main(int argc, char *argv[])
{
int i = 1;
if (i) {
printf("Ending\n");
return 0;
}
atexit(bye);
return 0;
}
这不会打印“再见!”,因为atexit() 是在检查i 之后注册的。
关于c - 在我的程序中调用 atexit() 的位置是否重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31860689/