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

📄 type-attributes.html

📁 自己收集的linux入门到学懂高级编程书集 包括linux程序设计第三版
💻 HTML
📖 第 1 页 / 共 2 页
字号:
     <pre class="smallexample">          struct my_unpacked_struct           {              char c;              int i;           };                    struct my_packed_struct __attribute__ ((__packed__))            {               char c;               int  i;               struct my_unpacked_struct s;            };          </pre>     <p>You may only specify this attribute on the definition of a <code>enum</code>,<code>struct</code> or <code>union</code>, not on a <code>typedef</code> which does notalso define the enumerated type, structure or union.     <br><dt><code>transparent_union</code>     <dd>This attribute, attached to a <code>union</code> type definition, indicatesthat any function parameter having that union type causes calls to thatfunction to be treated in a special way.     <p>First, the argument corresponding to a transparent union type can be ofany type in the union; no cast is required.  Also, if the union containsa pointer type, the corresponding argument can be a null pointerconstant or a void pointer expression; and if the union contains a voidpointer type, the corresponding argument can be any pointer expression. If the union member type is a pointer, qualifiers like <code>const</code> onthe referenced type must be respected, just as with normal pointerconversions.     <p>Second, the argument is passed to the function using the callingconventions of the first member of the transparent union, not the callingconventions of the union itself.  All members of the union must have thesame machine representation; this is necessary for this argument passingto work properly.     <p>Transparent unions are designed for library functions that have multipleinterfaces for compatibility reasons.  For example, suppose the<code>wait</code> function must accept either a value of type <code>int *</code> tocomply with Posix, or a value of type <code>union wait *</code> to comply withthe 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 alsoaccept any other pointer type and this would make argument type checkingless useful.  Instead, <code>&lt;sys/wait.h&gt;</code> might define the interfaceas 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="smallexample">          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="smallexample">          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 appearpossibly unused.  GCC will not produce a warning for any variables ofthat type, even if the variable appears to do nothing.  This is oftenthe case with lock or thread classes, which are usually defined and thennot referenced, but contain constructors and destructors that havenontrivial bookkeeping functions.     <br><dt><code>deprecated</code>     <dd>The <code>deprecated</code> attribute results in a warning if the typeis used anywhere in the source file.  This is useful when identifyingtypes that are expected to be removed in a future version of a program. If possible, the warning also includes the location of the declarationof the deprecated type, to enable users to easily find furtherinformation about why the type is deprecated, or what they should doinstead.  Note that the warnings only occur for uses and then onlyif the type is being applied to an identifier that itself is not beingdeclared 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.  Nowarning is issued for line 4 because T2 is not explicitlydeprecated.  Line 5 has no warning because T3 is explicitlydeprecated.  Similarly for line 6.     <p>The <code>deprecated</code> attribute can also be used for functions andvariables (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 totype-based alias analysis, but are instead assumed to be able to aliasany 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 variabledeclaration, the above program would abort when compiled with<code>-fstrict-aliasing</code>, which is on by default at <code>-O2</code> orabove in recent GCC versions.<h3 class="subsection">i386 Type Attributes</h4>     <p>Two attributes are currently defined for i386 configurations:<code>ms_struct</code> and <code>gcc_struct</code>     <br><dt><code>ms_struct</code>     <dd><dt><code>gcc_struct</code>     <dd>     <p>If <code>packed</code> is used on a structure, or if bit-fields are usedit may be that the Microsoft ABI packs them differentlythan GCC would normally pack them.  Particularly when moving packeddata between functions compiled with GCC and the native Microsoft compiler(either via function call or as data in a file), it may be necessary to accesseither format.     <p>Currently <code>-m[no-]ms-bitfields</code> is provided for the Microsoft Windows X86compilers to match the native Microsoft compiler. </dl>   <p>To specify multiple attributes, separate them by commas within thedouble parentheses: for example, <code>__attribute__ ((aligned (16),packed))</code>.   </body></html>

⌨️ 快捷键说明

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