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

📄 variable-attributes.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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.6"><!--Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.   <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below).  A copy of the license isincluded 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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!--  pre.display { font-family:inherit }  pre.format  { font-family:inherit }  pre.smalldisplay { font-family:inherit; font-size:smaller }  pre.smallformat  { font-family:inherit; font-size:smaller }  pre.smallexample { font-size:smaller }  pre.smalllisp    { font-size:smaller }--></style></head><body><div class="node"><p>Node:&nbsp;<a name="Variable%20Attributes">Variable Attributes</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Type-Attributes.html#Type%20Attributes">Type Attributes</a>,Previous:&nbsp;<a rel="previous" accesskey="p" href="Character-Escapes.html#Character%20Escapes">Character Escapes</a>,Up:&nbsp;<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 specialattributes of variables or structure fields.  This keyword is followedby an attribute specification inside double parentheses.  Someattributes are currently defined generically for variables. Other attributes are defined for variables on particular targetsystems.  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 followingeach keyword.  This allows you to use them in header files withoutbeing 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 usingattributes.     <dl><dt><code>aligned (</code><var>alignment</var><code>)</code>     <dd>This attribute specifies a minimum alignment for the variable orstructure 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 a16-byte boundary.  On a 68040, this could be used in conjunction withan <code>asm</code> expression to access the <code>move16</code> instruction whichrequires 16-byte aligned operands.     <p>You can also specify the alignment of structure fields.  For example, tocreate 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> memberthat 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 orstructure field.  Alternatively, you can leave out the alignment factorand just ask the compiler to align a variable or field to the maximumuseful alignment for the target machine you are compiling for.  Forexample, 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> attributespecification, the compiler automatically sets the alignment for the declaredvariable or field to the largest alignment which is ever used for any datatype on the target machine you are compiling for.  Doing this can often makecopy operations more efficient, because the compiler can use whateverinstructions copy the biggest chunks of memory when performing copies toor from the variables or fields that you have aligned this way.     <p>The <code>aligned</code> attribute can only increase the alignment; but youcan decrease it by specifying <code>packed</code> as well.  See below.     <p>Note that the effectiveness of <code>aligned</code> attributes may be limitedby inherent limitations in your linker.  On many systems, the linker isonly able to arrange for variables to be aligned up to a certain maximumalignment.  (For some linkers, the maximum supported alignment maybe very very small.)  If your linker is only able to align variablesup 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 bytealignment.  See your linker documentation for further information.     <br><dt><code>cleanup (</code><var>cleanup_function</var><code>)</code>     <dd>The <code>cleanup</code> attribute runs a function when the variable goesout of scope.  This attribute can only be applied to auto functionscope variables; it may not be applied to parameters or variableswith static storage duration.  The function must take one parameter,a pointer to a type compatible with the variable.  The return valueof the function (if any) is ignored.     <p>If <code>-fexceptions</code> is enabled, then <var>cleanup_function</var>will be run during the stack unwinding that happens during theprocessing of the exception.  Note that the <code>cleanup</code> attributedoes not allow the exception to be caught, only to perform an action. It is undefined what happens if <var>cleanup_function</var> does notreturn normally.     <br><dt><code>common</code>     <dd><dt><code>nocommon</code>     <dd>The <code>common</code> attribute requests GCC to place a variable in"common" storage.  The <code>nocommon</code> attribute requests theopposite - to allocate space for it directly.     <p>These attributes override the default chosen by the<code>-fno-common</code> and <code>-fcommon</code> flags respectively.     <br><dt><code>deprecated</code>     <dd>The <code>deprecated</code> attribute results in a warning if the variableis used anywhere in the source file.  This is useful when identifyingvariables that are expected to be removed in a future version of aprogram.  The warning also includes the location of the declarationof the deprecated variable, to enable users to easily find furtherinformation about why the variable is deprecated, or what they shoulddo instead.  Note that the warning 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 andtypes (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>mode (</code><var>mode</var><code>)</code>     <dd>This attribute specifies the data type for the declaration--whichevertype corresponds to the mode <var>mode</var>.  This in effect lets yourequest 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> toindicate 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>packed</code>     <dd>The <code>packed</code> attribute specifies that a variable or structure fieldshould 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

⌨️ 快捷键说明

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