• ### Announcements

GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Followers 0

# Questions about compile time constants

## 12 posts in this topic

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?

0

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

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

2

##### Share on other sites

Got it.Thanks

So,do compile time constants disappear after the compilation?

Edited by noatom
0

##### 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
0

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

0

##### 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?

0

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

0

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

##### 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
2

##### 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.
0

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

0

##### 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?
0

## Create an account

Register a new account