C Inline Functions and Memory Use

2020-07-11 05:49发布

If I use inline functions, does the memory usage increase?

8条回答
Lonely孤独者°
2楼-- · 2020-07-11 06:27

You program will in the general case become larger (I'm sure there are exceptions, though). The runtime memory consumption might go down, but not by much.

Why are you asking? Normally, you let the compiler determine whether a function should be inlined or not; it can usually make a better call given the size and complexity of the function.

查看更多
Viruses.
3楼-- · 2020-07-11 06:28

A function call requires several processor instructions.

You usually need a PUSH instruction for every argument to the function, a CALL instruction to call the function, and often another instruction that cleans up the stack after the function call.

Also, functions may modify the processor's registers, so the calling function may need more instructions to preserve registers or reload values that would otherwise still be in registers.

So if the function you're calling is just a few instructions long, inlining it can save memory and run faster.

That said, inlining is for when your profiler tells you that you should.

查看更多
神经病院院长
4楼-- · 2020-07-11 06:38

There are two kinds of memory usage that inline functions will affect:

code size — in general, inlining code will increase how much memory is used to load your program. This is because there will be multiple copies of the generated code scattered around your program. However, this isn't always true -- if your inlined function was only used once, there's little change, and if the inlined function is very small, you could get a net reduction in code size by removing the function call overhead. Also, the function may be reduced in size by the optimizer that's able to remove code that's not used in the particular inline invocation.

stack usage — If your inlined functions have lots of local variables, then you may use more stack space. In C, the compiler usually allocates the stack space for a function once upon entry to the function. This has to be large enough to hold all the local variables that aren't stored in registers. If you call a function out-of-line, the stack for that function is used until it returns, when it's released again. If you inline the function, then that stack space will remain used for the whole life of the uber-function.

Inlining won't affect heap usage, as the same allocations and deallocations would occur for the inlined code as would occur for the non-inlined version.

查看更多
▲ chillily
5楼-- · 2020-07-11 06:43

That is really un-answerable in the general case.

To start with you do not generally have control over the in-lining. Even if you mark a function inline it is actually still up to the compiler wither it will actually do the in-lining (it is just a hint).

The compiler will do its best to optimize the code; using in-lining is just one tool in doing this. So inlining short functions will make the code smaller (as you don't need to set up the parameters for the call or retrieve the return value. But even with long functions the answer is not absolute.

If the compiler decides to inline a long function then you would think the code would get longer. But this is not generally the case; as this gives the compiler extra opportunities to apply other optimization techniques that could potentially make the code still smaller. If the compilers analysis finds that the resulting code bloat is detrimental to the code the in-lining will not be done.

Basically the compiler does its analysis and decides and the best course of action.

Conclusion. Don't worry about it. The compiler is smarter than you and will do the correct thing.

查看更多
The star\"
6楼-- · 2020-07-11 06:48

It sometimes so happens that we have functions scattered all over the program.In this case a function call causes the program to jumps to the address of the function and come back when the function call terminates. This takes away some precious time.

The above problem can be resolved with the use of inline functions. This causes the compiler to call the code directly from the source. No new memory instruction set is created for the inline function code.

Although the inline declaration in c++ is free and occurs automatically when the function is defined in the declaration, in c it is restricted by the following rules ::

  1. In C, any function with internal linkage can be declared inline, but a function with external linkage is has restrictions on inline.

  2. If the inline keyword is used in the function declaration, then the function definition should be present in the same translation unit.

inline datatype function_name(arguments)

This code runs upto 30% fastert than a non inline function, the rest depending on the prcessor speed.

Now comes the strategy part. You may use inline functions at your will but keeping in mind that inline functions can take much less time to execute but they have a high memory occupancy on the run. Also the compiler has always an option to overlook your inline declaration if the code declared inline is abnormally large compared to the code size.

Inline declaration although destroys the order of evaluation, does not make the function internal. The function is still external.

查看更多
男人必须洒脱
7楼-- · 2020-07-11 06:49

Inline functions definitely increase the size of your final executable(or binary), because they will be "copy-pasted" whereever you call them.

查看更多
登录 后发表回答