What's the difference between static inline void and void?

TZPike05 picture TZPike05 · Jun 12, 2012 · Viewed 31.2k times · Source

I'm working in the C language and modifying code previously written by someone else. I'm struggling with a few things and I'm trying to understand as much as I can about what is going on as I can. So, as my question stated, what is the difference between static inline void and void when creating a function? I apologize in advance for the long post, but I wanted you to know I did do some research, but don't understand what I've found.

I found an explanation of static that confuses me:

The static specifier signifies that the function cannot be referenced from other files; that is, the name is not exported by the linker.

By reading this, I'm assuming referencing a function is different than calling a function? I assume that because this function is called from another .c file. If that is the case, what is referencing a function?

Through the same website, they explain inline functions and I don't understand what it means.

The __inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler's discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.

Huh???

Any help is greatly appreciated, and I once again apologize for the terribly long post.

The following is located in file1.c (Using generic names as I don't think it matters)

COMPLEX cNoiseSample;
CGauss( &cNoiseSample, loopbackRadio->pState );

The following is located in file2.c

static inline void CGauss( COMPLEX * pcGauss, P_OS_UNIFORM_RAND_STATE pState )
{
    //code
}

Answer

Graham Borland picture Graham Borland · Jun 12, 2012

static means it can't be referenced from another compilation unit (source file). "Referenced" means called, or otherwise referred to by name, e.g. assigned to a function pointer.

inline is a hint to the compiler that the function's code should be generated inline at the place it is called, rather than generated as a separate function to be branched to. This is normally done for performance reasons. To deal with Microsoft's quote:

the compiler does not inline a function if its address is taken or if it is too large to inline.

An inlined function has no address, since it doesn't exist as a separate entity. Its code is just intertwined seamlessly with the code it's called from. So, if you take the address of a function (e.g. to assign to a pointer) then the compiler has to generate it as a real function, and cannot inline it.

void means the function does not return a value.


Having looked at your code sample, I'd guess that there is a separate definition of CGauss() somewhere, which is being called from file1.c, whereas file2.c is calling its own private version. Either that, or file1.c is #includeing file2.c. Which would be nasty.