lxnf9...@gmail.com wrote: |On Wed, 10 Sep 2014, Werner LEMBERG wrote: |>> The problem with global variables is their long range effect, |>> comparable with the infamous goto statement: considered harmful. |> |> I like `goto' a lot, and it is an invaluable instruction if used with |> care. The same is true with global variables. Sorry to say, but we |> are now going into hearsay and hollow statements, which isn't helpful. | |I may have to look into this C++ stuff. I have only written C and I have |not figured out how to write multi-threaded applications without global |variables. And I make good use of goto and longjmp. Apps would be a lot |junkier without them.
_I_ usually collect such global data in a structure and instantiate that; in BSS usually, but if i there is complete control and it is doable then on the stack and all functions take that as a "self" first argument (most likely i use this approach for single compilation units). This has good side-effects: i can search for /_?g[.-] and get all global data matches (usually without false hits, then), and i can layout the structure in a way that minimizes useless alignment padding. I can use the global instance as a first-time initialization switch if i base it upon a pointer, then only have a single global mutex to protect _all_ first time inits all through the entire library / whatever. In fact i wrote programs and libraries without any DATA section, but text / rodata and a minimal bss, anything else was stack and heap-bss (through the dynamic allocator a.k.a. mmap(2)). This all was C++ (but -nostdlib -fno-rtti -fno-exceptions etc.). --steffen