Go to main content
Oracle® Developer Studio 12.6: C User's Guide

Exit Print View

Updated: July 2017
 
 

2.10 Supported Attributes

The compiler implements the following attributes (__attribute__ ((keyword)) ) for compatibility. Spelling the attribute keyword within double underscores, __keyword__, is also accepted.

alias

Makes a name an alias for a declared function or variable name.

aligned

Roughly equivalent to #pragma align. Generates a warning and is ignored if used on variable length arrays.

always_inline

Equivalent to #pragma inline and -xinline.

cleanup (cleanup function)

Causes the function specified with the attribute to be executed when the associated variable goes out of scope (exits the block in which it is declared). It can be applied only to auto function scope (local) variables, and cannot be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored. It is undefined what happens if the cleanup function does not return normally.

common, nocommon

The common attribute requests the compiler to place a variable in "common" storage, which is where a tentative definition of a variable is placed. The nocommon attribute requests the opposite, create a definition initialized to 0. These attributes override the default chosen by the -fno-common and -fcommon flags respectively.

const

Equivalent to #pragma no_side_effect.

constructor

Equivalent to #pragma init, except the constructor will only be called once.

deprecated (msg)

Results in a warning if the variable or function is used anywhere in the source file. The optional argument msg must be a string and will be included in the warning message if issued.

destructor

Equivalent to #pragma fini.

malloc

Equivalent to #pragma returns_new_memory.

noinline

Equivalent to #pragma no_inline and -xinline.

noreturn

Equivalent to #pragma does_not_return.

pure

Equivalent to #pragma does_not_write_global_data.

packed

Equivalent to #pragma pack(), except when applied to an enum type, where it tells the compiler to set the type of an enum to the smallest type capable of holding all the enumerator values. A signed type is used if and only if at least one of the enumerators has a negative value. Ultimately, a packed enum can become type signed char, unsigned char, signed short or unsigned short, depending on the enumerator values used.

returns_twice

Equivalent to #pragma unknown_control_flow.

section (section-name)

Places a global variable (or function) in a named section, instead of the sections .bss or .data. Make sure to initialize any global variable placed in a named section to avoid multiply defined error messages from the linker.

tls_model

Sets the thread-local storage model. The tls_model argument should be one of global-dynamic, local-dynamic, initial-exec, or local-exec.

vector_size

Indicates that a variable or a type name (created using typedef) represents a vector.

visibility

Provides linker scoping as described in Linker Scoping Specifiers. Syntax is: __attribute__((visibility(“visibility-type”))), where visibility-type is one of the following:

default

Same as __global linker scoping.

hidden

Same as __hidden linker scoping.

internal

Same as __symbolic linker scoping.

weak

Equivalent to #pragma weak.

2.10.1 __has_attribute function-like macro

The predefined function-like macro,

__has_attribute(attr)

evaluates to 1 if attr is a supported attribute. It evaluates to 0 otherwise. Example usage:

#ifndef __has_attribute // if we don't have __has_attribute, ignore it
    #define __has_attribute(x) 0
#endif
#if __has_attribute(deprecated)
   #define DEPRECATED __attribute__((deprecated))
#else
   #define DEPRECATED // attribute "deprecated" not available
#endif
void DEPRECATED old_func(int); // use the attribute if available