Should a built-in function be defined before it is called?

The C language allows you to read the source files in one pass, without hesitation; at some point, the compiler should only consider declarations, prototypes, macro descriptions, etc., that appeared before its current position in the file.

Does this mean that calling a call function to the compiler may require the function to be defined before the call? For instance:

int foo(void); int bar(void) { return foo(); } inline int foo(void) { return 42; } 

Is it possible for the foo call to be inline if the inline definition is moved before the bar definition?

Do I have to organize inline definitions in my code so that they appear before the calls that should be inline best? Or can I just assume that any optimizing compiler advanced enough to make an attachment at all can find the definition even if it appears after the call (which is similar to gcc)?

EDIT: I noticed that in Pelles C with the /Ob1 really requires the definition to be visible before the call can be embedded. The compiler also offers the /Ob2 , which removes this restriction (and also allows the compiler to embed functions without a built-in specifier, similar to what gcc does), but the documentation states that using this second option may require much more memory.

+6
source share
1 answer

This should not make any difference in practice. Because its compiler chooses a built-in function or not, even if it explicitly pointed to inline . The compiler can also embed a function, even if it is defined using the inline .

First I ran your code with gcc 4.6.3 without any optimizations:

 $ gcc -fdump-ipa-inline test.c 

From the generated assembly, both foo and bar not inserted, although foo is inline. When I changed the definition of inline foo at the top, the compiler still didn't insert both.

Next, I did the same with -O3 :

 $ gcc -fdump-ipa-inline -O3 test.c 

Now both functions are built-in. Although only one has an inline ad.

Basically, the compiler can embed the function as it sees fit.

+2
source

Source: https://habr.com/ru/post/923424/


All Articles