question explanation help

most common reason for using a macro instead of a function?

Page 1 of 1

3 Replies - 840 Views - Last Post: 31 January 2010 - 07:37 PM Rate Topic: -----

#1 btee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 09-January 10

question explanation help

Posted 31 January 2010 - 04:10 PM

I am trying to get some explanation on this problem.

the most common reason for using a macro instead of a function is:
A. save storage space
B. to reduce the possibility of errors
C. to make the program run faster
d. all of the above

I chose A because the only thing my book stated was that macro utilized less memory allocation and de-allocation.
the correct answer was C.

**Mod Edit: Removed code tags. You really only need them for code. Also added topic description**

This post has been edited by OliveOyl3471: 31 January 2010 - 04:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: question explanation help

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: question explanation help

Posted 31 January 2010 - 05:23 PM

View Postbtee, on 31 Jan, 2010 - 11:10 PM, said:

I chose A because the only thing my book stated was that macro utilized less memory allocation and de-allocation.
the correct answer was C.
Personally I think your book is misleading you with that answer - its a gross oversimplification of something which really needs to be handled with care. I would choose option 'E' "None of the above" - because none of those answers alone are terribly good reasons for using a macro; their use is far more situational than simply ticking a yes/no box.

a macro is capable of generating code at compile time - (similar to metaprogramming i.e it works in a manner almost identical to copy + paste). In general, if you can use a function for a particular task, then your immediate preference should be to use a function - the argument about using a macro for speed is rubbish, because for a trivial function, your compiler is likely to inline it for you anyway, but also because removing a couple of stack operations is unlikely to have any noticable impact.

Macros can sometimes be useful in situations to tidy up a bit of ugly code where where perhaps variable and function names are necessarily duplicated, but for the sake of writing a clean interface, a macro can do that pre-compile-time duplication for you.
there are a few macro examples here http://gcc.gnu.org/o...cpp/Macros.html - The 'pitfalls' page is especially worth a read.

This post has been edited by Bench: 31 January 2010 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: question explanation help

Posted 31 January 2010 - 05:32 PM

for an example of where macros can make horribly error-prone code, look at this. What statement does the else belong to?
#define DODGY_MACRO( x )        \
    if( x == 0 )                \
        printf("%s is zero", #x );

int main ()
{
    int a = 0;
    int b = 1;

    if( a == 0 )
        DODGY_MACRO( b )
    else
        printf("a is not zero");
} 
had DODGY_MACRO been a function, the program would have displayed more predictable behaviour.

This post has been edited by Bench: 31 January 2010 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: question explanation help

Posted 31 January 2010 - 07:37 PM

Macros can be handy where you want to call a function with fewer arguments. For example, I commonly use a macro:
#define zeromem(var) memset(&var, 0, sizeof(var))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1