Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ inline functions     - ORA-00020: No more process state objects available (Database)

- Ubuntu 14.04 / Linux Mint 17 How to install the MintMenu 5.5.2 menu (Linux)

- Start Linux ISO image directly from the hard disk (Linux)

- Python decorators to learn and practice the actual usage scenarios (Programming)

- Linux Kernel 4.2 Installation Instructions (Linux)

- Definition Format Oracle basis of various statements (Database)

- Oracle 11G using DG Broker create DataGuard (Database)

- Experts teach you how to identify the actual functional differences between the firewall (Linux)

- Installation Mesos + Marathon + Zookeeper under CentOS 7 (Server)

- Debian 7.6 install Nvidia graphics driver (Linux)

- Linux System Getting Started Tutorial: Five image processing software (Linux)

- CentOS 6.7 install Nagios Tutorials (Server)

- Oracle: RETURNING clause (Database)

- MongoDB common optimization settings in Linux (Database)

- Struts2 dynamic call DMI and error Solution (Programming)

- Graphics of Java Tools (Programming)

- Open log in Hibernate (Programming)

- JavaScript basic tools list (Programming)

- Spark parquet merge metadata problem (Server)

- Using Python multithreaded mistakes summary (Programming)

 
         
  C ++ inline functions
     
  Add Date : 2017-08-31      
         
         
         
  In the C language, we use the time to perform this function by means of a macro definition compiler optimization techniques to reduce the program, then in C ++, there is no same or better technical realization of it? The answer is yes, that is an inline function. One technique inline function as the compiler optimization means, in reducing the running time is very useful.

We will:

1. What is the inline functions

2. Why should I use inline functions

3. The advantages and disadvantages of inline function

4. When to use inline functions

These four aspects inline functions introduced.

What is the inline functions

Inline function is one of the enhanced features of C ++, the program used to reduce the running time. When the compiler inline functions received instructions to take place within the Union: the compiler will use the function definition body to replace the function call statement, this alternative behavior occurs at compile time rather than run the program phase.

It is noteworthy that, inline function inlining is only suggestion to the compiler, the compiler if you think it was taken in line with the recommendations depends on the function inline favorable conditions. How great body of the function, the compiler will ignore the inline function declaration, and the inline function as an ordinary function processing.

How to make function inlining

When you define a function, in front of the function keyword "inline" statement function, you can make the function is called inline function declaration.

E.g:

Class A
{
 Public:
    inline int add (int a, int b)
    {
      return (a + b);
    };
}

Class A
{
 Public:
    int add (int a, int b);
};

inline int A :: add (int a, int b)
{
  return (a + b);
}

Why should I use inline functions

      Sometimes we'll write some of the features of the specific function, the function body of these functions is not, contain very little execution statement. For example, in calculating the prime numbers from 1 to 1000 or less, we often use the square root operation makes narrow, then we will write a function:

int root (int n)
{
return (int) sqrt ((float) n);
}

The function then find within our range of primes can be written.

int prime (int n)
{
    int i;
    for (i = 2; i < = root (n); i ++)
    {
        if (n% i == 0)
return 0;
        return 1;
    }
}

      Of course, the root function in the loop is not a wise choice, but imagine, within the context of a program must frequently invoke a similar root function, which calls the function of expenses will be much: When faced with when an ordinary function call instruction, the program will save the current function of the execution site, the function of local variables and address of the function onto the stack, and then calls the new function will soon be loaded into memory, which is to be subjected to copy parameter values jump the memory address of the function to the calling function of position, execute the function code, the function return values stored procedure, before the function is executed when exhausted, and then get the calling, go back to perform that function, run time overhead is simply too much.
      C ++ inline functions provide an alternative function call program through inline declaration, the compiler replaces the function call first statement uses the function itself statement in the function call, and then compile the code after the replacement. Therefore, an inline function, the compiler does not need to jump to another memory address to perform a function call, no need to retain the data field function calls.

Advantages and disadvantages of inline function

    These are summarized by the following advantages and disadvantages you will probably understand why you want to use more inline function:

advantage:
1. It is by avoiding the overhead of a function call to improve the speed of your program.
2. When the function call occurs, it saves variable popped, push overhead.
3. It avoids the overhead of a function is executed after return to the original site.
4. To declare a function inline, you can put on the function definition in the header file.

Disadvantages:
1. Because the spreading code, inline function increases the volume of the executable program.
Expand 2.C ++ inline functions is compiled stage, which means that if you change the inline function took place, then you need to recompile the code.
3. When you put inline functions in header files, it will make your header information increases, but users do not care about these header files.
4. Sometimes inline function is not favored, such as embedded systems, embedded systems storage constraints may not allow a large volume of executable programs.

When to use inline functions

When programming required, each function can be declared as inline. Here are some tips:
1. When the program execution performance is required, then use it inline functions.
2. When you want to define a macro function, use it decisively now inline functions.
3. inside the class defined function defaults declared inline function, which is conducive to the class implementation details hidden.

key point
1. inline declaration is only a suggestion to the compiler, the compiler whether to adopt the measures inline by the compiler to decide. Even in the compilation phase or link phase, some do not declare inline functions The compiler will also inline expansion it.
2. inlining compiler code looks like copy and paste, which is very different from preprocessor macros: macros are mandatory inline expansion, may be contaminated with all namespace code, the program will be debugging difficult.
3. All the functions defined in the class are declared as inline functions default, all we do not have to explicitly declare inline.
4. inline virtual functions are not allowed.
5. Although the template function put in a header file, but they are not necessarily inline. (Not to say that the function is defined in the header file are inline functions).
     
         
         
         
  More:      
 
- Iptables Instructions (Linux)
- How to add a new hard disk without restarting the CentOS 7 / RHEL 7 virtual machine (Linux)
- Linux performance monitoring and common commands Introduction (Linux)
- Using Android Studio integrated development environment to build Android (Linux)
- XP virtual machine under VirtualBox solve occupy 100% CPU problem (Linux)
- Fedora 21 setting boot script (Linux)
- Vi editor Introduction (Linux)
- Ubuntu update bug fixes Daquan (Linux)
- Grub2 Boots the openSUSE installation image (Linux)
- Android Touch message passing mechanism analysis (Programming)
- How MySQL tracking sql statement (Database)
- Kali Linux virtualbox rc = Error 1908 workaround (Linux)
- Ten correct use Redis skills (Database)
- Kubuntu 14.04 desktop to the user how to upgrade KDE 4.13.2 (Linux)
- ORA-00845: MEMORY_TARGET not supported on this system Problem (Database)
- Nginx logging client ip (Server)
- SecureCRT session buffer size settings (Linux)
- Android Studio Installation and Configuration Guide tutorial (Linux)
- How to modify the Ubuntu terminal user name changed to red color (Linux)
- Use top to monitor the remote server (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.