![]() |
|
#1
|
|||
|
|||
Usage of Pragma in CHi,
Can anybody give some explanation about Pragma usage in C ?? Please share a few examples in which Pragma is used. Thanks, Kashyap. |
|||
|
#2
|
|||
|
|||
Re: Usage of Pragma in CQuote:
#pragma is a preprocessor directive. See footnote. From The C Programming Language by Brian Kernighan and Dennis Ritchie, Second Edition (K&R 2) "A control line of the form #pragma tokensequence causes the processor to perform an implementation-dependent action. An unrecognized pragma is ignored." If a given compiler implements some kind of pragma action that you find useful, you have to realize that it may not have any effect on other compilers (or the effect might be different). One potentially useful pragma that is recognized and implemented similarly for a number of different compilers (but not necessarily all compilers) is something that tells the compiler to pack structures more tightly than "normal". CPP / C++ / C Code:
Output from Borland, Microsoft, and GNU compilers that I have: Code:
What happens in these particular cases is that the compiler aligns the different elements on 32-bit boundaries, so that the size of the structure is 12 bytes rather than 9. (You can print out the addresses of the struct members if you are interested.) If I change the program to CPP / C++ / C Code:
I get Code:
Note that the particular versions of the specific compilers that I tested gave the results that I showed. Structure packing and pragmas that alter compilers' treatment of structure data layout are not specified in any C or C++ language standards. All compilers are free to do what they want in this area. Other compilers and even different revisions of the same compiler may behave differently. So, if the size of a structure is important to your application, you always have to test your code before using it. Note that some CPU architectures (not the kind of CPU that most of us have on our desktop or laptop workstations) don't allow data to start on anything other than 32-bit boundaries, so it is literally impossible to access things on odd byte boundaries. This struct would not be usable, and the compiler would (probably) ignore any kind of pragma (or anything else) that tried to pack variables this way. If, somehow, you did manage to define data with addresses on odd boundaries in your C program you would get a "bus fault" error at runtime when the program tried to access that data. Regards, Dave Footnote: The word pragma is a Greek word, and is the root of the English word pragmatic, which, as an adjective, connotes practical or specific, rather than theoretical or idealistic. So, the #pragma directive designates an implementation-dependent action rather than one covered by a general rule of the C or C++ language. |
|
#3
|
|||
|
|||
Re: Usage of Pragma in CI might also ask here what is the meaning of #pragma once? Does it do the same as #ifndef / #endif?
|
|
#4
|
|||
|
|||
Re: Usage of Pragma in CQuote:
For some compilers, the effect is the same. It is a means to prevent the same header from being included more than once in the same compilation unit. (Like if a.h includes "b.h" and b.h includes "a.h". Without some guard against multiple inclusions, the compiler chokes.) However... If a particular compiler doesn't recognize a particular #pragma token, it ignores the #pragma statement, and #pragma once is not recognized by all compilers. So the answer, in general, is, "no." Since the "#ifndef/#endif" stuff in a header is covered by the C and C++ language standards, and the #pragma once may not work with some compilers, which one would you rather use? Now, if you are never, ever going to try to compile your code with any other compiler, and you want to use a pragma that you have discovered for your implementation, I guess you could go that way, but... Regards, Dave Last edited by davekw7x : 23-Aug-2007 at 10:16.
|
|
#5
|
|||
|
|||
Re: Usage of Pragma in CQuote:
|
|
#6
|
|||
|
|||
Re: Usage of Pragma in CQuote:
Regards, Dave |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| tcpsvcs.exe causing 100% CPU usage | jammm | Computer Software Forum - Windows | 21 | 12-Oct-2007 10:33 |
| CPU Usage | prakhar_jack | MS Visual C++ / MFC Forum | 6 | 08-Feb-2006 00:05 |
| no output, usage of sqrt and pow | micheldenostra | C Programming Language | 3 | 25-Sep-2005 17:32 |
| PHP/Apache memory usage issue | bacchus | Apache Web Server Forum | 0 | 18-Aug-2003 12:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The