Make local constant arrays static #23647
Merged
+39
−40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Local, non-static constant arrays may get re-initialised at runtime (by pushing their contents on the stack) every time they are reached. This somewhat surprising behaviour is caused by the fact that the C standard requires (almost) every object to have a unique address.
In theory, a compiler may eliminate the re-initialisation, if it can prove that the address of an array is never examined. However:
This often isn't possible to prove. The compiler has no way to know what will happen to an array pointer once it leaves the compilation unit.
Unlike Clang, GCC and Visual C++ don't even attempt this optimisation at all. GCC has an open ticket for this:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863
Making those arrays static avoids the problem, because static arrays are guaranteed to be initialised only once, prior the to the program startup.