r/C_Programming • u/heavymetalmixer • Sep 11 '26
Question Unmodifiable globals initialized from functions?
Right now I'm trying to use Windows's SYSTEM_INFO to initialize 2 global constants, but global constants can only be initialized with constant expressions so this cannot be done.
Is there a way I can have "read-only" global variables that can be initialized from functions?
6
Upvotes
8
u/Physical_Dare8553 Sep 11 '26
that's a bind, in order to write to it you obviously need it to be write-able. this is the kind of case where you'd want to bind it behind a function with a static member, that you initalize when the function is first called, then just return it after
5
u/pjl1967 Sep 11 '26
In C, no.
3
u/kun1z Sep 12 '26
Yes.. kind of in a way if you use the OS's memory protection features like VirutalProtectEx.
Allocate some memory, like 1 page, set it writable, write the values you want from the initialize functions, and then afterwards set the page to Read Only. After that any attempt to write or execute on the globals will cause an exception.
2
u/heavymetalmixer Sep 12 '26
Given that the values I want from SYSTEM_INFO are just integers I opted for using function calls instead of variables, after all, functions cannot be modified at runtime.
2
u/WittyStick Sep 12 '26 edited Sep 12 '26
The obvious solution is just to encapsulate the global in a code file and provide functions to set and access it.
info.hinfo.cmain.cYou could add some checking to ensure that if
set_page_sizeis called again it does nothing.Another trick is we can just never expose the
set_page_sizefunction, but have the C runtime call it beforemainis called.info.hinfo.cmain.cOf course, this is not portable beyond MSVC, but other compilers can do the same thing. With GCC and Clang we have
__attribute__((constructor))which is even simpler to use.On linux you wouldn't really need because we have
getpagesize(), but if we want to avoid the system call and cache the result, we can do:info.cAlternatively, we can just cast to remove the
const.But anyone else could do the same and mutate
page_size, so it isn't really constant.1
u/yuehuang Sep 12 '26
Are you in C or C++?
My 2c is to avoid static initialization with system calls. Instead, in your main, have an init() function and de_init() before exit.
An even better answer is to cache on demand, if your app doesn't need the data, then it won't need to perform a system call. If it does, the first caller will pay the init price.
1
u/tstanisl Sep 12 '26
It cannot be done in C because what you want is not a constant variable but a variable with non-modifiable interface. Moreover, such design is sensitive to static initialization fiasco. Generally, the concept of "const" in C++ is a bit broken by design and a source of many problems.
One solution is to wrap access to a singleton getter:
Note, that this design is not thread-safe and it requires some non-trivial atomic operations to initialize
foocorrectly. One can use helper function like pthread_once or call_once from C standard library.2
u/sciencekm Sep 12 '26
Not possible. You need to initialize those variables at start-up or on first use (where you detect that it is not yet initialized and you proceed with the initialization).
1
u/non-existing-person Sep 12 '26
I do it like that in header file
For me it's enough. Especially that I only have a handful of globals anyway, and they are never "mutable" directly.