⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 type-attributes.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>Using the GNU Compiler Collection (GCC)</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Using the GNU Compiler Collection (GCC)">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,

1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.2 or

any later version published by the Free Software Foundation; with the

Invariant Sections being "GNU General Public License" and "Funding

Free Software", the Front-Cover texts being (a) (see below), and with

the Back-Cover Texts being (b) (see below).  A copy of the license is

included in the section entitled "GNU Free Documentation License".



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development.-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Type%20Attributes">Type Attributes</a>,

Next:<a rel="next" accesskey="n" href="Alignment.html#Alignment">Alignment</a>,

Previous:<a rel="previous" accesskey="p" href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>,

Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>

<hr><br>

</div>



<h3 class="section">Specifying Attributes of Types</h3>



   <p>The keyword <code>__attribute__</code> allows you to specify special

attributes of <code>struct</code> and <code>union</code> types when you define such

types.  This keyword is followed by an attribute specification inside

double parentheses.  Six attributes are currently defined for types:

<code>aligned</code>, <code>packed</code>, <code>transparent_union</code>, <code>unused</code>,

<code>deprecated</code> and <code>may_alias</code>.  Other attributes are defined for

functions (see <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>) and for variables

(see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>).



   <p>You may also specify any one of these attributes with <code>__</code>

preceding and following its keyword.  This allows you to use these

attributes in header files without being concerned about a possible

macro of the same name.  For example, you may use <code>__aligned__</code>

instead of <code>aligned</code>.



   <p>You may specify the <code>aligned</code> and <code>transparent_union</code>

attributes either in a <code>typedef</code> declaration or just past the

closing curly brace of a complete enum, struct or union type

<em>definition</em> and the <code>packed</code> attribute only past the closing

brace of a definition.



   <p>You may also specify attributes between the enum, struct or union

tag and the name of the type rather than after the closing brace.



   <p>See <a href="Attribute-Syntax.html#Attribute%20Syntax">Attribute Syntax</a>, for details of the exact syntax for using

attributes.



     <dl>

<dt><code>aligned (</code><var>alignment</var><code>)</code>

     <dd>This attribute specifies a minimum alignment (in bytes) for variables

of the specified type.  For example, the declarations:



     <pre class="smallexample">          struct S { short f[3]; } __attribute__ ((aligned (8)));

          typedef int more_aligned_int __attribute__ ((aligned (8)));

          </pre>



     <p>force the compiler to insure (as far as it can) that each variable whose

type is <code>struct S</code> or <code>more_aligned_int</code> will be allocated and

aligned <em>at least</em> on a 8-byte boundary.  On a SPARC, having all

variables of type <code>struct S</code> aligned to 8-byte boundaries allows

the compiler to use the <code>ldd</code> and <code>std</code> (doubleword load and

store) instructions when copying one variable of type <code>struct S</code> to

another, thus improving run-time efficiency.



     <p>Note that the alignment of any given <code>struct</code> or <code>union</code> type

is required by the ISO C standard to be at least a perfect multiple of

the lowest common multiple of the alignments of all of the members of

the <code>struct</code> or <code>union</code> in question.  This means that you <em>can</em>

effectively adjust the alignment of a <code>struct</code> or <code>union</code>

type by attaching an <code>aligned</code> attribute to any one of the members

of such a type, but the notation illustrated in the example above is a

more obvious, intuitive, and readable way to request the compiler to

adjust the alignment of an entire <code>struct</code> or <code>union</code> type.



     <p>As in the preceding example, you can explicitly specify the alignment

(in bytes) that you wish the compiler to use for a given <code>struct</code>

or <code>union</code> type.  Alternatively, you can leave out the alignment factor

and just ask the compiler to align a type to the maximum

useful alignment for the target machine you are compiling for.  For

example, you could write:



     <pre class="smallexample">          struct S { short f[3]; } __attribute__ ((aligned));

          </pre>



     <p>Whenever you leave out the alignment factor in an <code>aligned</code>

attribute specification, the compiler automatically sets the alignment

for the type to the largest alignment which is ever used for any data

type on the target machine you are compiling for.  Doing this can often

make copy operations more efficient, because the compiler can use

whatever instructions copy the biggest chunks of memory when performing

copies to or from the variables which have types that you have aligned

this way.



     <p>In the example above, if the size of each <code>short</code> is 2 bytes, then

the size of the entire <code>struct S</code> type is 6 bytes.  The smallest

power of two which is greater than or equal to that is 8, so the

compiler sets the alignment for the entire <code>struct S</code> type to 8

bytes.



     <p>Note that although you can ask the compiler to select a time-efficient

alignment for a given type and then declare only individual stand-alone

objects of that type, the compiler's ability to select a time-efficient

alignment is primarily useful only when you plan to create arrays of

variables having the relevant (efficiently aligned) type.  If you

declare or use arrays of variables of an efficiently-aligned type, then

it is likely that your program will also be doing pointer arithmetic (or

subscripting, which amounts to the same thing) on pointers to the

relevant type, and the code that the compiler generates for these

pointer arithmetic operations will often be more efficient for

efficiently-aligned types than for other types.



     <p>The <code>aligned</code> attribute can only increase the alignment; but you

can decrease it by specifying <code>packed</code> as well.  See below.



     <p>Note that the effectiveness of <code>aligned</code> attributes may be limited

by inherent limitations in your linker.  On many systems, the linker is

only able to arrange for variables to be aligned up to a certain maximum

alignment.  (For some linkers, the maximum supported alignment may

be very very small.)  If your linker is only able to align variables

up to a maximum of 8 byte alignment, then specifying <code>aligned(16)</code>

in an <code>__attribute__</code> will still only provide you with 8 byte

alignment.  See your linker documentation for further information.



     <br><dt><code>packed</code>

     <dd>This attribute, attached to an <code>enum</code>, <code>struct</code>, or

<code>union</code> type definition, specified that the minimum required memory

be used to represent the type.



     <p>Specifying this attribute for <code>struct</code> and <code>union</code> types is

equivalent to specifying the <code>packed</code> attribute on each of the

structure or union members.  Specifying the <code>-fshort-enums</code>

flag on the line is equivalent to specifying the <code>packed</code>

attribute on all <code>enum</code> definitions.



     <p>You may only specify this attribute after a closing curly brace on an

<code>enum</code> definition, not in a <code>typedef</code> declaration, unless that

declaration also contains the definition of the <code>enum</code>.



     <br><dt><code>transparent_union</code>

     <dd>This attribute, attached to a <code>union</code> type definition, indicates

that any function parameter having that union type causes calls to that

function to be treated in a special way.



     <p>First, the argument corresponding to a transparent union type can be of

any type in the union; no cast is required.  Also, if the union contains

a pointer type, the corresponding argument can be a null pointer

constant or a void pointer expression; and if the union contains a void

pointer type, the corresponding argument can be any pointer expression. 

If the union member type is a pointer, qualifiers like <code>const</code> on

the referenced type must be respected, just as with normal pointer

conversions.



     <p>Second, the argument is passed to the function using the calling

conventions of first member of the transparent union, not the calling

conventions of the union itself.  All members of the union must have the

same machine representation; this is necessary for this argument passing

to work properly.



     <p>Transparent unions are designed for library functions that have multiple

interfaces for compatibility reasons.  For example, suppose the

<code>wait</code> function must accept either a value of type <code>int *</code> to

comply with Posix, or a value of type <code>union wait *</code> to comply with

the 4.1BSD interface.  If <code>wait</code>'s parameter were <code>void *</code>,

<code>wait</code> would accept both kinds of arguments, but it would also

accept any other pointer type and this would make argument type checking

less useful.  Instead, <code>&lt;sys/wait.h&gt;</code> might define the interface

as follows:



     <pre class="smallexample">          typedef union

            {

              int *__ip;

              union wait *__up;

            } wait_status_ptr_t __attribute__ ((__transparent_union__));

          

          pid_t wait (wait_status_ptr_t);

          </pre>



     <p>This interface allows either <code>int *</code> or <code>union wait *</code>

arguments to be passed, using the <code>int *</code> calling convention. 

The program can call <code>wait</code> with arguments of either type:



     <pre class="example">          int w1 () { int w; return wait (&amp;w); }

          int w2 () { union wait w; return wait (&amp;w); }

          </pre>



     <p>With this interface, <code>wait</code>'s implementation might look like this:



     <pre class="example">          pid_t wait (wait_status_ptr_t p)

          {

            return waitpid (-1, p.__ip, 0);

          }

          </pre>



     <br><dt><code>unused</code>

     <dd>When attached to a type (including a <code>union</code> or a <code>struct</code>),

this attribute means that variables of that type are meant to appear

possibly unused.  GCC will not produce a warning for any variables of

that type, even if the variable appears to do nothing.  This is often

the case with lock or thread classes, which are usually defined and then

not referenced, but contain constructors and destructors that have

nontrivial bookkeeping functions.



     <br><dt><code>deprecated</code>

     <dd>The <code>deprecated</code> attribute results in a warning if the type

is used anywhere in the source file.  This is useful when identifying

types that are expected to be removed in a future version of a program. 

If possible, the warning also includes the location of the declaration

of the deprecated type, to enable users to easily find further

information about why the type is deprecated, or what they should do

instead.  Note that the warnings only occur for uses and then only

if the type is being applied to an identifier that itself is not being

declared as deprecated.



     <pre class="smallexample">          typedef int T1 __attribute__ ((deprecated));

          T1 x;

          typedef T1 T2;

          T2 y;

          typedef T1 T3 __attribute__ ((deprecated));

          T3 z __attribute__ ((deprecated));

          </pre>



     <p>results in a warning on line 2 and 3 but not lines 4, 5, or 6.  No

warning is issued for line 4 because T2 is not explicitly

deprecated.  Line 5 has no warning because T3 is explicitly

deprecated.  Similarly for line 6.



     <p>The <code>deprecated</code> attribute can also be used for functions and

variables (see <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>, see <a href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</a>.)



     <br><dt><code>may_alias</code>

     <dd>Accesses to objects with types with this attribute are not subjected to

type-based alias analysis, but are instead assumed to be able to alias

any other type of objects, just like the <code>char</code> type.  See

<code>-fstrict-aliasing</code> for more information on aliasing issues.



     <p>Example of use:



     <pre class="smallexample">          typedef short __attribute__((__may_alias__)) short_a;

          

          int

          main (void)

          {

            int a = 0x12345678;

            short_a *b = (short_a *) &amp;a;

          

            b[1] = 0;

          

            if (a == 0x12345678)

              abort();

          

            exit(0);

          }

          </pre>



     <p>If you replaced <code>short_a</code> with <code>short</code> in the variable

declaration, the above program would abort when compiled with

<code>-fstrict-aliasing</code>, which is on by default at <code>-O2</code> or

above in recent GCC versions. 

</dl>



   <p>To specify multiple attributes, separate them by commas within the

double parentheses: for example, <code>__attribute__ ((aligned (16),

packed))</code>.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -