Thread: Normal function call...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Question Normal function call...

    i'm currently in cprogramming.com's c++ tutorial section and in the inline functions lesson...

    i read that when an inline function is called the compiler replaces the function call with the actual code...

    if that is how an inline function is called, then how is a normal function called?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address.

    EDIT: Or is the address pushed last? I don't remember...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tennisstar View Post
    i read that when an inline function is called the compiler replaces the function call with the actual code...
    That is a vast oversimplification.

    When inlining a function call, the compiler still needs to take steps to preserve scope. For example, if a function changes an argument that has been passed by value, that change should not propagate to the caller.

    The starting point for inlining a function is - conceptually at least - the means of calling it without inlining (because the semantics of the function should not change by inlining it). The difference is that, since the compiler has visibility of code in both the caller and callee, it can eliminate unnecessary steps (eg pushing values to a stack before, popping them off later, clearing and resetting registers, shadowing of variables, etc).

    Note also that a programmer declaring a function inline (or defining it inline, say within a class definition) is only a hint to the compiler. A compiler may ignore that hint. Modern compilers often do ignore that hint because they can do a better job of spotting and exploiting opportunities for inlining than a typical programmer can - and because, depending on how the compiler works, not all functions can be inlined anyway. Similarly, modern compilers (or even linkers) can elect to inline a function, even if they haven't been asked to.


    Quote Originally Posted by GReaper View Post
    Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address.

    EDIT: Or is the address pushed last? I don't remember...
    What you've described (or attempted to describe) is specific to one compiler. It is not general to all compilers.

    The details of how inlining is achieved (or not, as the case may be) are part of the "secret sauce" of most compilers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GReaper View Post
    Briefly, the next instruction's address is pushed on the stack, then the passed arguments are pushed onto the stack and finally the execution jumps to where the function code is. When finished, execution jumps right after the call, thanks to that previously pushed address.

    EDIT: Or is the address pushed last? I don't remember...
    In x86 assembly, the call function pushes the address to the stack before jumping, so it would be pushed last.
    Just to point out to the curious. This is, after all, compiler and platform specific.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. Normal maps: Get normal x/y/z from color
    By Devils Child in forum Game Programming
    Replies: 2
    Last Post: 08-09-2009, 12:01 PM
  4. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  5. Replies: 2
    Last Post: 06-21-2005, 02:41 PM