My question directly pertains to the __attribute__((noreturn))
but more generally could pertain to others as well - such as __attribute__(noinline)
. I have looked at both the gcc manual and the Keil compiler reference guide to determine what the proper syntax is for using __attribute__
with a function. What I have generally seen is the following:
void function (void) __attribute__((noreturn)); //Prototype has __attribute__
void function (void) //Definition does not.
{
while (1);
}
I have also seen the __attribute__
used before the function definition as follows:
__attribute__((noreturn)) void function (void)
{
while (1);
}
However, I have not seen an example of it used with both the function prototype and the function definition. I think having the __attribute__
in both locations would result in better code readability; I would know by looking at either the function prototype or the definition that an attribute has been applied. The result would be as follows:
__attribute__((noreturn)) void function (void) ; //Prototype has __attribute__
__attribute__((noreturn)) void function (void) //Definition has __attribute__
{ //as well.
while (1);
}
I have successfully compiled code with the Keil armcc compiler using my aforementioned method. Is there any reason why I should not use this method with either armcc or gcc?
The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.
A function is a block of code that performs some operation. A function can optionally define input parameters that enable callers to pass arguments into the function. A function can optionally return a value as output.
The only difference between the function prototype and the function header is a semicolon (see diagram below). The function definition is placed AFTER the end of the int main(void) function. The function definition consists of the function header and its body.
A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
Here is a snippet from the GCC 4.0 docs available here.
The keyword __attribute__ allows you to specify special attributes when making a
declaration.
Note it says 'declaration' not 'definition'. This older Unix Wiz article also has lots of good advice. It also says to use attributes in declarations.
As Sean Perry says, it would appear that GCC only specifies that special attributes can be used with declarations.
I was digging some more into the ARMCC docs and finally found what I was looking for here:
You can set these function attributes in the declaration, the definition, or both.
So, for ARMCC my use of __attribute__
as shown in the OP is safe, but that is not true for GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With