Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent my 'unused' global variables being compiled out?

I'm using static initialisation to ease the process of registering some classes with a factory in C++. Unfortunately, I think the compiler is optimising out the 'unused' objects which are meant to do the useful work in their constructors. Is there any way to tell the compiler not to optimise out a global variable?

class SomeClass {     public:         SomeClass() {             /* do something useful */         } };  SomeClass instance; 

My breakpoint in SomeClass's constructor doesn't get hit. In my actual code, SomeClass is in a header file and instance is in a source file, more or less alone.

EDIT: As guessed by KJAWolf, this code is actually compiled into a static lib, not the executable. Its purpose is to register some types also provided by the static lib with a static list of types and their creators, for a factory to then read from on construction. Since these types are provided with the lib, adding this code to the executable is undesirable.

Also I discovered that by moving the code to another source file that contains other existing code, it works fine. It seems that having a file purely consisting of these global objects is what's causing the problem. It's as if that translation unit was entirely ignored.

like image 963
Ben Hymers Avatar asked Aug 04 '09 19:08

Ben Hymers


People also ask

How can parameters be used to avoid the use of global variables?

Parameter passing - allows the values of local variables within the main program to be passed to sub-programs without the need to use global variables. The value of these variables (or a copy of the value of these variables) is passed as a parameter to and from sub-programs as necessary.

How do you avoid global variables?

The simplest way to avoid globals all together is to simply pass your variables using function arguments. As you can see, the $productData array from the controller (via HTTP request) goes through different layer: The controller receives the HTTP request. The parameters are passed to the model.

What are two reasons why you should not use global variables?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

Do global variables need to be freed?

Due to this, global variables are one way we have of sharing the same data between functions. The main difference with local variables is that the memory allocated for variables is freed once the function ends. Global variables are only freed when the program ends.


2 Answers

The compiler is not allowed to optimiza away global objects.
Even if they are never used.

Somthing else is happening in your code.
Now if you built a static library with your global object and that global object is not referenced from the executable it will not be pulled into the executable by the linker.

like image 62
Martin York Avatar answered Oct 19 '22 22:10

Martin York


The compiler should never optimize away such globals - if it does that, it is simply broken.

like image 25
Pavel Minaev Avatar answered Oct 19 '22 23:10

Pavel Minaev