Predefined Data

I was just wandering about whether the values such as GLOBAL_H defined at start of headers is actually included in the memory of the compiled app or not.
No. That is a preprocessor symbol. Storage does not get allocated for it.

Yes, this means that every reference to GLOBAL_H in your code is simply replaced with it's value by the preprocessor before the compiler runs. So the following code
1
2
3
4
5
6
#define VALUE 5

int main()
{
  cout << VALUE << endl;
}

would effectively be turned into
1
2
3
4
int main()
{
  cout << 5 << endl;
}


That said, the value 5 itself must be stored somewhere in the program. So, in summary:
1) GLOBAL_H is not stored in memory like a variable, it is substituted for its value at compile time
2) The value of GLOBAL_H, if used in the program will become part of the program, in the same way that all constants (integer, string literal, etc) in your program will.
Thank you Xander, I wasn't entirely sure, is it possible to use bitwise operators on them in the #if statements?
To be clear, it all depends on how you use the preprocessor symbol.

In the example you gave using GLOBAL_H, that naming convention is usually used to provide a guard symbol to prevent a header file from being included multiple times.
1
2
3
4
5
// global.h header file
#ifndef GLOBAL_H
#define GLOBAL_H
// declarations 
#endif 

In this case, GLOBAL_H is only known to the compiler, and should the header file get included a second time, its contents would be skipped. As stated earlier, no storage would be allocated in the program for GLOBALS_H.

You can use expressions with #if as long as the preprocessor symbols are constants.
1
2
3
4
5
#define SOME_BITS 3 
#define FLAG1 1
#if SOME_BITS & FLAG1
// some code
#endif 







Thanks, I was thinking that those guards were going to bloat my app unnecessarily.
Topic archived. No new replies allowed.