Advertisement

What exactly is a macro?

Started by April 12, 2002 04:03 AM
22 comments, last by maccaroo 22 years, 8 months ago
Yes I mustly program in c haven''t really done too much in c++. But in c compilers you can still use inline functions instead of macros.

The only thing that you can''t protect a macro against is a wrong type being placed in it and if converting the incorrect type to the desired type doesn''t cause any problems with any calculations it''ll be okay. But that will not exist for all cases.

The RAD_DEGREE macro would be best used if you pass a numeric variable to it but if a char was passed (this should be highly unlikely but could happen) it could cause whatever code that is dependent on that calculation to act strange. Making it hard to track what the problem is.

quote: Original post by maccaroo
I know this probably sounds like a really dumb question. I''ve worked with MS Access, and from that I thought that macros were just shortcuts for little tasks that would be used often (I never really used them actually, ''cos apparently they''re not totally compatable). Anyway, I''ve come across the word ''macro'' quite a bit recently, and I''d like to know if anyone could explain it a little more in depth for me. What are they used for, and should I try to use them when I program games?

What is the difference between a macro and a function? I''ve tried to find ''macro'' in the GameDev dictionary, alas, that was not to be...

[edited by - maccaroo on April 12, 2002 5:06:12 AM]


A macro in the C/C++ sense is a piece of code that is assigned a name and everytime the pre-compiler runs into the keyword, it replaces it with the piece of code you assigned to that macro. ie: in most books you sometimes run into these C/C++ macros:

#define MAX( A, B ) ( ( ( A ) > ( B ) )? ( A ) : ( B ) )// and then in your main, you will se a call to MAX// like so:int main( int argc, char *argv[] ){   int maxnum;   maxnum = MAX( 20, 10 );} 


The pre-compiler replaces MAX( 20, 10 ) with before the compiler runs through the code:

maxnum = ( ( ( 20 ) > ( 10 ) ) ? ( 20 ) : ( 10 ) )

it replaces every instance of MAX( X, Y ) in your main app by the code that is assigned to it.

Now, functions are a different thing, the compiler doesn''t replace the name of your function for the code. It jumps to it( if you understand the basics of ASM, you''ll know what I mean ). For those who don''t know ASM, let me elaborate a bit:

In ASM, the JUMP command is like a goto( ie: JMP or JUMP is an uncoditional JUMP, it goes to a specified tag like in C when you use gotos. JNZ, JZ, JNE, JE, etc and there are many more, are conditional jumps, and can be concidered a bit more like if statements in C.)
example:

   mov cx, 10label:   dec cx   jnz label 


The code above is a standard for ( x = 10; x > 0; x-- ){} in C.

   mov AX, 10   mov BX, 20   jmp skipthis   add ax, bx   je calcOKskipthis:   push AX   push BX   ... here the would probably be a jmp to the end of the func.calcOK:   ...    


Now, with that said, when you compile your code, each function is assigned a name tag and the code jumps to that name tag when it sees a call to a function in your code. ( Correct me if I''m wrong on that but as far as VC++ goes, that''s the way it seems to work when I try to debug my code. ) Anyhow, the explanation sounds a bit complicated but hey, I can''t explain it in any other way. That''s how it looks to me. ( Maybe the words aren''t exactly right or I overcomplicated it, but I''m at a point where I tend to have problems putting things in simple terms ).

"And that''s the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
Advertisement
quote: Original post by maccaroo
Thanks to both of you for the input. I think I get it mostly. From what anonymous said, it sounds like you are describing an inline function. There must be a lot of similarities and differences between macros and functions, but I''m sure they''ll sink in as I program more...


Actually, it''s not like an inline function at all since inline functions are usually typesafe and are functions, not macros. Inline functions aren''t processed by the pre-compile but by the compiler itself. They are actually 2 different concepts. Even though they tend to work similarly.



"And that''s the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
Thanks, it still sounds very similar to inline functions (they also get substituted into that point in the code). On a different topic, how important is assembler? ...in fact, i''ll start that as a new thread. Ta
jo|
i often notice macros in direct3d code. the same action
is done in my code by a short function. so, is there a need
of macros?? i mean you can replace every macro by a function
doing the same. or is there a performance difference??
are macros faster??

--cya felix
Here, let me try. A macro is not a function at all. The define statement tells the preprocessor to replace the HIWORD and LOWORD and other macros with what is specified. That is then compiled directly into the code. There is no function call happening at all. No parameter shifting, nothing.

Inline functions are function calls, so parameters need to be prepared(hard to explain without showin asm). They will not always be inlined, unless you choose to use __forceinline. Macros are usually used for simple one line things that replace a rather nasty mathematical expression and need to be called very fast and very often.

____________________________________________________________
Direct3D vs. OpenGL
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
I know it sounds like I''m pushing a point here... I''m not though. I understand what you guys say, and I''m not arguing, just trying to clear stuff in my head.

If you do use use__forceinline, and the inline function is put in there, is that slower than using a macro?
Spontaneous macro question: Do you NEED to have brackets around every variable in a macro? It seems excessive...

#define GETMAX(A,B) (((A)>(B))?(A)B))

or could you write it like this...

#define GETMAX(A,B) ((A>B)?A:B)

Woops! That's supposed to be , a colon then bracket, not a sad face!

[edited by - Fundy Glostna on April 12, 2002 10:15:20 AM]
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
No, it happened again!!!!
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
No, it happened again!!!!
No, Canadians DON'T all live in igloos. They live in one BIG igloo!

This topic is closed to new replies.

Advertisement