What does "statically allocated" exactly mean in libc? One per library instance? One per program instance?

182 Views Asked by At

In (g)libc, for example in time and date functions like localtime, the manual says:

The return value points to a statically allocated string which might be overwritten by subsequent calls to any of the date and time functions.

As far as I know, my program is single-threaded. Is it safe for me to use the "MT-Unsafe" functions like asctime or localtime?

Even if there is only 1 (g)libc library instance in the memory? (I.e. as a dynamic library.)

Does "static" mean "static to my program" (for each program instance a new buffer instance is allocated..), "static to the process" (one buffer instance per process -- this suggests), or static to the (libc) library (as many buffer instances as library (-fragment) instances)?

(I know about question #8694365 but mine a bit differs.)

1

There are 1 best solutions below

0
laarmen On

The global (defined outside a function) and static (defined inside a function) are allocated once for each program. Otherwise, asctime would be totally unusable, as you cannot be sure there isn't another program calling that exact function at the same time yours does.