Macros Vs Inline functions

This is a discussion on Macros Vs Inline functions within the C++ Programming forums, part of the General Programming Boards category; Hello, Could someone explain which of the two: Macros or Inline functions produces smaller sized object code and why? Thanks! ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Macros Vs Inline functions

    Hello,

    Could someone explain which of the two: Macros or Inline functions produces smaller sized object code and why?

    Thanks!

    Passionate Guy

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    depends on your compiler.

    Macros are bad.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Read this:
    Inline functions and macros...
    "they are different in some important ways. For example, since inline functions are part of the language, it is generally possible to step through them in a debugger, whereas macros are notoriously difficult to debug. Also, macro parameters that have side effects can cause surprises if the parameter appears more than once (or not at all) in the body of the macro, whereas inline functions don't have these problems. Finally, macros are always expanded, but inline functions aren't always inlined.
    "
    I think of macros as a simple text replace during preprocessing phase of compilation.
    Look at this example of using macros:
    Code:
    #include <iostream>
    using namespace std;
    #define square(x) x*x
    
    int main()
    {
    	int x = 10;
    	int a = 3, b = 5;
    	cout<< square(x)<<endl;
    	cout<< square(a+b)<<endl;
    	return 0;
    }
    Result is not what you expect is it?
    Solution to this problem is using:
    Code:
    #define square(x) (x)*(x)
    And when you need to take care of such things every time, it is very easy to make mistake.

    Cheers

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thanks Micko for the detailed example. I understand the vulnerabilities of using Macros.

    But as major_small has suggested even I guess that the size of object code will vary depending on the compiler.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Inline has a tendency to make code bigger, not smaller.

    You're saving the cost of a function call by pasting the context of the function directly into the caller. If you call an inline function a lot, that's going to bloat the exe with lots of near-identical code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline empty polymorphic functions
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-16-2009, 11:12 AM
  2. Inline functions and passing by value
    By gentinex in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2006, 06:53 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21