Question Details

No question body available.

Tags

c++ arrays pointers sdl-3

Answers (2)

Accepted Answer Available
Accepted Answer
April 2, 2026 Score: 5 Rep: 126,344 Quality: Expert Completeness: 60%

In SDLAppInit(void appstate,... you can return a pointer to some user defined information. When only returning a pointer to an SDLRenderer you don't need any allocation (since the SDL framework holds the pointer that appstate points at internally).

If you need to return two pointers, you need to allocate space for that. Example:

struct RendererAndWindow {
    SDLRenderer* mrenderer;
    SDLWindow* mwindow;
};

SDLAppResult SDLAppInit(void appstate, int argc, char argv[]) {

SDL_Window window = nullptr; SDLRenderer* renderer = nullptr;

if(!SDLCreateWindowAndRenderer(MESSAGE, WIDTH, HEIGHT, SDLWINDOWRESIZABLE, &window, &renderer)) { SDLLog("Couldn't create window and renderer: %s", SDLGetError()); return SDLAPPFAILURE; } appstate = new RendererAndWindow{renderer, window}; // allocate return SDL_APP_CONTINUE; }

Then in SDL_AppIterate:

SDL_AppResult SDL_AppIterate(void appstate) { // NOLINT
    auto raw = staticcast(appstate);
    auto localrenderer = raw->m_renderer;
    //... 

Note: You need to delete the allocated RendererAndWindow when shutting down to not leak memory.

April 3, 2026 Score: 3 Rep: 27,828 Quality: Medium Completeness: 80%

One problem is that the line

appstate = (void )&state;

is wrong. The function argument appstate is a pointer to the void that will be passed to SDL_AppIterate. Therefore, you should be changing appstate instead of appstate. So you should change that line to the following:

appstate = (void )state;

After fixing this problem, another problem is that the array state in the function SDLAppInit has automatic storage duration. This means that the array will cease to exist as soon as the function SDLAppInit ends. Therefore, the pointer to this array that you store in *appstate is useless, because it points to an object that no longer exists. This is called a dangling pointer.

To fix this, you can change the storage duration of state to static. That way, the array will exist for the entire duration of the program.

In order to make this change, all you have to do is to change the line

void state[2];

to:

static void
state[2];

However, storing several pointers of different types as pointers to void in an array is generally not considered good programming style, because C++ is a type-safe programming language. Therefore, instead of using an array, it would probably be better to define a structure that specifies the exact types of the individual pointers:

struct appstate
type { SDLWindow *window; SDLRenderer renderer; };

You can make the void that is passed to SDLAppIterate point to such a structure, instead of an array. That way, you will only need a single type cast for the structure as a whole, instead of a type cast for every member of the structure. This will become more imporant if you add more members to the structure.

In order to accomplish this, you can write the following code in the function SDLAppIterate:

static appstate_type state = { window, renderer }; appstate = (void )&state;