S
santosh
REH said:Why is this non-portable? The Ada standard (and, thus, compilers)
have done this portably for years.
It not portable in C. Ada is totally different.
REH said:Why is this non-portable? The Ada standard (and, thus, compilers)
have done this portably for years.
Jacob said:OK. Thanks. Maybe there is some document describing this in detail?
It not portable in C.
Ada is totally different.
It not portable in C. Ada is totally different.
I don't know Ada, but I guess I can guess, right? It is quite possible that
Ada makes structure packing portable by picking one strategy and insisting
on it. If so, well, yes, that would work. C, on the other hand, does not
do this (and in a different way, that works too, but it can be a trap for
the unwary).
Depending on your programming needs and preferences, you might call this an
obstacle to portability, or you might call it a sensible unwillingness to
place arbitrary restrictions on implementations.
In practice, the various struct packing strategies are not a serious
obstacle to portability. My approach is to ignore them completely except
where data interchange formats (eg file formats) are at stake, at which
point I recognise that I need to put a layer between the struct and the
point where data exchange occurs (whether it's a socket or a file or
whatever), so that I can sidestep the issue altogether.
CJ said:Probably gcc/etc. want to allow the programmer to decide *for each
individual struct* whether it should be packed or not.
This approach
will create terrible maintenance headaches - the pack/don't pack
directive will need to be present each time the compiler sees a
declaration of the struct.
CJ said:Probably gcc/etc. want to allow the programmer to decide *for each
individual struct* whether it should be packed or not.
This approach
will create terrible maintenance headaches - the pack/don't pack
directive will need to be present each time the compiler sees a
declaration of the struct.
Spoon said:
[...]Since standard C doesn't provide any way for the programmer to direct
the compiler as to how to layout structures, most compilers provide some
way to do this, albeit in different forms.Microsoft (and lcc-win) uses
#pragma pack(1)Gcc uses
__attribute__ {(packed)}Has anyone seen other directives in other compilers?
http://docs.sun.com/app/docs/doc/819-5265/bjach?l=en&a=view&q=pragma+pack
The Sun Studio 12 C User's Guide
show a syntax largely the same as Microsoft's
but requires #pragma pack() to end the scope.
Sun also includes two important usage notes that might not be the
same on other systems:
[...]
Note --
If you use #pragma pack to align struct or union members on
boundaries other than their natural boundaries, accessing these
fields usually leads to a bus error on SPARC. In order to avoid
such an error, be sure to also specify the -xmemalign option.
See B.2.108 -xmemalign=ab , for the optimal way to compile such
programs.
(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:
I'd consider that to be a bug in the SPARC compiler. If you declare a
structure to be packed, it's the compiler's job to generate the right
code to access the members of that structure, however they happen to
be aligned. The fact that there's a workaround for this bug doesn't
make it any less a bug.
Richard Heathfield said:santosh said:
I don't know Ada, but I guess I can guess, right? It is quite possible that
Ada makes structure packing portable by picking one strategy and insisting
on it. If so, well, yes, that would work. C, on the other hand, does not
do this (and in a different way, that works too, but it can be a trap for
the unwary).
santosh said:Keith Thompson wrote:
<snip>
It seems that the UDP on rr.com has been lifted, as I can read this post
on Motzarella.org.
jacob navia said:Yes, I saw that already, but in that "manual"
there is NO MENTION of any packed <n>
(An Ada record representation clause can specify the *exact* layout of
each member of a record. I don't think anyone has suggested such a
thing for C.)
Since standard C doesn't provide any way for the programmer to direct
the compiler as to how to layout structures, most compilers provide some
way to do this, albeit in different forms.
I am writing this part of my tutorial, and I would like to make a table
about the constructs used by the various compilers. Note that under
windows you must know this kind of stuff since windows uses packed
structures extensively.
Surely a QOI rather than a bug? Since C doesn't define a #pragma pack
the meaning of the directive is unconstrained and it could introduce
UB into code that would be otherwise clean.
http://docs.hp.com/en/B3901-90012/ch02s02.html
HP also uses
#pragma PACK n
where n is 1, 2, 4, 8, or 16. ...
... on the other hand, the messiness also gives an indication as
to why it might not be as easy as expected to standardize a single
#pragma pack
CBFalconer said:James Kuyper wrote: ....
I don't think I said "couldn't be done", but I may have said "a
pain" and "inefficient". Yes, there are cases when it has to be
done. But the idea is to avoid them.
Since, having packed, you can no longer index arrays of these
beasties, please explain how you are going to address 100e6 of
them? ...
Jacob is working with a machine that is especially forgiving of
misalignment, the X86. Also, failing to keep an eye on the code
generated for simple operations leads to C++ bloat.
REH said:Why is this non-portable? The Ada standard (and, thus, compilers)
have done this portably for years.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.