Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pragma

Reply
Thread Tools

Pragma

 
 
swengineer001@gmail.com
Guest
Posts: n/a
 
      05-15-2006

Gordon Burditt wrote:
> >> When #pragma seems like
> >> a good idea, you are probably hallucinating and should get some
> >> bed rest.
> >>

> >So what would you suggest when stucture packing is required for a

>
> structure packing is NOT required for a C program. I don't know
> what kind of program it is, but it's not C.


OK, there are other ways to accomplish the same thing but they are
likely, IMHO, to make the code more difficult to read which should also
be a consideration. I am sure many people here could do just about
anything in completely portable C but I would also bet that anyone here
that really knows what they are doing would not suggest that that
should always be the approach taken. Sometimes the best solution to a
problem is a non portable solution (non portable does not mean non
standard).

As far as structure packing not being C, I would suggest that it is C
since #pragma without a STDC preprocessing token immediately following
it invokes implementation defined behavior according to the standard.
Thus anything the implementation wants to implement with a #pragma
directive is by definition standard C compliant. It may not be portable
but it is without question C and in compliance with the standard.
Discussing a specific implementations #pragma directive is off topic
here but I have never worked with an implementation that did not
support a pack direvtive of some sort (I am sure there are some out
there but I have not come accross one).

Just my opinion, and I am sure I will get plentty of feedback on it.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [...]
> As far as structure packing not being C, I would suggest that it is C
> since #pragma without a STDC preprocessing token immediately following
> it invokes implementation defined behavior according to the standard.
> Thus anything the implementation wants to implement with a #pragma
> directive is by definition standard C compliant. It may not be portable
> but it is without question C and in compliance with the standard.
> [...]


C, or not-C?

int main(void) {
#pragma fortran
write (6,10)
10 format(14H Hello, world!)
#pragma c
return 0;
}

All right, that example is perhaps a little bit fanciful!
(Not completely implausible, though: consider all those postings
showing "C" sources with assembly languages embedded in them.)
My point is to offer the notion that #pragma moves the code out
of the noontime glare of the C Standard and somewhere into the
shadowy dusk -- or even, as above, into black midnight.

The semantics of #pragma are not (in my limited experience)
usually as drastic as my illustration. Still, even more "normal"
adjustments can raise issues with the language. The business of
struct packing, for example: Consider the notion of "compatible
type" as defined in the Standard, and ponder how you would need
to adjust it to accommodate a struct-packing #pragma. It is easy
to see that two "compatible" structs meeting all the Standard's
requirements can wind up being incompatible if one is packed and
the other is not. The #pragma thus makes the language not an
extended C, but a divergent C.

I've seen alignment-controlling #pragmas, too -- and they
can be even more deadly, IMHO. That's because lots of compilers
support a `#pragma align <number>' directive, but disagree over
the significance of the <number>: Does `#pragma align 16' specify
alignment to a 16-byte or a 65536-byte boundary? The promise that
the compiler will ignore a #pragma it doesn't recognize is of no
help in this case.

Even "harmless" #pragmas cast doubt on the code. If you
see `#pragma csect shared' you may guess that the linker is
being asked to put something in a "shared" area -- but shared
with whom, and by what kind of mechanism, and with what sort
of initialization, and ...? I opine that the answers to such
questions are essential to the proper operation of the program
(else the #pragma wouldn't be there), but that they almost
certainly change the semantics of the "shared" objects in ways
that are foreign to C.

#pragma transforms C code into "C, but ..." code. In the
context of the original poster's question

> I'm a beginner in C...May i like to know the difference between a
> #pragma and a #define....


.... I think "Don't Do That" is the correct answer, at least for
the time being.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      05-15-2006
"(E-Mail Removed)" wrote:
>
> Eric Sosman wrote:
>
> > When #pragma seems like
> > a good idea, you are probably hallucinating and should get some
> > bed rest.
> >

> So what would you suggest when stucture packing is required for a
> program. As far as I know there is no standar C way to do it but it is
> often required in embedded systems. Am I wrong on this?


On all of the systems I have worked on, the vendors have been kind enough
to supply (admitedly non-standard) header files for this purpose. However,
all of the vendors have also been kind enough to use the same names for
these files -- pshpack1.h, pshpack2.h, pshpack4.h, pshpack8.h, and
poppack.h -- so I have been able to use these headers in the very few
source files that needed them.

They have also included #pragma's to do this, but they are different for
each of the systems. However, the header files they have supplied hide
this.

But, you are correct that there is no standard way of doing this. And,
it may be possible that it's not possible to do this on some platforms.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>


 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      05-15-2006
muttaa wrote:
> I'm a beginner in C...May i like to know the difference between a
> #pragma and a #define....
>
> Also,yet i'm unclear what a pragma is all about as i can find topics on
> it only in high-standard books...
>

The behaviour of #pragma is implementation defined. On GCC it used to
(does it still?) allow you to play hack/rogue. Very different than a
#define, that.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-15-2006
Ian Collins wrote:
> Eric Sosman wrote:
>

.... snip ...
>>
>> Structure packing is also used to make a struct match an
>> externally-imposed format like a packet header or some such.
>> This is a snare and a delusion; in the long run, It Doesn't Work.

>
> It does if the externally imposed format is hardware registers.


On one (maybe more) system. Bit picking and packing works anywhere.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-15-2006
CBFalconer wrote:
> Ian Collins wrote:
>
>>Eric Sosman wrote:
>>

>
> .... snip ...
>
>>> Structure packing is also used to make a struct match an
>>>externally-imposed format like a packet header or some such.
>>>This is a snare and a delusion; in the long run, It Doesn't Work.

>>
>>It does if the externally imposed format is hardware registers.

>
>
> On one (maybe more) system. Bit picking and packing works anywhere.
>

As a developer (or manager) one can either be pragmatic or dogmatic. If
your toolchain offers you non-standard way of simplifying a task, the
choice is yours whether you use it.

--
Ian Collins.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding pragma translate_off/on , synthesis_off/on suman.nandan@gmail.com VHDL 9 07-21-2005 04:51 AM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
[Q] pragma no-cache : what about the images ? Paul J. Le Genial HTML 5 03-16-2005 02:58 AM
Re: pragma Denis Lagno C++ 0 10-04-2003 10:29 AM
<meta http-equiv="pragma" content="no-cache"> - What does this mean??? Titus A Ducksass HTML 1 08-01-2003 07:44 PM



Advertisments