PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

macros vs. function calls

Hand of Fate · 05 Oct 2004, 01:20 UTC
I was curious. Macros vs. functions. Using macros would substitute the macro string possibly many times, so this would create much larger code than using a single function call? So would this make macros instead of using a function a disadvantage when it comes to memory usage because the memory usage for the program would be bigger w/macros?
Macros are evil. There is absolutely nothing a macro does better then a C++ function.

The only two exceptions are:

1) Include guards at the top of a header file.
2) Do automatically insert __LINE__, __FILE__ and __FUNCTION__ into your code. This is usually used for profiling.

Problems with macros (and this is only a partial list quickly put together):

- They don't show in the debugger (macros aren't symbols)
- Parameters can end up being evaluated multiple times
- They aren't type safe
- They duplicate code (i.e. they are always inlined)
- They pollute ALL namespaces.
- They are tricky to write.

Do yourself a favor, don't use them.
I just read an article over at codeproject the other day about inline functions vs. macros. Take a look:

http://www.codeproject.com/cpp/Macros_vs_Inlines.asp
Guru of the Week #32: Preprocessor Macros

http://www.gotw.ca/gotw/032.htm
mlepage wrote:Guru of the Week #32: Preprocessor Macros

http://www.gotw.ca/gotw/032.htm


Thank you. I always forget about GOTW when it comes time to answer questions like this one. Seems I only got 2 out of 4. I feel ashame.
Hand of Fate · 05 Oct 2004, 03:10 UTC
lol cool. hey thanks. I think that clears it up for me. They are utterly evil!