Thread: Define a const variable at compile time?

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    10

    Define a const variable at compile time?

    There seems to be so many ways in C++ to define a variable...

    I would like to define some constant variables at compile time such that there will be no run-time penalty. In other words, I don't want the code to have to look them up in memory each time it wants one of these variables; I'd like the variables 'inserted' and 'hard-coded' into the code, since I know they'll never change.

    Here is what I have so far:
    Code:
    const short downMinX = 30;
    const short downMinY = 450;
    const short downRangeLower = 5;
    const short downXRangeLower = 500;
    ...
    I'm not sure if this is the correct way to do it. I'm not asking freebies here. Even if one could point me in the right direction, I'm okay with that.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,974
    consts in C++ have the property you describe.

    Code:
    $ cat foo.cpp
    #include <iostream>
    using namespace std;
    
    const int ANSWER = 42;
    const int QUESTION = 24;
    
    void foo ( const int *ptr ) {
        cout << *ptr << " " << ANSWER << endl;
    }
    
    int main ( ) {
        const int *ptr = &QUESTION;
        foo(ptr);
    }
    
    $ g++ -S -O2 foo.cpp
    
    $ egrep 'QUESTION|ANSWER|42|24' foo.s
    .LFB824:
    .LFE824:
    	.cfi_def_cfa_offset 24
    	.cfi_offset 3, -24
    	movl	$42, %esi
    	movq	-24(%rax), %rax
    	movq	240(%rbp,%rax), %rbx
    	.cfi_def_cfa_offset 24
    	movl	$_ZL8QUESTION, %edi
    	.type	_ZL8QUESTION, @object
    	.size	_ZL8QUESTION, 4
    _ZL8QUESTION:
    	.long	24
    Unless you do weird things like point at them, the compiler will just use the known value without ever allocating storage for it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    10
    Quote Originally Posted by Salem View Post
    consts in C++ have the property you describe.*
    Code:
    $ cat foo.cpp
    #include <iostream>
    using namespace std;
    
    const int ANSWER = 42;
    const int QUESTION = 24;
    
    void foo ( const int *ptr ) {
        cout << *ptr << " " << ANSWER << endl;
    }
    
    int main ( ) {
        const int *ptr = &QUESTION;
        foo(ptr);
    }
    
    $ g++ -S -O2 foo.cpp
    
    $ egrep 'QUESTION|ANSWER|42|24' foo.s
    .LFB824:
    .LFE824:
    	.cfi_def_cfa_offset 24
    	.cfi_offset 3, -24
    	movl	$42, %esi
    	movq	-24(%rax), %rax
    	movq	240(%rbp,%rax), %rbx
    	.cfi_def_cfa_offset 24
    	movl	$_ZL8QUESTION, %edi
    	.type	_ZL8QUESTION, @object
    	.size	_ZL8QUESTION, 4
    _ZL8QUESTION:
    	.long	24
    Unless you do weird things like point at them, the compiler will just use the known value without ever allocating storage for it.
    So, if I've gathered correctly, a const, so long as it is not being taken as a reference, or pointer, or some type of situation forcing it to give its location in memory, is pretty much the same as if it was compiled as a direct value? It would be like a #define variable, perhaps?

    I don't know assembly, but I'm thinking the code above would compile 'QUESTION' to be put into memory at run-time, since it is being referred to by its location? 'ANSWER' would be hard-coded and direct?

    Amazing how smart and efficient they've made the compiler to be; it almost always knows best!

    Thank you for taking the time to post the assembly code as proof!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why const variable can not define array bounds.
    By MartinR in forum C Programming
    Replies: 2
    Last Post: 05-03-2018, 02:43 PM
  2. Replies: 4
    Last Post: 08-29-2010, 04:33 PM
  3. compile-time vs. runtime const multiplication
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2010, 05:32 PM
  4. #define vs const
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 11-29-2006, 08:08 PM
  5. #define or const?
    By Vber in forum C Programming
    Replies: 6
    Last Post: 11-24-2002, 03:50 PM

Tags for this Thread