Jump to content

  • Log In with Google      Sign In   
  • Create Account


Questions about compile time constants


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 noatom   Members   -  Reputation: 767

Like
0Likes
Like

Posted 29 April 2013 - 05:06 PM

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?



Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14256

Like
0Likes
Like

Posted 29 April 2013 - 05:15 PM

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 :-)

#3 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
2Likes
Like

Posted 29 April 2013 - 06:15 PM

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).


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#4 noatom   Members   -  Reputation: 767

Like
0Likes
Like

Posted 29 April 2013 - 11:15 PM

Got it.Thanks

 

So,do compile time constants disappear after the compilation?


Edited by noatom, 29 April 2013 - 11:21 PM.


#5 Anthony Serrano   Members   -  Reputation: 1098

Like
0Likes
Like

Posted 29 April 2013 - 11:27 PM

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, 29 April 2013 - 11:45 PM.


#6 zaphod2   Members   -  Reputation: 576

Like
0Likes
Like

Posted 30 April 2013 - 12:39 AM

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.

 



#7 noatom   Members   -  Reputation: 767

Like
0Likes
Like

Posted 30 April 2013 - 01:16 AM

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?



#8 0r0d   Members   -  Reputation: 797

Like
0Likes
Like

Posted 30 April 2013 - 01:42 AM

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.



#9 Anthony Serrano   Members   -  Reputation: 1098

Like
0Likes
Like

Posted 30 April 2013 - 05:46 AM

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!";


#10 Álvaro   Crossbones+   -  Reputation: 11861

Like
2Likes
Like

Posted 30 April 2013 - 07:30 AM

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, 30 April 2013 - 07:33 AM.


#11 ApochPiQ   Moderators   -  Reputation: 14256

Like
0Likes
Like

Posted 30 April 2013 - 11:47 AM

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.

#12 Sik_the_hedgehog   Crossbones+   -  Reputation: 1483

Like
0Likes
Like

Posted 01 May 2013 - 01:49 PM

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.


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#13 Álvaro   Crossbones+   -  Reputation: 11861

Like
0Likes
Like

Posted 01 May 2013 - 02:11 PM

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?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS