Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does __attribute__ apply to all the variables in a declaration?

Tags:

c

gcc

Does the __attribute__ directive apply to all the members declared on one line?


int a, b, c; 

Declares three int variables.


int *a, b, c; 

Declares variable "a" as a pointer to int, and b and c as int.


int __attribute__((used)) a, b, c; 

Does the used attribute apply to all variables or only to a?

like image 637
jbl Avatar asked Jun 26 '15 07:06

jbl


People also ask

What does __ attribute __ mean 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.

Where is __ attribute __ defined?

The __attribute__ is added just after the variable name, and though it can appear unwieldy, it's a style you can get used to: int main(int argc __attribute__((unused)), char **argv) { ... Additional uses shown, each with a comment showing the compiler warning it might have generated.

What is __ attribute __ (( section?

__attribute__((section("name"))) variable attribute data and . bss . However, you might require additional data sections or you might want a variable to appear in a special section, for example, to map to special hardware. The section attribute specifies that a variable must be placed in a particular data section.

What are the attributes associated with a variable?

The keyword __attribute__ allows you to specify special attributes of variables or structure fields. This keyword is followed by an attribute specification inside double parentheses. Four attributes are currently defined for variables: aligned , mode , packed , and section .


2 Answers

From GCC: Attribute-Syntax:

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

__attribute__((noreturn)) void d0 (void), 
     __attribute__((format(printf, 1, 2))) d1 (const char *, ...),       d2 (void); 

the noreturn attribute applies to all the functions declared; the format attribute only applies to d1.


Correction: As the comment points out, my previous conclusion is incorrect. I didn't notice the other than the first part.

Modified conclusion:

In both

int __attribute__((used)) a, b, c; 

and

__attribute__((used)) int a, b, c; 

The attribute applies to all a, b, and c.

But if it were:

int a, __attribute__((used)) b, c; 

The attribute would apply to b only.

like image 125
Yu Hao Avatar answered Sep 30 '22 19:09

Yu Hao


gcc documentation (6.36 Attribute Syntax) says it only applies to the identifier before whose declarator they appear:

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

__attribute__((noreturn)) void d0 (void),      __attribute__((format(printf, 1, 2))) d1 (const char *, ...),       d2 (void); 

So in your example:

int __attribute__((used)) a, b, c; 

the attribute only applies to a.

like image 40
ouah Avatar answered Sep 30 '22 17:09

ouah