Link to home
Start Free TrialLog in
Avatar of arut
arut

asked on

#pragma usage

Can someone explain with a simple example the usage of
#pragma directive.

Thanks,
Arut
Avatar of rajeev_devin
rajeev_devin

#pragma inline_depth( [0... 255] )

Controls the number of times inline expansion can occur by controlling the number of times that a series of function calls can be expanded (from 0 to 255 times).
Another example:

#if _M_IX86 == 500
#pragma message( "Pentium processor build" )
#endif

This will generate the message "pentium processor build", if your processor is a pentium one.
Avatar of arut

ASKER

Is inline_depth a macro understood by a compiler?

ASKER CERTIFIED SOLUTION
Avatar of bharat_mane
bharat_mane

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes its' a kind of macro.
Avatar of Kent Olsen

In the MicroSoft URL mentioned above, take particular note of the opening paragrpah's last line, "Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler."

This means that a #pragma statement that works just fine with your MicroSoft compiler may fail with Borland, gcc, or any other compiler.  (The inverse is also true.)

The standard states that unknown pragma directives are ignored.  The intent is that, since #pragma directives are meant to control machine-specific characteristics, they will be ignored on other platforms.  The byproduct is that the program might easily compile when ported to another system, but there is no assurance that it will run.

Often, the default characteristic of a C/C++ "struct" is that items contained within the struct are word aligned for speed.  If you want the items byte aligned, you include the correct #pragma directive.

#pragma align=b
#pragma align=byte

is a perfectly valid pragma statement.  Any system that uses the "align" parameter to control data alignment will pack all subsequent definitions so that they occupy the least amount of space.  But not all systems use "align" for this parameter.  If compiled on one of these systems, the compiler will parse the statement, recognize that "align" is not in its repertoire, an continue without affecting the rest of the compilation.

#pragma pack

is used in other environments.  So you have to know which one is appropriate for your system.  You could play it safe and include both.

Unknown parameters are supposed to be ignored.  Illegal parameters are errors.

#pragma align=byte
#pragma align=word
#pragma align=hword
#pragma align=dword

All of these are valid on at least one platform.  If one of these statements is compiled on a system that doesn't support that alignment the compiler generates an error.


This ends today's lesson on #pragma.  :)
Kdo