Naked 函数Naked Functions

Microsoft 专用Microsoft Specific

naked 存储类特性是特定于 Microsoft 的 C 语言扩展。The naked storage-class attribute is a Microsoft-specific extension to the C language. 对于使用 naked 存储类特性声明的函数,编译器生成不带 prolog 和 epilog 代码的代码。For functions declared with the naked storage-class attribute, the compiler generates code without prolog and epilog code. 利用此功能,可以使用内联汇编程序代码编写您自己的 prolog/epilog 代码序列。You can use this feature to write your own prolog/epilog code sequences using inline assembler code. 裸函数对于编写虚拟设备驱动程序特别有用。Naked functions are particularly useful in writing virtual device drivers.

由于 naked 特性仅与函数定义相关且不是类型修饰符,因此 naked 函数使用扩展的特性语法,如扩展的存储类特性中所述。Because the naked attribute is only relevant to the definition of a function and is not a type modifier, naked functions use the extended attribute syntax, described in Extended Storage-Class Attributes.

下面的示例利用 naked 特性定义了一个函数:The following example defines a function with the naked attribute:

__declspec( naked ) int func( formal_parameters )  
   /* Function body */  

或者:Or, alternatively:

#define Naked   __declspec( naked )  

Naked int func( formal_parameters )  
   /* Function body */  

naked 特性仅影响函数的 prolog 和 epilog 序列的编译器代码生成的性质。The naked attribute affects only the nature of the compiler's code generation for the function's prolog and epilog sequences. 它不影响为调用这些函数而生成的代码。It does not affect the code that is generated for calling such functions. 因此,naked 特性不被视为函数的类型的一部分,并且函数指针不能具有 naked 特性。Thus, the naked attribute is not considered part of the function's type, and function pointers cannot have the naked attribute. 此外,naked 特性不能应用于数据定义。Furthermore, the naked attribute cannot be applied to a data definition. 例如,下面的代码将生成错误:For example, the following code generates errors:

__declspec( naked ) int i;  /* Error--naked attribute not */  
                            /* permitted on data declarations. */  

naked 特性仅与函数的定义相关,且无法在函数原型中指定。The naked attribute is relevant only to the definition of the function and cannot be specified in the function's prototype. 下面的声明生成编译器错误:The following declaration generates a compiler error:

__declspec( naked ) int func();   /* Error--naked attribute not */  
                     /* permitted on function declarations.    */   \  

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

C 函数定义C Function Definitions