#define and const keyword?
Ok, #define is a C relic so is their any downside to using const instead of #define? Also which is faster? If I am going to use #define why don''t I use typedef instead of #define for making alias''s? Or is it faster to use #define again? I am confused on this approach or is it a matter of personal preference?
Windows 98/SE/ME SUCKS!
Deal with it!
if(windows crashes)
run Linux
else
yeah right!!
RESIST WINDOWS XP!!!!!!!!!!
RESIST .NET TECH!!!!!!!!!!!
#define isn''t a C relic. It''s a preprocessor directive, and there are some things that can only be done using #define and others (#ifdef, #if, #endif, #pragma, etc). Using #define to declare constants is what is now deprecated, because of the lack of type information and checking. const allows/ensures type checking, which is a Good Thing(tm). const also applied scope to your constant while #define creates system-wide definitions (other programs can detect your #define''s, such as #ifdef _cplusplus - a compiler environment variable).
In terms of speed, all preprocessor directives are handled prior to compilation (#include is also a preprocessor directive). So #include tells the preprocessor - the part of the build process that gets all the files ready for compilation and then linking - to place the entire contents of the specified file in the place of the directive. All #defines are therefore resolved before any compilation occurs. It has zero effect on speed. If you''re simply using #define to declare constants and aliases, using const or typedef entails no performance penalty (also consider that you don''t declare constants within critical code, but rather usually at the top of a file).
In the final analysis, in C++ the use of #define for constants/aliases is a matter of personal preference. In C, it''s largely a matter of necessity.
In terms of speed, all preprocessor directives are handled prior to compilation (#include is also a preprocessor directive). So #include tells the preprocessor - the part of the build process that gets all the files ready for compilation and then linking - to place the entire contents of the specified file in the place of the directive. All #defines are therefore resolved before any compilation occurs. It has zero effect on speed. If you''re simply using #define to declare constants and aliases, using const or typedef entails no performance penalty (also consider that you don''t declare constants within critical code, but rather usually at the top of a file).
In the final analysis, in C++ the use of #define for constants/aliases is a matter of personal preference. In C, it''s largely a matter of necessity.
I''m not sure, but I thik #define is faster than const, because it''s done all by the preprocessor, so using a #defined value is the same as actually typing that number into your program. Const might be inserted the same way by the compiler, just with type-checking, but I think it''s stored like a normal variable, which makes it slightly slower.
-Deku-chan
DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
-Deku-chan
DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
May 24, 2001 09:16 PM
quote:
Original post by DekuTree64
I''m not sure, but I thik #define is faster than const, because it''s done all by the preprocessor, so using a #defined value is the same as actually typing that number into your program. Const might be inserted the same way by the compiler, just with type-checking, but I think it''s stored like a normal variable, which makes it slightly slower.
If the compiler you use is doing things correctly, const literals of native types will be no slower than #define constants.
Well,
I don''t really mind the sig. I think everyone is entitled to their own opinions.
Anyway, I use const more. Why? When debugging, consts appear in the debugger, while #defines don''t.
And as someone pointed out, consts have type checking, which is good.
I don''t really mind the sig. I think everyone is entitled to their own opinions.
Anyway, I use const more. Why? When debugging, consts appear in the debugger, while #defines don''t.
And as someone pointed out, consts have type checking, which is good.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
quote:
Original post by DekuTree64
I''m not sure, but I thik #define is faster than const, because it''s done all by the preprocessor, so using a #defined value is the same as actually typing that number into your program. Const might be inserted the same way by the compiler, just with type-checking, but I think it''s stored like a normal variable, which makes it slightly slower.
This topic has been beaten to death 50 times over.
http://www.gamedev.net/community/forums/topic.asp?topic_id=43419
http://www.gamedev.net/community/forums/topic.asp?topic_id=36357
The GameDev search engine--it really does work!
They''re both constants. Performance difs will be nil. But one has the potential to make your debug life hell.
#define x 1
Rips through and replaces all your x''s with 1. Then you get the error:
Type mis-match in:
if n < 1 {
And you''re left wondering where the hell 1 came from. Searching for it will NOT be fun.
obfuscation bonus point: instead of a 1, it''s a lower case L.
The Rule: Only use #define for compiler directives to manage your libraries, release vs debug builds, etc. Do NOT use it for regular variables.
Oh, but if you hate Microsoft, you might want to ignore this tip -- they taught it to me.
#define x 1
Rips through and replaces all your x''s with 1. Then you get the error:
Type mis-match in:
if n < 1 {
And you''re left wondering where the hell 1 came from. Searching for it will NOT be fun.
obfuscation bonus point: instead of a 1, it''s a lower case L.
The Rule: Only use #define for compiler directives to manage your libraries, release vs debug builds, etc. Do NOT use it for regular variables.
Oh, but if you hate Microsoft, you might want to ignore this tip -- they taught it to me.
Dustin
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement