what's the big difference between a function and a macro?

Started by
14 comments, last by Gink 18 years, 9 months ago
It's something I wondered about for quite some time. Why do people write (small) functions using a macro instead of just writing a "normal" function? What exactly is the benefit?
"It's better to regret something you've done than to regret something you haven't done."
Advertisement
None to little, don't do it. It opens your code up to all kinds of funny little bugs that will make you lose your hair prematurely.
Turring Machines are better than C++ any day ^_~
A function is called as a procedure, so you can have it in a library file. You can't do that with a macro, as all a macro does is replace x with whatever the function does. Also, macros can sometimes make rather ugly code... especially that Allegro END_OF_MAIN thing.
[whining]but I wanna knooooowww[/whining]

So what's the point of having macros? They must have been useful for something.
"It's better to regret something you've done than to regret something you haven't done."
I think it the macro and all the '#'s gets done before the code is written into the exe ( or compiled ) ... this might be useful if you have some kind of math that you need an awnser from.

MUst sLeEP
A function has to push and pop off of the stack and takes a performance penalty when called (extremely small and not important in 99.9999999% of cases). A macro is like an inline function - the code gets inserted directly each place you use it when compiled which is a bit faster. It's really only useful in the critical loops of your code that get called thousands of times every frame.
Macros also let you do some hacky stuff you probably shouldn't be doing in the first place.

Macros are also static (compile-time) meaning, if you're clever, you can do some things you otherwise couldn't.

For example, Boost's STATIC_ASSERT macro allows you to check a condition at compile-time. That couldn't be done with an inline function (or a function at all).
Well using a macro has just saved me from writing the same bit of code in many classes:

class GameActor : public GameEntity{public:    GameActor(){}    ~GameActor(){}    char name[128];    int  controller;    int  state;    char model[256];private:    DECLARE_HEAP;};


And DECLARE_HEAP is defined as:
#define DECLARE_HEAP '\'     public: '\'         static void * operator new(size_t size); '\'         static void operator delete(void * p, size_t size); '\'     private: '\'         static Heap * s_pHeap; '\'

('\' is without the 's, but they keep geting removed????)

The macro allows me to write the code one and put in in any class that I want to overload new and delete in. Ohhh...there is a corresponding DEFINE_HEAP as well..:)
although I could (and should) probably use templates but templates frighten me a little (ohhhhh, the big bad template is gonna get me), coz I don't really know how to use them properly.

And macros don't exactly promote type saftey now, and probably many other things besides. I imagine Fruny will have a complete explination..;)

Oh and marcos are part of pre-compilation not regular, run of the mill, compilation.
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by Shai
[whining]but I wanna knooooowww[/whining]

So what's the point of having macros? They must have been useful for something.


Macros became popular in the wee olden' days of programming. They are (were) a convenient way for programmers (and especially compiler writers) to add powerful features to languages. A text pre-processor is much easier to make than a compiler allowing for the same features. Generally modern languages and sophisticated compiler features have made macros less convenient and less necessary. Of course, macros can still do things that are otherwise difficult and unnecessarily complex.
I am a signature anti-virus. Don't spread me. I must die in peace.
Have a browse around the MFC source, or the old Half Life 1 SDK, if you can't get your hands on the HL2 SDK. Loads of macros, usually doing things that just can't be done with functions, makes life WAY easier and code MUCH easier to read/write.

This topic is closed to new replies.

Advertisement