Wednesday 20 March 2013

__declspec

__declspec is a storage specifier which specifies that an  instance of a given type is to be stored with a Microsoft specific storage -class attribute listed below:-


  1.  align( # )
  2. allocate(" segname ")
  3. appdomain
  4. deprecated
  5. dllimport
  6. dllexport
  7. jitintrinsic
  8. naked
  9. noalias
  10. noinline
  11. noreturn
  12. nothrow
  13. novtable
  14. process
  15. property( {get=get_func_name|,put=put_func_name})
  16. restrict
  17. safebuffers
  18. selectany
  19. thread
  20. uuid(" ComObjectGUID ") 

(1)align (#):-We can use __declspec (align(#)) to precisely control the alignment of user-defined data (for example ,static allocations or automatic data in a function).
Ex:-


__declspec( align( # ) ) declarator

(2)alllocate ("segname") declarator:-The allocate declaration specifier names a data segment in which the data item will be allocated .
Ex:-
__declspec(allocate("segname")) declarator

(3)appdomain:-_declspec(appdomain) is only valid when one of the /clr  compiler option is used .Only a global variable ,static member variable or static local variable can be marked with _declspec(appdomain ).It is an error to apply _declspec(appdomain ) to static members of managed types because they always have this behaviour.
(4)deprecated:-  The deprecated specify particular forms of functions overload as deprecated ,whereas the pragma form applies to all overload forms of a function name.The deprecated declaration specify a message that will display at compile time.The text of message can be from a macro.Macros can be marked as deprecated with the deprecated pragma.
(5)dllimport:- dllimport storage class attribute enables us to import function ,data & objects from the dll.The declaration of dllimport must use extended attribute syntax and the _declspec keyword.Ex:-
__declspec( dllimport ) int i;
(6)dllexport:-dllexport storage class attribute enables us to export function ,data & objects from the dll.Declaring function as dllexport eliminates the need for a module definition file ,at least with respect to the
specification of exported functions .Note that dllexport replaces the _export keyword .If a class is marked declspec (dllexport ),any specialization of class templates in the class hierarchy are implicitly marked as declspec(dllexport ).This means templates are explicitly instantiated and its member must be defined .The declaration of dllexport must use extended attribute syntax and the _declspec keyword.
Ex:-
__declspec( dllexport ) void func();
(7)jitintrinsic:-It marks the function as significant to the 64-bit common language runtime.This is used on certain functions in microsoft -provided library .
_decspec (intrinsic)

(8)naked:-For the function declared with the naked attribute ,the compiler generates code without prolog and apilog code.Naked functions are particularly useful in writing virtual device driver.The naked code is only valid on x86 and is not available on x64 or itanium.
Ex:-
__declspec( naked ) int func( formal_parameters ) {}

(9)noalias:-noalias means that a function call does not modify or reference visible globle state and only modifies the memory pointed to directly by pointer parameters .If a function is a annonated as noalias ,the optimizer can assume that in addition to the parameter themselves,only first-level indirections of pointer parameter are referenced or modified inside the function .

(10)noinline:-_declspec(noinline) tells the compiler to never inline a particular member function .If a function is marked noinline ,the calling function will be smaller and thus ,itself a candidate for compiler inlining .

(11)noreturn:-It tells the compiler that a function does not return .As a consequence ,the compiler knows that the code following a call to a _declspec (no return function) is unreachable .
(12)nothrow:-It can be used in declaration of functions.It tells the compiler that the declared function and the the functions it calls never throw an exception . 

return-type __declspec(nothrow) [call-convention] function-name ([argument-list])

(13)novtable:-This form of _decspec can be applied to any class declaration ,but should only be applied to pure interface classes,that is ,classes that will never be instantiated on their own .If we attempt to instantiate a  class marked with novtable  and then access a class member ,we will receive an access violation.

(14)  process:-It specifies that we managed application process should have a single copy of a particular global variable ,static member variable or static local variable shared  across all application domain in the process .Only a global variable,static member variable or static local variable of native type can be marked with __declspec(process).

(15)property:-This attribute can be applied to non-static "virtual data members" in a class or structure definition.The compiler treats these "virtual data members " as data members by changing their references into function calls.



__declspec( property( get=get_func_name ) ) declarator
__declspec( property( put=put_func_name ) ) declarator
__declspec( property( get=get_func_name, put=put_func_name ) ) declarator


(16)restrict:-It is applied to a function declaration or definition that returns a pointer type and tells the compiler that the function returns an object that will not be aliased with any other pointer .



__declspec(restrict) return_type f();

(17)safebuffers:-safebuffers tells the compiler not to insert buffer overrun security checks for a function.
 An expert manual code review or external analysis might determine that a function safe from a buffer overrun.In that case we  can suppress security checks for a function by applying the _declspec (safebuffer)
keyword to the function declaration .

__declspec(safebuffers)

(18)selectany:-It tells the compiler that the declared global data item (variable or object )pick any packaged function.selectany can only be applied to the actual initialization of global data items that are externally visible.

__declspec( selectany ) declarator

(19)thread:-The thread extended storage class modifier is used to declare a thread local variable.It is used with _declspec(thread) declarator .We can apply the thread  attribute only to data declaration and definitions and classes do not have member functions ,thread cannot be used on function declaration or function definitions.


___declspec( thread ) declarator

(20)uuid:-The compiler attaches a guide to a class or structure declare or defined with the uuid attribute.The uuid takes a string as its argument .This string names a GUID in normal registry format with or without the ()delimeters.

struct __declspec(uuid("00000000-0000-0000-c000-000000000046")) IUnknown;
struct __declspec(uuid("{00020400-0000-0000-c000-000000000046}")) IDispatch; 


No comments:

Post a Comment