Does a constant variable as a defining one reduce your program?

Example:

#define Var1 35 static const int Var1( 35); 

So, although #define replaces everywhere that I used Var1 with 35 at compile time (which I suppose makes compilation time a bit longer if you have a lot of them since it parses the code) using the static const int , the compiler considers it to be a variable .

Does this mean that when using static const int it will increase the memory footprint of my program, because it should use memory for all these constants, or is this overhead largely optimized by the compiler?

The reason I am asking for is that I am wondering if it would be better for situations like static const int in debug mode (so that you can easily see the values ​​during debugging), but do them #define in release mode so that the program is smaller.

+5
source share
4 answers

Using macros to "make the program smaller" is not available for several reasons:

  • Using macros may instead make the program larger or have no effect.
  • Macros do not comply with C ++ scope rules. You run the risk of inadvertently replacing text.
  • Depending on the quality of the tools, you may lose debugging information.
  • The beneficial effect, if it occurs, is marginal.
  • The general agreement to avoid collisions with macro names, namely ALL UPPERCASE, is a thorn.

In short, this is an example of premature optimization .

And as Donald Knuth noted, premature optimizations are Evil and trade.


Passing, note that static in

 static const int Var1( 35); 

& hellip; is redundant if it is in a namespace scope. By default, the space namespace constant has an internal relationship. Just write

 const int Var1 = 35; 

& hellip; for the same effect, but IMHO more clear.

+4
source

If it is static , then the compiler can see that it is used only inside this translation unit, and there is no need to wonder how it is used externally, which is an advantage. If you are not doing anything, this should be the actual variable (for example, by creating a pointer to it), then the compiler will often optimize it.

A friendly approach might use enums

 enum { Var1 = 35 }; 

or in c ++ 11, constexpr

 constexpr int Var1 = 35; 

They also have the advantage of not messing with a variable of the same name in a different scope if you later had

 void f() { int Var1; } 

#define will turn it into int 35;

But the difference in memory used will be very small, probably so insignificant that it will never have any noticeable impact on performance unless you are in an extremely limited environment.

+3
source

"Does this mean that using static const int will increase the memory capacity of my program, because it should use memory for all these constants, or is it overhead that is largely optimized by the compiler?"

It completely depends on your actual implementation of the compiler and how well the optimization functions work with it.

In the case of simple numeric constants that fall into a logical context, I would still use enum declarations.
And most of the time I find using static const int Var1( 35); is a better choice than #define 'd because I have full control over the scope.

+2
source

Every worthy compiler makes constant distribution to see which expression remains constant. const helps the compiler in this task.

The next thing most compilers do is remove unused portions of code. This is why const variables that are not visible from the outside either directly (local variables, static variables), or indirectly (i.e. the address of the variable was not used as a reference to assign a value to the pointer) are removed by the optimizer.

Example:

 static const int e = 29; int main() { int x = e; return x + 1; } 

MSVC 2013 will be compiled in release mode for:

 PUBLIC _main _TEXT SEGMENT _main PROC mov eax, 30 ; optimized the code to return 30 ret 0 _main ENDP _TEXT ENDS END ; no place is reserved nowhere for the static. 
+1
source

Source: https://habr.com/ru/post/1209860/


All Articles