We can extract page_size at runtime via sysconf(_SC_PAGESIZE)
. My first intention was to put this value on program startup into an object with static storage duration. So my intention was to declare some extern
variable in a file scope as follows
extern const size_t page_size;
But when I try to define it somewhere else in a file scope as
const size_t page_size = (const size_t) sysconf(_SC_PAGESIZE);
it does not compile. And that seems to be clear since 6.7.9(p4)
:
All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.
I would not like to call the sysconf(_SC_PAGESIZE)
any time I need a page size. Is there some workaround for that or what is the common solution?
sysconf(_SC_PAGESIZE)
is a function call. It will always return the same value, but it is still a function call, so it cannot be used to initialize a global variable in C.
If you wanted to avoid calling that function repeatedly, you could declare the global variable as non-const
, and assign its value during application startup.