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

📄 type-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="Type%20Attributes">Type Attributes</a>,Next:&nbsp;<a rel="next" accesskey="n" href="Alignment.html#Alignment">Alignment</a>,Previous:&nbsp;<a rel="previous" accesskey="p" href="Variable-Attributes.html#Variable%20Attributes">Variable Attributes</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 Types</h3><p>The keyword <code>__attribute__</code> allows you to specify specialattributes of <code>struct</code> and <code>union</code> types when you define suchtypes.  This keyword is followed by an attribute specification insidedouble 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 forfunctions (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 theseattributes in header files without being concerned about a possiblemacro 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 theclosing curly brace of a complete enum, struct or union type<em>definition</em> and the <code>packed</code> attribute only past the closingbrace of a definition.   <p>You may also specify attributes between the enum, struct or uniontag 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 usingattributes.     <dl><dt><code>aligned (</code><var>alignment</var><code>)</code>     <dd>This attribute specifies a minimum alignment (in bytes) for variablesof 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 whosetype is <code>struct S</code> or <code>more_aligned_int</code> will be allocated andaligned <em>at least</em> on a 8-byte boundary.  On a SPARC, having allvariables of type <code>struct S</code> aligned to 8-byte boundaries allowsthe compiler to use the <code>ldd</code> and <code>std</code> (doubleword load andstore) instructions when copying one variable of type <code>struct S</code> toanother, thus improving run-time efficiency.     <p>Note that the alignment of any given <code>struct</code> or <code>union</code> typeis required by the ISO C standard to be at least a perfect multiple ofthe lowest common multiple of the alignments of all of the members ofthe <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 membersof such a type, but the notation illustrated in the example above is amore obvious, intuitive, and readable way to request the compiler toadjust 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 factorand just ask the compiler to align a type to the maximumuseful alignment for the target machine you are compiling for.  Forexample, 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 alignmentfor the type to the largest alignment which is ever used for any datatype on the target machine you are compiling for.  Doing this can oftenmake copy operations more efficient, because the compiler can usewhatever instructions copy the biggest chunks of memory when performingcopies to or from the variables which have types that you have alignedthis way.     <p>In the example above, if the size of each <code>short</code> is 2 bytes, thenthe size of the entire <code>struct S</code> type is 6 bytes.  The smallestpower of two which is greater than or equal to that is 8, so thecompiler sets the alignment for the entire <code>struct S</code> type to 8bytes.     <p>Note that although you can ask the compiler to select a time-efficientalignment for a given type and then declare only individual stand-aloneobjects of that type, the compiler's ability to select a time-efficientalignment is primarily useful only when you plan to create arrays ofvariables having the relevant (efficiently aligned) type.  If youdeclare or use arrays of variables of an efficiently-aligned type, thenit is likely that your program will also be doing pointer arithmetic (orsubscripting, which amounts to the same thing) on pointers to therelevant type, and the code that the compiler generates for thesepointer arithmetic operations will often be more efficient forefficiently-aligned types than for other types.     <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>packed</code>     <dd>This attribute, attached to <code>struct</code> or <code>union</code> typedefinition, specifies that each member of the structure or union isplaced to minimize the memory required. When attached to an <code>enum</code>definition, it indicates that the smallest integral type should be used.     <p>Specifying this attribute for <code>struct</code> and <code>union</code> types isequivalent to specifying the <code>packed</code> attribute on each of thestructure 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>In the following example <code>struct my_packed_struct</code>'s members arepacked closely together, but the internal layout of its <code>s</code> memberis not packed - to do that, <code>struct my_unpacked_struct</code> would need tobe packed too.

⌨️ 快捷键说明

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