Static inline in C language

Source: Internet
Author: User

Today began to look at Redis's source code, which declares the following function in the simplest data structure SDS (Simple Dynamic String) header file

Static inline size_t Sdslen (const SDS s) {
    struct SDSHDR *sh = (void*) (s (sizeof (struct)));
    Return sh->len;
}

See this code stunned a bit, before always thought inline this keyword intelligence in C + + use, did not think here also use .... Google's ...

Http://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function

Inline instructs the compiler to attempt to embed the function content into the calling code instead of executing a Actua L Call.

For small functions this are called frequently that can make a big performance difference.

However, this are only a "hint", and the compiler may ignore it, and most compilers'll try to "inline" even when the KEYW Ord is isn't used, as part of the optimizations, where its possible.
Translation: Inline this keyword will give the compiler such a suggestion: do not make the actual function call and just insert the code into the place where the call. For shorter functions, two different ways of dealing with frequent calls can result in significant performance differences. However, this keyword is just a "hint" for the compiler, which is a hint. The compiler might ignore it and "inline" some functions even without the keyword inline in it.


In C, static is mainly applied to a specified range, and static means that the variable or function can only be used in the same "translation unit", what does this translation unit mean? Look at the wiki ...

This article is about the C programming term. For the linguistic meaning of the term, and the translation unit.
In C programming language terminology, a translation of the unit are the ultimate input to a C compiler from which a object file is generated. [1] in casual usage It's sometimes referred to as a compilation unit. A translation unit roughly consists of a source file over it has been processed by the "C preprocessor, meaning that Heade R files listed in #include directives are literally included, sections of code within #ifdef May is included, and macros H Ave been expanded.
Yes, another translation: in C language terminology, "translation Unit" refers to a target file to generate the required "ultimate input", that is, the input file required to produce this target file. Under normal circumstances, the "translation Unit" is also called "compliation", which is also the compilation module. A "translation unit" probably contains source files that have been processed by the C preprocessor, which means that the files in #include are included in the recent, #ifdef也会被包含近来, macros.

In this way, a compilation unit typically contains source files, as well as other recent files contained in the source file, the recent files of the #include mentioned above, #ifdef近来的文件, and macros.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.