No pshpack1.h in g++?

S

Simon Elliott

I have some structs which must be byte packed with no padding. When I
was compiling with Borland BCB3/6 and Microsoft VC++ I used various
pragmas. Now I want to compile with g++.

I had the impression that pshpack1.h and the other alignment headers
were now standard, so I did some testing and found that pshpack1.h
isn't shipped with g++.

Is there any portable way of specifying alignment and packing?
 
R

Rolf Magnus

Simon Elliott said:
I have some structs which must be byte packed with no padding.

It must? Why? Does it represent some hardware registers?
When I was compiling with Borland BCB3/6 and Microsoft VC++ I used various
pragmas. Now I want to compile with g++.
I had the impression that pshpack1.h and the other alignment headers
were now standard,

I have no idea what "pshpack1.h" is, but if it is a header that deals in
some way with alignment, they are not, since alignment requirements are
platform specific.
so I did some testing and found that pshpack1.h isn't shipped with g++.
Is there any portable way of specifying alignment and packing?

No. Some hardware platforms don't even allow mis-aligned access to variables
and will terminate your program if you try. Even on platforms that do
support it, such accesses are often slower than correctly aligned ones.
 
P

Pete Becker

Simon said:
Is there any portable way of specifying alignment and packing?

No. Alignment requirements depend on the hardware, so forcing your
notion of appropriate alignment is not a good idea.
 
A

Azdo

Simon Elliott ha escrito:
I have some structs which must be byte packed with no padding. When I
was compiling with Borland BCB3/6 and Microsoft VC++ I used various
pragmas. Now I want to compile with g++.

I had the impression that pshpack1.h and the other alignment headers
were now standard, so I did some testing and found that pshpack1.h
isn't shipped with g++.

Is there any portable way of specifying alignment and packing?

Not portable, but this does what you intend:

http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Attribute-Syntax.html#Attribute-Syntax

look for __attribute__((packed))
 
S

Simon Elliott

It must? Why? Does it represent some hardware registers?

Something along those lines. The structs represent a set of messages as
specified by a standard. All implemented in C by another developer a
long time ago.
I have no idea what "pshpack1.h" is, but if it is a header that deals
in some way with alignment, they are not, since alignment
requirements are platform specific.

Yes. This is always a pain when someone, using a specific compiler,
tries to represent a specific layout in memory by using a struct, and
then this needs to be ported to a different platform and compiler. This
is always a problem in C and C++ as the data types may have different
sizes and the endian-ness may be different. (And the ways of
specifiying the alignment...)
No. Some hardware platforms don't even allow mis-aligned access to
variables and will terminate your program if you try. Even on
platforms that do support it, such accesses are often slower than
correctly aligned ones.

Yes. Fortunately I'm not too bothered about performance in this
particular application.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Simon Elliott said:
Yes. This is always a pain when someone, using a specific compiler,
tries to represent a specific layout in memory by using a struct, and
then this needs to be ported to a different platform and compiler. This

Then do not use a struct at all. Use an unsigned char array of the required
size and write packing and unpacking functions that takes care of size,
endianess and signeds encoding. Then the code will be portable to any
architecture hands down.
 
S

Simon Elliott

Then do not use a struct at all. Use an unsigned char array of the
required size and write packing and unpacking functions that takes
care of size, endianess and signeds encoding. Then the code will be
portable to any architecture hands down.

Yes. Or design a C++ class which encapsulates all this. But this is
legacy code.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,284
Latest member
alphabetsalphabets

Latest Threads

Top