Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
}
like image 821
TZPike05 Avatar asked Jun 12 '12 14:06

TZPike05


People also ask

What is a static inline?

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.

What is void inline function?

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.

What is inline void in C++?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

Why should inline function be static?

Conclusion. static marks the symbol LOCAL , which restricts the symbol in current translation unit, and the linker may keep multiple instances of (possibly different) definition. static inline generally works as static , but the inline keyword suggest compiler trying to inline this function.


1 Answers

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.

like image 127
Graham Borland Avatar answered Oct 10 '22 06:10

Graham Borland