Came across this coding standard from Oracle:
Do not use a static array of strings.
What is the reason for this recommendation?
If the array has static storage duration (meaning it was declared at file scope outside of any function body, or was declared with the static keyword) and no initializer is present, then all of the array elements are initialized to 0 (for scalars) or NULL (for pointers).
Static array or variables will not be freed, when control comes out of that function. Scope of static variable is local to the function in which it is declared, but its lifetime is throughout the program.
If the array is declared statically, then we do not need to delete an array since it gets deleted by the end of the program/ block in which it was declared. If the array is declared dynamically, we need to free the memory allocated to it using the free() function.
Arrays are mutable containers. Global mutable container without built-in synchronisation is multi-threaded horror waiting to happen. You can store reference to that array in some local context and then have it's content changed at any time without you knowing it. Or vice versa you may assume that this is convenient global state, but then every client must know the exact correct way to synchronise its access to that state. And somebody will forget and bug will be born.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With