Weird values in constants

Previous topic - Next topic

hardyx

I use some constants in my game, including simple constant expressions with other constants. Like this:

Code (glbasic) Select
CONSTANT SCR_WIDTH = 640
CONSTANT TSIDE = 32 // tile side
CONSTANT SCR_COLS = SCR_WIDTH / TSIDE


When I use the SCR_COLS constant in the code, I notice that the value is -1.INF (infinite). I was investigating the code generated and I saw this. When SCR_COLS is defined, TSIDE is initialized to 0. The correct value is set after, because the compiler "reorders" the constant lines :blink:

Code in gpc_tempg.cpp:
Code (glbasic) Select
void __glb_init_globals(){
    SCR_WIDTH= 640;
    SCR_COLS= SCR_WIDTH / TSIDE;    // TSIDE is 0!!
    TSIDE = 32;
}


If I define the constant with a value works ok.

Code (glbasic) Select
CONSTANT SCR_COLS = SCR_WIDTH / 32


I use the last GLBasic version 8.148, but I remember this (constant expressions) worked in a older version (8.0xx I think).

MrTAToad

Its not surprising - TSIDE is defined after the SRC_COLS line...

One thing to cant do is assuming the entered order of variables is the same when the program is compiled.

hardyx

But in my GLBasic code the constants are defined correctly. I know that the code generated can be different or optmized, but with the same effects as the source. If this is normal, then you can't use constants using other constants.

Slydog

I had kinda similar problems way back in version 7.341.
See this forum post:
http://www.glbasic.com/forum/index.php?topic=4575.msg34327#msg34327

But, I wasn't doing any calculations in my definition, just assigning one constant as the value of another.
I haven't checked since version 8 came out, may have been fixed.


Looking at your code in 'gpc_tempg.cpp', does GLBasic just convert our constants into standard C++ variables?
How do normal C++ constants work?  Do they take up memory?  Or just used as a direct text replacement on compile time, using no extra memory?

If using constants takes memory, then what advantages do they have? 
GLOBALs would work the same but without these problems.

GLOBALs are only global to the file they are defined in (unless it's the main project file).
But CONSTANTS are global project wide, no matter what file they are defined in? 
If so, I guess they have that working for them!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

hardyx

Quote from: Slydog on 2010-Nov-04
Looking at your code in 'gpc_tempg.cpp', does GLBasic just convert our constants into standard C++ variables?
How do normal C++ constants work?  Do they take up memory?  Or just used as a direct text replacement on compile time, using no extra memory?

If using constants takes memory, then what advantages do they have? 
GLOBALs would work the same but without these problems.
GLBasic converts constants in special variables that you can't write.

C++ constants take memory too. But they say the optimizer "hey, I can't change", and the constant is replaced for the value many times. You use constants when you want to keep the type of the value, or to write cleaner code.

With constants in GLBasic you are sure that you don't change it. And you can use Globals and Constants in other file too.

MrTAToad

The result of trying to use contants to calculate a value for storage in a constant should be regarded as unknown...