Doubt about inline

This is a discussion on Doubt about inline within the C Programming forums, part of the General Programming Boards category; I have a program with same .c and .h files and I want to inline a function that is widely ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Doubt about inline

    I have a program with same .c and .h files and I want to inline a function that is widely used in a few .c files. As an inline function must be in the current file, the question are:
    - Must I duplicate the inline function code in differents .c files in order to inline in various places.
    - I have seen people sometimes put pure c code into .h file, so include that header in differents .c files avoidd the problem. I dont know if .h are designed for that, but sound to me like a wrong thing.

    (my inline function is around 10 lines long, but it use extensibely a lot of precomputed matrix declared, so another question is if I must duplicate all those tables).

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Kempelen View Post
    I have a program with same .c and .h files and I want to inline a function that is widely used in a few .c files. As an inline function must be in the current file, the question are:
    - Must I duplicate the inline function code in differents .c files in order to inline in various places.
    - I have seen people sometimes put pure c code into .h file, so include that header in differents .c files avoidd the problem. I dont know if .h are designed for that, but sound to me like a wrong thing.

    (my inline function is around 10 lines long, but it use extensibely a lot of precomputed matrix declared, so another question is if I must duplicate all those tables).
    Let the compiler do the work, lookup the compiler documentation on how to inline a function. All calls to it will now be automatically inlined by the compiler. But the tabular matrix needs to be defined in each caller so in this case it maybe better to call it as a regular function instead of inlining it, or globalize the table and then inline.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First... will your function work non-inline with static declarations?
    Code:
    int MyPredefinedValues(int x)
      { static int x;  // keep even out of scope
        static int y;
    
         // do something impressive here
    
        reutrn y; }
    Inlining the function would duplicate your tables, one copy for each time it's imbedded.

    Performance wise there's not a lot to gain by inlining with optimizing compilers so it's not like you can double your speed or anything like that.

    Generally it's bad ju-ju to put code in .h files... the compiler doesn't care but it effectively hides the code from anyone not expecting it.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    I have seen people sometimes put pure c code into .h file, so include that header in differents .c files avoidd the problem. I dont know if .h are designed for that, but sound to me like a wrong thing.
    If you have an interface used in a lot of places, it is correct to put it in an area where all of the users can get at it. In general, that code would be in a separate translation unit or compiled binary with an appropriate header. For code that may need to be inlined, that area is a header. It is isn't wrong.

    [Edit]
    so another question is if I must duplicate all those tables
    No, you probably need to declare those tables in the same interface header, as above, and define them in a translation unit.

    It depends on what types are involved and what you mean by "precomputed", but you may also be able to simply put the definition in the same header. Post an example of your code.
    [/Edit]

    First... will your function work non-inline with static declarations?
    Wait. How is that an alternative?

    [Edit]
    Inlining the function would duplicate your tables, one copy for each time it's imbedded.
    If the code is that large, the compiler will probably refuse to inline the definition.

    Anyway, he said that the inline function only used the tables; they may not even be local to the function.
    [/Edit]

    Soma
    Last edited by phantomotap; 04-26-2011 at 09:38 AM. Reason: none of your business

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with inline asm
    By master5001 in forum C Programming
    Replies: 8
    Last Post: 02-03-2005, 06:25 AM
  2. Inline: I don't get it
    By Epo in forum C++ Programming
    Replies: 17
    Last Post: 06-21-2004, 12:13 AM
  3. Inline ASM ???????
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 09-03-2002, 12:43 PM
  4. inline??
    By bman1176 in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2002, 05:31 PM

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