r/C_Programming • u/reimuhakurei71 • Jun 05 '26
Is a global struct bad in my case?
I have a config struct that is used in EVERY function in my project. It's okay to turn it global? Also, I heard global variables doesnt work well with multithreading, and in my case, I use a lot OpenMP, maybe because of this, its better to not turn my struct global?
Heres the example:
typedef struct
{
float up_value;
int interface;
float down_value;
} parallel_t;
typedef struct {
int nt;
float dt;
int perc;
const char* model_mode;
const char* model_path;
int nx;
int nz;
parallel_t* p_mdl;
int interface_count;
8
Upvotes
5
u/SmokeMuch7356 Jun 05 '26
Your code cuts off a line too early so the name of the typedef is missing; for the purposes of the examples below we'll just call it
foo.Ideally subroutines (functions, procedures, methods, whatever) should communicate solely through parameters and return values (and exceptions in languages that support them); they should not share global state. Doing so creates concurrency, idempotency, access, and maintenance issues. Such tightly coupled code is harder to reuse in other projects, and seemingly innocuous changes in one function can have ripple effects throughout the whole program. And if one day you do decide you want to create more instances of that type...well, that's not going to be fun.
Sometimes, every now and again, under certain conditions, it's not the wrong answer. On embedded systems with tight memory constraints it can be beneficial, and those programs aren't going to be very large or complex (compared to desktop software, anyway).
You can take a middle ground - write your functions to take a pointer to this object:
that way you can better control who can and cannot modify the object.
Then you can create a single static instance and pass its address everywhere:
This makes those functions easier to reuse elsewhere, you can enforce read-only access, you can work with more than one instance, all that good stuff.
31
u/Atijohn Jun 05 '26
use a regular static variable inside one C file and expose a global function for accessing and writing to that variable
later on if you'll need multithreading support, you won't have to change every access to that variable in the whole codebase, you'll just have to change those two functions
12
u/deckarep Jun 05 '26
If your config is loaded once on startup, then readonly for the life of the application and threads access only after initialization you are safe.
But that is just one example pattern you can employ.
4
u/mikeblas Jun 05 '26
Depends on the access patterns.
3
u/duane11583 Jun 05 '26
I use this design pattern often
I create something like struct thing_vars;
Then put ALL thing global vars in that struct
The benefit is this: I get a name space
The benefit is in the debugger the variables are organized and grouped together
The benefit is 20 global vars effectively becomes one
2
u/un_virus_SDF Jun 05 '26
Make it global with a mutex or any equivalent if you need to acces it on multiple threads
2
u/kabekew Jun 05 '26
Those are just definitions, not variable instances.
2
u/runningOverA Jun 05 '26
make it global.
global variables are bad when sending data from function to function.
configurations are called "global settings" for a reason.
1
u/MagicWolfEye Jun 08 '26
Since nobody has mentioned it yet (?!?), you might also want to look into thread_local, which is a specifier that you can put in front of a variable that makes it so that each thread has its own copy of the thing. So basically a thread-based global.
1
u/Brisngr368 Jun 09 '26 edited Jun 09 '26
With global structs if you want to change the values youre going to need to use omp master / omp single regions. It would be better if it was read only ofc.
If you want the struct to be thread local then global might not be the best option but you can mark it as private for the omp parallel region