Thread: Is it ok to use #define 's ?

  1. #1
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    Question Is it ok to use #define 's ?

    A big habit of mine currently is to #define everything i can.
    I find this useful because:
    1) its easy to change the value of it, rather than go and change each instance in the code

    2) its faster and uses less memory (or so i hear)

    Im just wondering if #define's are good, seeing as i dont see them used a lot, at least in the sources i look at...

    Is there any drawbacks to using them?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    They are very good to use (when used correctly).
    I don't know about 2)...
    I thought that the compiler just replaces X with Y in the code, when using #define X Y, so I don't think you'll "earn" any memory.
    They are very useful for understanding the code better and easily change values though(like you said).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    A quick search on the cprogramming board:

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    #defines can be handy, but remeber that you can use const ints for a lot of #define constants.......these can be preferable #define constants can make debugging difficult.

    As to memory saving, with a #define, the value may be placed in the actual assembly as opposed to the CPU having to reference memory to find the value;

    Code:
    #define x 10
    
    int main(void) {
    
    	int y = 10,
    		z;
    
    	z = x;
    	/* 1 instruction, value of 10 (0x0A) in instruction
    	mov         dword ptr [ebp-8],0Ah*/
    
    	z = y;
    	/* 2 instructions, memory in y loaded into register, then value assigned
    	mov         eax,dword ptr [ebp-4]
    	mov         dword ptr [ebp-8],eax*/
    
    	
    	return 0;
    }
    So I suppose you are saving efficiency of a sort.....but how eefective this is depends on what you are doing....most of the time it wont matter

    For inlining of small functions they are really dodgy at times.....try this nonsense..

    Code:
    #include <iostream>
    using namespace std;
    	
    #define defmax(a,b)(((a) > (b)) ? (a) : (b))
    
    int main(void) {
    
    	int x = 10,
    		y = 11;
    
    	cout << defmax(x,y) << endl; //sensibly this will output 11..
    
    	cout << defmax(++x,--y) << endl;//x+1=11,y-1=10...11 still??...nope!!	
    
    	return 0;
    }
    Also, such #defines as above arent type safe.....while the better option (inline functions) are.......so try stick with inlines where you can.

  5. #5
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    yeah, i mostly use them as just a plain swap:

    ie: #define x 10

    and try to steer clear of complex macros as it becomes hard to debug.

    About the earning memory thing, if i declare

    const int x = 10;

    then arent i allocating memory for it, whereas a #define doesnt need to, hence the efficiency?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by fry
    yeah, i mostly use them as just a plain swap:

    ie: #define x 10

    and try to steer clear of complex macros as it becomes hard to debug.

    About the earning memory thing, if i declare

    const int x = 10;

    then arent i allocating memory for it, whereas a #define doesnt need to, hence the efficiency?
    If I had declared y as const, then 10 would have appeared as part of the instruction just as if I had used #define (well...this is how my compiler would implement it anyway )

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Im just wondering if #define's are good, seeing as i dont see them used a lot, at least in the sources i look at...
    Defining things with the #define directive is acceptable in C++, but when possible you should opt for const variables and inline functions, they tend to be safer and easier to read.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    I use #defines in large programs and stuff, and for variables I'll be using a lot. For example, in MoA, I have #defines for GRASS, WATER, MOUNTAIN, TREE, etc. It's easier to remember and use then trying to remember the number I'm using to represent grass. Instead, I can just type GRASS. That way others know what I'm talking about, as well. It's more convenient for me. I could use const ints or something, but I did #define so oh well. They both work.

    Brendan

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    #define when used for values such as those to be used in a switch is great. I can't imagine that being bad form. For something like NULL,TRUE,FALSE it's perfect

    #define NULL 0
    #define TRUE 1
    #define FALSE 0

    No problems there. but will agree certainly that macros (code hidden behind #defines) is naughty.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#define when used for values such as those to be used in a switch is great.
    For grouped values such as those to be used in a switch statement, I usually prefer enum.

    >#define NULL 0
    To be safe you should define NULL as such:
    Code:
    #undef NULL
    #define NULL 0
    >#define TRUE 1
    >#define FALSE 0
    Same as above, many implementations may define TRUE and FALSE for their own (different)purposes. This can result in unwanted problems.

    -Prelude
    My best code is written with the delete key.

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    oh of course you would likely do the #ifndef or #undef thing I was just simplifying the example.

    As far as enum, probably not a bad plan except that it doesn't make it clear what that value is. So if you're going across dll's with those values (as in WM_* messages) you had better make sure the enum lines everything up.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Prelude
    This can result in unwanted problems.
    is there any other kind of problem?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there any other kind of problem?
    Well, sometimes I create problems as an example of what not to do. And if you do something wrong you want the computer to scream and moan and die from the pain rather than quietly destroy things.

    I suppose 'unwanted' can be placed into degrees.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I like using #define's for flags. Using bitwise operators it is very easy to test and set flags for specific purposes. I think this is pretty handy and not 'poor' code.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    23
    i see #define's as a way to give commands that thier purpose isnt clear a name.
    ones that i use alot too, not something i just have to do once or twice.
    let us eat and drink

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:52 PM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM