• Advertisement
Sign in to follow this  

Questions about compile time constants

This topic is 1725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Do compile time constants exist only during compile time?

Ex:

const int x = 10;
int demo_array[x];

 

All I know is that x won't occupy storage,because the compiler can't access the storage.Instead,x is put into the symbol table.

 

And,is it possible for user defined types to become a compile time constants?

Share this post


Link to post
Share on other sites
Advertisement
Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

Share this post


Link to post
Share on other sites

First of all that variable isn't guaranteed to be constant at all because you could declare it with extern elsewhere without the const and it could be accessed without const restrictions. If you want a value to be 100% constant it needs to be static const. In fact the lack of static is bound to give you compile-time errors if you try to use the value as a constant expression since, well, it isn't really constant (it's only so for the code within that file).

 

That aside, static const values in general are no storage, provided you don't do something stupid like taking a pointer to them. Things like structures and arrays take up memory for obvious reasons (though they'll be in the read-only area).

Share this post


Link to post
Share on other sites

Got it.Thanks

 

So,do compile time constants disappear after the compilation?

Edited by noatom

Share this post


Link to post
Share on other sites

Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

 

Also worth pointing out that in some cases a constant can't become zero-storage - the most obvious and frequently-encountered such example being string constants.

 

Got it.Thanks

So,do compile time constants disappear after the compilation?

 

That depends on what exactly you mean by "disappear".

Edited by Anthony Serrano

Share this post


Link to post
Share on other sites

A few years ago I tried this with VS 2010 and one of the newer GCC version that time available for Windows. Both of them stored the constants, even if they didn't need to (the constant is not referred by address only by value). Actually when I accessed the constant in the header file from multiple source files the constant got stored multiple times. This can be problematic for global constants that are used in many files. Maybe the newer compilers / linkers are not so lazy about this, it might worth a test.

 

Share this post


Link to post
Share on other sites

Assuming C++ here.


A constant can become zero-storage but isn't guaranteed to be so. For example, if you take the address of a constant, it basically is just another variable except you can't write to it at compile-time (barring evil like const_cast).

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

 

Also worth pointing out that in some cases a constant can't become zero-storage - the most obvious and frequently-encountered such example being string constants.

 

>Got it.Thanks

So,do compile time constants disappear after the compilation?

 

That depends on what exactly you mean by "disappear".

 

 

 

Well,correct me if I'm wrong,but if I create a constant like:

const int x = 10;

 

Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

Share this post


Link to post
Share on other sites

Well,correct me if I'm wrong,but if I create a constant like:

const int x = 10;

 

Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

As with many things, the compiler is free to do whatever it wants within the constraints of the language spec.  As long as it can give you the expected behavior, it might take any approach that it wants.  The approach could be due to performance, or ease of coding (on the compiler writers part), or whatever.

 

But, basically, unless you put constraints on what the compiler can do by referencing that variable in other places, then it could implement this in any way it sees fit.

Share this post


Link to post
Share on other sites

Well,correct me if I'm wrong,but if I create a constant like:
const int x = 10;
 
Wherever I use that x,shouldn't the compiler just insert 10? If it's true,it inserts 10 wherever needed,but after that,why keep it?

The compiler can do that, but it doesn't have to; furthermore in certain situations, const variables MUST be treated as literals, in certain other situations, they can't be.

For example, with code like this:

const int x=10;
...
for (i=0; i<x; ++i){...}

the compiler can treat the variable x in the for statement as if it were a literal 10, although it is not required to. But, in this code:

const int x=10;
...
int array[x];

in C++, the x in the array declaration MUST be treated as a literal 10. In C, it may be, but isn't required to (at least under the C99 and C11 standards; under older standards, it simply won't compile).

On the other hand, if you do this:

const int x=10;
...
const int *px=&x;

the compiler is required to allocate storage for x, regardless of how it deals with x elsewhere in the code.

 

And while you may find yourself asking why you would take the address of a constant, here's a more familar-looking snippet of code that does just that:

std::string hw="Hello World!";

Share this post


Link to post
Share on other sites

UDTs can be compile-time constants, sure. But they won't ever be zero-storage because that doesn't quite make sense :-)

 
 

Things like structures and arrays take up memory for obvious reasons (though they'll be in the read-only area).

 
I don't know what you guy means by "doesn't quite make sense" and "for obvious reasons". The following three programs generate the same executable byte for byte (using g++ 4.7.1):
#include <iostream>

int main() {
  std::cout << 5 << '\n';
}
#include <iostream>

int main() {
  int const i = 5;
  std::cout << i << '\n';
}
#include <iostream>

struct X {
  int x;
 
  X(int x) : x(x) {
  }
};

int main() {
  X const i = 5;
  std::cout << i.x << '\n';                                                                                   
}                                                                                                           
Edited by Álvaro

Share this post


Link to post
Share on other sites
You are correct that a UDT that can trivially be replaced with a single immediate value can (and clearly will) be elided by a good optimizer.

I suppose I should clarify my earlier statement: UDTs that are non-trivial (i.e. more than one field, complex behavior, construction/destruction semantics, etc. etc.) will almost certainly not be elided by the optimizer, assuming there is no simplification that can be done to make it trivial.


Optimizing compilers can do strange and unexpected things, and for often unclear reasons, will fail to do things that look completely sensible but are in fact prohibited by some language spec or reality constraint. In general, trying to develop an intuition for compiler optimization is best done as a white-box exercise rather than black-box. To wit: look at the generated programs in a disassembler, don't try to guess.

Share this post


Link to post
Share on other sites

I don't know what you guy means by "doesn't quite make sense" and "for obvious reasons". The following three programs generate the same executable byte for byte (using g++ 4.7.1):

Those are not arrays or structures, handling the constants there as the values directly is easy. Now consider this:

static const uint8_t dpcm_offsets[] = {
   0x00, 0x01, 0x02, 0x04,
   0x08, 0x10, 0x20, 0x40,
   0x80, 0xFF, 0xFE, 0xFC,
   0xF8, 0xF0, 0xE0, 0xC0
};

It's unlikely you'll declare an array of constants unless you pretend to access it as an array, which pretty much requires access through a pointer (even if implicitly), so stuff like this is pretty much going to end up in memory always. Similar deal with structures, especially as they're generally used to pass around large amounts of data rather than just for the sake of organization.

Share this post


Link to post
Share on other sites

Those are not arrays or structures, handling the constants there as the values directly is easy.

How is `struct X' not a structure? You made a very general statement, and I showed you a counter example. You can try to make a more limited statement if you want it to be true.

Now consider this:

static const uint8_t dpcm_offsets[] = {
   0x00, 0x01, 0x02, 0x04,
   0x08, 0x10, 0x20, 0x40,
   0x80, 0xFF, 0xFE, 0xFC,
   0xF8, 0xF0, 0xE0, 0xC0
};


I tried an example with an array just like that, and still no differences.
#include <iostream>

static const int constants[3] = {4, 5, 6};

int main() {
  std::cout << constants[1] << '\n';
}
So what are those "obvious reasons" again?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement