GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Aug-2007, 04:42
kashyap kashyap is offline
New Member
 
Join Date: Jul 2007
Posts: 3
kashyap is on a distinguished road

Usage of Pragma in C


Hi,

Can anybody give some explanation about Pragma usage in C ??
Please share a few examples in which Pragma is used.

Thanks,
Kashyap.
  #2  
Old 23-Aug-2007, 08:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Usage of Pragma in C


Quote:
Originally Posted by kashyap
Hi,

Can anybody give some explanation about Pragma usage in C ??
Please share a few examples in which Pragma is used.

Thanks,
Kashyap.

#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:
#include <stdio.h>

typedef struct _test {char cc[5]; int i;} test;

int main()
{
    test myStruct;
    printf("sizeof(myStruct.cc) = %d, sizeof(myStruct.i) = %d\n\n", 
            sizeof(myStruct.cc), sizeof(myStruct.i));

    printf("sizeof(myStruct) = %d\n", sizeof(myStruct));

    return 0;
}

Output from Borland, Microsoft, and GNU compilers that I have:
Code:
sizeof(myStruct.cc) = 5, sizeof(myStruct.i) = 4 sizeof(myStruct) = 12

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:
#include <stdio.h>

#pragma pack(1) /* note that this does not ***guarantee*** packing */

typedef struct _test {char cc[5]; int i;} test;

int main()
{
    test myStruct;
    printf("sizeof(myStruct.cc) = %d, sizeof(myStruct.i) = %d\n\n", 
            sizeof(myStruct.cc), sizeof(myStruct.i));

    printf("sizeof(myStruct) = %d\n", sizeof(myStruct));

    return 0;
}

I get

Code:
sizeof(myStruct.cc) = 5, sizeof(myStruct.i) = 4 sizeof(myStruct) = 9

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  
Old 23-Aug-2007, 09:05
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Usage of Pragma in C


I might also ask here what is the meaning of #pragma once? Does it do the same as #ifndef / #endif?
  #4  
Old 23-Aug-2007, 09:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Usage of Pragma in C


Quote:
Originally Posted by Kimmo
I might also ask here what is the meaning of #pragma once? Does it do the same as #ifndef / #endif?

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  
Old 23-Aug-2007, 09:21
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Usage of Pragma in C


Quote:
Originally Posted by davekw7x
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?
I'm going the #ifndef way, but have seen the #pragma once used in headers. I thought maybe it had some other uses. But I guess, based on your answer, that it could do pretty many things in theory.
  #6  
Old 23-Aug-2007, 09:34
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Usage of Pragma in C


Quote:
Originally Posted by Kimmo
I..have seen the #pragma once used in headers.
So have I, and it's not "wrong" if it works for the person who put it there. It's just not guaranteed to be portable. (It doesn't have the desired effect with my Borland compilers, for example.)

Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.