#define

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

#define

Post by Zacariaz »

i was wondering if anyone would care to take a minute or two explaining a little tiny thing for me.

Code: Select all

#define AND(a, b) (a&b)
// vs.
bool AND(bool a, bool b) {
    return (a&b);
}
besides no input and returntype when using #define, what is the real difference really?

I realize this is something i should know, but well... i dont.

Regards
This was supposed to be a cool signature...
User avatar
JackScott
Member
Member
Posts: 1031
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Contact:

Post by JackScott »

You'll find that if you pass a complex expression ( for instance '2-1') to the #define, you'll get interesting results.

Code: Select all

#define AND(a,b) ((a)&(b))
is a better way to write it.

To answer your question, apart from that, there is only one crucial difference. The function is stored in the binary and called, whereas the definition will be replaced at compile time. For things like this, where function < function overhead, it's better to go either the inline route, or use a #define like you did.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Yayyak wrote:overhead, it's better to go either the inline route
IIRC, most good compilers do inline automatically during those situations.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

I think i get it, thanks for the explanation.
This was supposed to be a cool signature...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Some more:

You cannot take the address of the macro, i.e. you cannot pass it's function pointer to some other function.

Inlining of the function by the compiler only works when the function is defined in the translation unit, i.e. if the compiler sees only the function declaration (e.g. in some "utils.h"), it cannot inline it.

The user can undefine the macro.

The macro "vanishes" in the binary, i.e. when you're debugging the code, you don't see AND anymore, but only the expanded code.
Every good solution is obvious once you've found it.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

Playing with defines can be fun. You can set up your header files so each time they're included a different set on functions are included.

If you have one main header file which includes multiple header files, it may take a while to compile each of the source files which includes the main header. In this case you could set up a system where by you use "define" which parts you would not like included.
My OS is Perception.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

MessiahAndrw wrote:If you have one main header file which includes multiple header files, it may take a while to compile each of the source files which includes the main header. In this case you could set up a system where by you use "define" which parts you would not like included.
How about, not including a main header file to begin with?
TomTom
Posts: 23
Joined: Tue May 01, 2007 2:03 am
Location: USA

Post by TomTom »

Code: Select all

static __inline int do_something(int a, int b)
{
    return a + (a / b);
}
vs.

Code: Select all

#define do_something(a,b) (a + (a / b))
now let's make up some code that uses do_something like a function:

Code: Select all

int get_a(void)
{
    printf("get_a()\n");
    return 5;
}

int get_b(void)
{
    printf("get_b()\n");
    return 2;
}

int main(void)
{
    printf("do_something() == %d\n", do_something(get_a(), get_b());
    return 0;
}
If you run the code you're going to get different results. Use function-like macros carefully. Also, inlined function enable the compiler to perform type checks.
Last edited by TomTom on Thu Nov 08, 2007 10:10 pm, edited 1 time in total.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Hables en Inglés, por favor. :)

[edit] If you want to comment on this sentence, please do it here [/edit]
C8H10N4O2 | #446691 | Trust the nodes.
TomTom
Posts: 23
Joined: Tue May 01, 2007 2:03 am
Location: USA

Post by TomTom »

oops, for some reason I thought this was a German forum at the time I wrote the reply...
Post Reply