On trickle down economics... I mean pointers.
Greetings folks,
I have the following main:
Code:
t_dserver display_server;
t_scene scene;
if (argc == 2)
{
bzero(&display_server, sizeof(display_server));
bzero(&scene, sizeof(scene));
connect_display_server(&display_server);
generate_scene(&scene, argv);
render(&scene, &display_server);
launch_event_manager(&display_server, &scene);
}
printf("Usage: program file_input\n");
return (EXIT_SUCCESS);
Whenever I find an error, or a normal quit condition, I call a shutdown() function -defined elsewhere- and clean everything, close handles, free memory, etc, before exiting.
The problem is I'm having a hard time accessing the two structures, display_server and scene from within all sections of my code that handle exit conditions. Short of passing these two structures everywhere that I may need to call shutdown(), even though I don't have any other reason to use the structures in that specific section, or making the structures global I don't see a way around the issue.
The latter choice is out of the question as I'm forbidden from using global variables. That leaves the first choice; problem with that is that I'm limited to using a max of four parameters per function. So even though I normally have pointers to the structures in the primary -read extern- functions of any given source file, I may not have enough parameter-budget left to pass the pointers to the satellite functions in that translation unit.
I came up with a "hack", that seems to work very well. I store each structure pointer in a static variable inside an adequately named function, then use this function to gain access to the structure. e.g
Code:
t_dserver *get_display_server(t_dserver *serverptr)
{
static t_dserver *dserver;
if (!dsever && serverptr)
dserver = serverptr;
return (dsever);
}
I would call this with a valid t_dserver argument once, to save the pointer, then use (void*)0 in subsequent calls when retrieving it.
What do you guys think of this? Any pitfalls I'm not thinking of?
Given the constraints I'm bound with, is there a better solution?
Thanks.