'errno 11 after initialize SDL
After initialize SDL with SDL_Init and IMG_Init i observe the code 11 on errno. It means that Resource temporarily unavailable. But i don't know why. Please help me resolve this problem. I observe the value of errno before and after initialisation.
#include <stdio.h>
#include <stdlib.h>
#include "../includes/init.h"
#include "../includes/map.h"
#include "../includes/startGame.h"
#include <errno.h>
int main(int argc, char const *argv[]){
int game = 1;
SDL_Window* screen = NULL;
SDL_Renderer *screen_render = NULL;
SDL_Event e;
initAllScreenAndRenderer(&screen, &screen_render);
while (game){
while (SDL_PollEvent(&e)){
switch (e.type){
case SDL_QUIT:
game=0;
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym){
case SDLK_a:
play(&screen_render);
break;
}
break;
}
}
SDL_RenderClear(screen_render);
SDL_RenderPresent(screen_render);
}
destroyAllScreenAndRenderer(&screen, &screen_render);
}
void initAllScreenAndRenderer(SDL_Window **window, SDL_Renderer **window_renderer){
printf("\n%d\n", errno);
if (SDL_Init(SDL_INIT_VIDEO) == -1 || IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG){
printf("\nError of initialisation : %s\n", SDL_GetError());
exit(-1);
}
printf("\n%d\n", errno);
*window = SDL_CreateWindow(GAME_NAME, SDL_WINDOWPOS_UNDEFINED, `SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGTH, SDL_WINDOW_SHOWN);`
if (*window==NULL){
printf("\nError of initialisation window : %s\n", SDL_GetError());
exit(-1);
}
printf("\n%d\n", errno);
*window_renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
if (*window_renderer==NULL){
printf("\nError of initialisation renderer : %s\n", SDL_GetError());
exit(-1);
}
printf("\n%d\n", errno);
}
Solution 1:[1]
If the functions haven't reported an error, you should not check errno. A function can set errno (usually as a side-effect), even though it succeeds.
It used to be the case (and probably still is the case, but I haven't checked for several years), the many of the Standard I/O functions on Solaris would set errno to ENOTTY when the output was not to a terminal, even though the function succeeded. This is wholly legitimate.
See the C standard §7.4 Errors <errno.h> §3 where it says:
The value of
errnoin the initial thread is zero at program startup (the initial value oferrnoin other threads is an indeterminate value), but is never set to zero by any library function.202) The value oferrnomay be set to nonzero by a library function call whether or not there is an error, provided the use oferrnois not documented in the description of the function in this International Standard.202) Thus, a program that uses
errnofor error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value oferrnoon entry and then set it to zero, as long as the original value is restored iferrno's value is still zero just before the return.
TL;DR — don't check errno unless a function reports failure and is documented to set errno to a meaningful value on failure.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonathan Leffler |
