Thread: Inline Functions

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    Inline Functions

    I needed to make some wrappers for some functions in my program as my argument structure was different to that of the lib/functions that I had created. It has been suggested that the best way to do this is to use inline functions in header files, now this works fine for me:
    Code:
    /* In main.h */
    static inline void iReAllocList (struct functionParam argv)
    {
    	reAllocList(argv.lp, argv.allocSize, argv.allocMode);
    }
    However, I am not sure if this is valid C99 code (it works on GCC which is my main target compiler, but I would like to stick to valid code if possible) and if it is allowed in a header file. I also not sure if this is the best way to make a function wrapper.
    Can anyone give me any advice.

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    There is no language difference between .c and .h files; so anything that you can do in C you can do in a header file. Remember, #including a header file is equivalent to taking that file and pasting it's contents inserted at the #include line. So yeah, you can do that in a header file. Common practice is defining functions in .c files and declare (prototype) those functions in header files.

    GCC and inline is a bit tricky... C99 adds the inline keyword, but GCC supported it before that and in a somewhat different way. Start here maybe? http://www.greenend.org.uk/rjk/2003/03/inline.html

    I wouldn't worry about it though; a good optomizing compiler can figure out pretty well when to inline things, and it knows more about subtle machine-specifics for the particular target than you will writing standard code that compiles on many systems.
    hello, internet!

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How about:
    Code:
    #define iReAllocList(a) reAllocList((a).lp, (a).allocSize, (a).allocMode)
    Guaranteed to be inline every time
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Yes, that was the site that I came across when I was looking for information on inline functions and was not sure if the GCC way is C99 compliant. I put the wrapper there because it does not really do anything new, just allows me to use an array of structures with function pointers (and an identification char) and then by passing a structure with all of the functions arguments and letting the function choose which ones to use.

    Another beginners question, variable names, how do you C lot do them? I come from a Java/C# world where do you helloWorldBye but in C most people do hello_world_bye and what is with '_t' at the end of variable and structure names? One more question if I am allowed to, how do you deal with long lines e.g.:
    Code:
    argv.lp->list[argv.lp->realSize - 2] = pow(argv.lp->list[argv.lp->realSize - 2], popItem(argv.lp));
    Do I need to split the code onto multiple lines? As while I understand it I am sure that many other people will not (takes an item out of a managed list (popItem) and then raises the last item in the list to the power of that number).

    Thanks for all of your help and advice

    *edit*
    While I was typing up my reply itsme86 posted, and I like his way of doing it! It is smaller (code) simpler and will probably work on more compilers. Only problem is that I need to be able to get the memory address of the function (to put in an array of function pointers).
    Last edited by EvilGuru; 11-07-2005 at 04:08 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    There is no standard way to do variable names. Different people have different styles, and follow different notations. The '_t' at the end of some types probably indicates that it is a typedef. I think this is from hungarian notation, but I could be wrong here.

    A lot of librarys (especially on linux) use the hello_world_bye() function names, but on windows you would most likely see it as HelloWorldBye(). This is all up to the coder though.

    Do I need to split the code onto multiple lines?
    I think code like that fits just fine on one line. Of course a line of code like that also deserves a comment above it which tells the reader what the heck is going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  2. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  3. When to inline your *tors
    By Angus in forum C++ Programming
    Replies: 43
    Last Post: 10-29-2008, 03:38 PM
  4. conditional breakpoints and inline functions
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 08-10-2006, 08:30 PM
  5. inline friend functions.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:37 PM