📄 variable-attributes.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 © 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="Variable%20Attributes">Variable Attributes</a>,
Next:<a rel="next" accesskey="n" href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>,
Previous:<a rel="previous" accesskey="p" href="Character-Escapes.html#Character%20Escapes">Character Escapes</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 Variables</h3>
<p>The keyword <code>__attribute__</code> allows you to specify special
attributes of variables or structure fields. This keyword is followed
by an attribute specification inside double parentheses. Ten
attributes are currently defined for variables: <code>aligned</code>,
<code>mode</code>, <code>nocommon</code>, <code>packed</code>, <code>section</code>,
<code>transparent_union</code>, <code>unused</code>, <code>deprecated</code>,
<code>vector_size</code>, and <code>weak</code>. Some other attributes are defined
for variables on particular target systems. Other attributes are
available for functions (see <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>) and for types
(see <a href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>). Other front ends might define more
attributes (see <a href="C---Extensions.html#C++%20Extensions">Extensions to the C++ Language</a>).
<p>You may also specify attributes with <code>__</code> preceding and following
each keyword. This allows you to use them 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>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 for the variable or
structure field, measured in bytes. For example, the declaration:
<pre class="smallexample"> int x __attribute__ ((aligned (16))) = 0;
</pre>
<p>causes the compiler to allocate the global variable <code>x</code> on a
16-byte boundary. On a 68040, this could be used in conjunction with
an <code>asm</code> expression to access the <code>move16</code> instruction which
requires 16-byte aligned operands.
<p>You can also specify the alignment of structure fields. For example, to
create a double-word aligned <code>int</code> pair, you could write:
<pre class="smallexample"> struct foo { int x[2] __attribute__ ((aligned (8))); };
</pre>
<p>This is an alternative to creating a union with a <code>double</code> member
that forces the union to be double-word aligned.
<p>As in the preceding examples, you can explicitly specify the alignment
(in bytes) that you wish the compiler to use for a given variable or
structure field. Alternatively, you can leave out the alignment factor
and just ask the compiler to align a variable or field to the maximum
useful alignment for the target machine you are compiling for. For
example, you could write:
<pre class="smallexample"> short array[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 declared
variable or field 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 or fields that you have aligned this way.
<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>mode (</code><var>mode</var><code>)</code>
<dd>This attribute specifies the data type for the declaration--whichever
type corresponds to the mode <var>mode</var>. This in effect lets you
request an integer or floating point type according to its width.
<p>You may also specify a mode of <code>byte</code> or <code>__byte__</code> to
indicate the mode corresponding to a one-byte integer, <code>word</code> or
<code>__word__</code> for the mode of a one-word integer, and <code>pointer</code>
or <code>__pointer__</code> for the mode used to represent pointers.
<br><dt><code>nocommon</code>
<dd>This attribute specifies requests GCC not to place a variable
"common" but instead to allocate space for it directly. If you
specify the <code>-fno-common</code> flag, GCC will do this for all
variables.
<p>Specifying the <code>nocommon</code> attribute for a variable provides an
initialization of zeros. A variable may only be initialized in one
source file.
<br><dt><code>packed</code>
<dd>The <code>packed</code> attribute specifies that a variable or structure field
should have the smallest possible alignment--one byte for a variable,
and one bit for a field, unless you specify a larger value with the
<code>aligned</code> attribute.
<p>Here is a structure in which the field <code>x</code> is packed, so that it
immediately follows <code>a</code>:
<pre class="example"> struct foo
{
char a;
int x[2] __attribute__ ((packed));
};
</pre>
<br><dt><code>section ("</code><var>section-name</var><code>")</code>
<dd>Normally, the compiler places the objects it generates in sections like
<code>data</code> and <code>bss</code>. Sometimes, however, you need additional sections,
or you need certain particular variables to appear in special sections,
for example to map to special hardware. The <code>section</code>
attribute specifies that a variable (or function) lives in a particular
section. For example, this small program uses several specific section names:
<pre class="smallexample"> struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
int init_data __attribute__ ((section ("INITDATA"))) = 0;
main()
{
/* Initialize stack pointer */
init_sp (stack + sizeof (stack));
/* Initialize initialized data */
memcpy (&init_data, &data, &edata - &data);
/* Turn on the serial ports */
init_duart (&a);
init_duart (&b);
}
</pre>
<p>Use the <code>section</code> attribute with an <em>initialized</em> definition
of a <em>global</em> variable, as shown in the example. GCC issues
a warning and otherwise ignores the <code>section</code> attribute in
uninitialized variable declarations.
<p>You may only use the <code>section</code> attribute with a fully initialized
global definition because of the way linkers work. The linker requires
each object be defined once, with the exception that uninitialized
variables tentatively go in the <code>common</code> (or <code>bss</code>) section
and can be multiply "defined". You can force a variable to be
initialized with the <code>-fno-common</code> flag or the <code>nocommon</code>
attribute.
<p>Some file formats do not support arbitrary sections so the <code>section</code>
attribute is not available on all platforms.
If you need to map the entire contents of a module to a particular
section, consider using the facilities of the linker instead.
<br><dt><code>shared</code>
<dd>On Windows NT, in addition to putting variable definitions in a named
section, the section can also be shared among all running copies of an
executable or DLL. For example, this small program defines shared data
by putting it in a named section <code>shared</code> and marking the section
shareable:
<pre class="smallexample"> int foo __attribute__((section ("shared"), shared)) = 0;
int
main()
{
/* Read and write foo. All running
copies see the same value. */
return 0;
}
</pre>
<p>You may only use the <code>shared</code> attribute along with <code>section</code>
attribute with a fully initialized global definition because of the way
linkers work. See <code>section</code> attribute for more information.
<p>The <code>shared</code> attribute is only available on Windows NT.
<br><dt><code>transparent_union</code>
<dd>This attribute, attached to a function parameter which is a union, means
that the corresponding argument may have the type of any union member,
but the argument is passed as if its type were that of the first union
member. For more details see See <a href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>. You can also use
this attribute on a <code>typedef</code> for a union data type; then it
applies to all function parameters with that type.
<br><dt><code>unused</code>
<dd>This attribute, attached to a variable, means that the variable is meant
to be possibly unused. GCC will not produce a warning for this
variable.
<br><dt><code>deprecated</code>
<dd>The <code>deprecated</code> attribute results in a warning if the variable
is used anywhere in the source file. This is useful when identifying
variables that are expected to be removed in a future version of a
program. The warning also includes the location of the declaration
of the deprecated variable, to enable users to easily find further
information about why the variable is deprecated, or what they should
do instead. Note that the warnings only occurs for uses:
<pre class="smallexample"> extern int old_var __attribute__ ((deprecated));
extern int old_var;
int new_fn () { return old_var; }
</pre>
<p>results in a warning on line 3 but not line 2.
<p>The <code>deprecated</code> attribute can also be used for functions and
types (see <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>, see <a href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>.)
<br><dt><code>vector_size (</code><var>bytes</var><code>)</code>
<dd>This attribute specifies the vector size for the variable, measured in
bytes. For example, the declaration:
<pre class="smallexample"> int foo __attribute__ ((vector_size (16)));
</pre>
<p>causes the compiler to set the mode for <code>foo</code>, to be 16 bytes,
divided into <code>int</code> sized units. Assuming a 32-bit int (a vector of
4 units of 4 bytes), the corresponding mode of <code>foo</code> will be V4SI.
<p>This attribute is only applicable to integral and float scalars,
although arrays, pointers, and function return values are allowed in
conjunction with this construct.
<p>Aggregates with this attribute are invalid, even if they are of the same
size as a corresponding scalar. For example, the declaration:
<pre class="smallexample"> struct S { int a; };
struct S __attribute__ ((vector_size (16))) foo;
</pre>
<p>is invalid even if the size of the structure is the same as the size of
the <code>int</code>.
<br><dt><code>weak</code>
<dd>The <code>weak</code> attribute is described in See <a href="Function-Attributes.html#Function%20Attributes">Function Attributes</a>.
<br><dt><code>model (</code><var>model-name</var><code>)</code>
<dd>Use this attribute on the M32R/D to set the addressability of an object.
The identifier <var>model-name</var> is one of <code>small</code>, <code>medium</code>,
or <code>large</code>, representing each of the code models.
<p>Small model objects live in the lower 16MB of memory (so that their
addresses can be loaded with the <code>ld24</code> instruction).
<p>Medium and large model objects may live anywhere in the 32-bit address space
(the compiler will generate <code>seth/add3</code> instructions to load their
addresses).
</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 + -