• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# #define Question....

## Recommended Posts

EbonySeraph    122
I know what it does but I am unsure of exactly how it does it. I''ve been thinking it basically it replaces the identifier with the value it is set to everytime you type in the identifier. Basically, everywhere you put in that identifier the compiler sees only the value you have set it to when you have comiled -- am I right or does #define take up memory? Basically what I mean is if I do something like this: #define DEFINE 100 and then I have : cout << DEFINE; somewhere in my code. The compiler really sees this: cout << 100; Therefor no memory allocated because the DEFINE isn''t a variable. "Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

##### Share on other sites
Yes, no extra memory is taken up while the program is running because the #define''s aren''t compiled into the program, just the value they are set to.

##### Share on other sites
Guest Anonymous Poster
Before the actual compilation process begins the pre-processor goes through the code and among other things resolves all #define''s. The change prompted by the resolution literally replaces all instances of ''DEFINE'' to ''100''.

Two common reasons (obviously there are plenty more) for using a #define are for naming constants and for getting rid of unecessary function calls to very simple functions (macro, etc.). In C this is fine but in C++ there are other constructs better suited for the job at hand. In C++ it is better to declare the value as a constant (const int DEFINE = 100 since it allows for the same level of abstraction but now the compiler is able to perform type-checking. A rebuttal to the argument of using a const is that it wastes memory since a new variable is created and that it is slower since the program now has to resolve a location in memory for the value. The thing is that any compiler worth it''s gahones will perform the same optimization as given by using a #define. The compiler will realize it knows the value of the variable at compile time and plug it in.

For the other reason C++ allows functions to be declared as inline. An example is below:

inline bool simple_f(int x) {/* ... */}[/CODE]Wherever a call to simple_f occurs it will be as if the body of the function call were cut and pasted similar to how a #define would do it. As with using a const the inline function allows for type checking, etc. The thing is sometimes a compiler will be picky about actually inlining a function call so keep an eye on your compiler.

##### Share on other sites
Succinct    169
heh, very funny.

I had no idea what this thread was going to be about, because, when you understand #defines, "#define Question" makes perfect sense.

I thought you had just defined a new question.

lol