.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Forum Thread
  Posts  
qc++ #define constants (Groups : qc : Forum : vault() : qc++ #define constants) Locked
Thread Options
numbersix
numbersix quake-c coder++
Nov 23 2014 Anchor

I have wrote code complicated enough to break the qcc compilers I use.

How?

18221 numglobaldefs (of 32768)

Quake-c has a limit on global variables!

What does qcc consider a global?

- any constant, if used
- every variable defined - locals inside a function included ! - even if unused
vectors are 4 globals - ouch ! vector v - is v, and float v_x, v_y, v_z - four new globals for every vector defined
- every function defined - i.e. void() a_function = { }; - even if unused - including frame macros
- every function parameter - i.e. void(float f1, string s1) adds 3 globals - 1 function and 2 parms - every time, no reusing
- every unique string
* special case: "" (that is an empty quote set or null string) bumps globals every time its used locally !!
so if you use "" once in a function, it is another global -- _really_, using it a second time in the same function does not
one advantage is that vector literals - '1 2 3' counts as a string - but it is still a global for every unique vector literal
-- every unique literal !!! I am not even kidding
what is a literal? - 1, 10, 215 are literals, so if you have never used 666 and you assign it or test it, you have a new global !

note: compiler optimizations may (or may not) remove some of these
( I tested fteqcc -O2 with "" and it doesnt fix that case. -O3 does, however I dont know what it does to code... )

What doesnt count?
- constants defined but NOT used
- if you have float CNST = 666; then do NOT reference it, it counts not
- repeated strings - i.e. "string1" then anywhere else in the code "string1", is only one global
- repeated vectors - i.e. '32 32 56' then anywhere else in the code '32 32 56', is only one global

The more complex your code, the closer you come to that 32768 limit. I tried to recode qcc and the engine once. It broke the engine. Or you had to recompile every single progs.dat you run on it. (I know that can be adjusted, but the point is this is a difficult thing to change...)

What can a coder do?

How about #define for constants. From the fteqcc faq:

  • max. 2048 defines are allowed

So, I checked fteqcc source for this. I could not find it.
I did a test compile with 2100 defines. It had no complaints.
It appears we can safely use as many defines as we want.

// items
float IT_AXE = 4096;
float IT_SHOTGUN = 1;
float IT_SUPER_SHOTGUN = 2;
float IT_NAILGUN = 4;
float IT_SUPER_NAILGUN = 8;
float IT_GRENADE_LAUNCHER = 16;
float IT_ROCKET_LAUNCHER = 32;
float IT_LIGHTNING = 64;
float IT_EXTRA_WEAPON = 128;

Those are 9 globals if used. (v106 only uses 8 )
These are not:

#define IT_AXE 4096
#define IT_SHOTGUN 1
#define IT_SUPER_SHOTGUN 2
#define IT_NAILGUN 4
#define IT_SUPER_NAILGUN 8
#define IT_GRENADE_LAUNCHER 16
#define IT_ROCKET_LAUNCHER 32
#define IT_LIGHTNING 64
#define IT_EXTRA_WEAPON 128

The code looks exactly the same. In essence these are true constants.

Does it change other compile stats? - Yes:

global constants:

88356 strofs (of 1000000)
4266 numglobaldefs (of 32768)
380482 TOTAL SIZE

define constants:

88244 strofs (of 1000000)
4258 numglobaldefs (of 32768)
380306 TOTAL SIZE

Some space savings happens as well.

Hints:

You need at the least this one global constant:

string null_string = ""; // saves many globals

And in a function: if (s1 == null_string)... - this avoids the "" counting as new globals issue.
Note: defs.qc has string_null, but it is never assigned "" - however, this may compile the same code.

DO NOT:

define null_string ""

Why? - the compiler sees that as "" - and count it as a new global for every local use!

Now, go replace some global constants...

Edited by: numbersix

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.