Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an __attribute__ specifier be used with both the function prototype and the function definition?

Tags:

c++

c

gcc

armcc

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?

like image 551
embedded_guy Avatar asked May 28 '14 16:05

embedded_guy


People also ask

What is the use of __ attribute __ in C?

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.

What is meant by function definition in c++?

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.

What is the difference between function prototype and function header?

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.

What is function declaration and prototype?

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.


2 Answers

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.

like image 118
Sean Perry Avatar answered Nov 15 '22 19:11

Sean Perry


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.

like image 20
embedded_guy Avatar answered Nov 15 '22 18:11

embedded_guy