It is possible to instantiate a static variable twice.

I discovered recently while working on a C++ project that if being a bit sneaky one can instantiate a static variable twice. Normally C++ compilers won't let you do as when creating the object code it will a variable being initialized twice. But if the variable sits inside a library that is dynamically linked this is possible.

Now it goes without saying that this may caused a lot of obscure and difficult to debug errors. For instance if the static variable allocates memory dynamically on the heap, having more than one static variable being instantiate this lead to a memory leak.

I created a mini project on Github to illustrate this.

social