Portability Gripe... (Structures) 
Author Message
 Portability Gripe... (Structures)

I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
Solaris (SPARC).

My main gripe is there seems to be no way to specify in a portable manner
that all structures should be packed to 1 byte (unless I'm missing
something).

The structures need to be packed to ensure they align with fixed data in
disk files, the format of which is not under my control.

So far, I have found the following:

Solaris cc:
#pragma pack(1)
in the source code.

HP-UX cc:
#pragma pack 1
in the source code.

Win32 gcc:
-fpack-struct
on the compile parameters

Dynix/PTX cc:
-qalign=packed
on the compile parameters

Win32 Visual C:
Haven't cracked this one yet.

Does anyone else get irritated by this sort of thing or is it just me? (or
am I missing something)



Fri, 09 Jan 2004 17:56:08 GMT  
 Portability Gripe... (Structures)

Quote:
>My main gripe is there seems to be no way to specify in a portable manner
>that all structures should be packed to 1 byte (unless I'm missing
>something).

why try

presumably you trying to encode a specific byte sequence for some external
interface such as ip packets

move the bytes across the interface with read or fwrite or whatever to an
internal byte array

write an unpack procedure that memcpy fields from the byte array (and
perhaps changes byte orders) into a normal struct
another pack procedure to go the other way

the external interface dependent portions of the code are neatly separated
from rest of your code

you can write your code with structs that package the information in the
best manner for your code



Sat, 10 Jan 2004 18:03:28 GMT  
 Portability Gripe... (Structures)


Quote:
> I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX
and
> Solaris (SPARC).

> My main gripe is there seems to be no way to specify in a portable manner
> that all structures should be packed to 1 byte (unless I'm missing
> something).

> The structures need to be packed to ensure they align with fixed data in
> disk files, the format of which is not under my control.

> So far, I have found the following:

> Solaris cc:
> #pragma pack(1)
> in the source code.

> HP-UX cc:
> #pragma pack 1
> in the source code.

> Win32 gcc:
> -fpack-struct
> on the compile parameters

> Dynix/PTX cc:
> -qalign=packed
> on the compile parameters

> Win32 Visual C:
> Haven't cracked this one yet.

> Does anyone else get irritated by this sort of thing or is it just me? (or
> am I missing something)

You want todo something like

struct somestruct mystruct;
fread(&mystruct, 1, sizeof mystruct, infile);

right ?

Well don't!

I suggest you build support routines to portably read integers/etc from
files.  It's not hard at all just a bit annoying.

Tom



Sat, 10 Jan 2004 18:04:07 GMT  
 Portability Gripe... (Structures)

Quote:

> I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
> Solaris (SPARC).

> My main gripe is there seems to be no way to specify in a portable manner
> that all structures should be packed to 1 byte (unless I'm missing
> something).
> The structures need to be packed to ensure they align with fixed data in
> disk files, the format of which is not under my control.

The structure are packed on disk but do not need
to be packed when loaded.
I suspect you want this to be able to read/write all fields of a struct
with a single fread/fwrite.
If so, I suggest you to fill the struct field by field instead
for the following reason :

If the file format is really portable,
then its endianess is specified and machine-independant
(so that the files may be exchanged between different machines).
Since your program must run both on a little endian machine (PC Win NT)
and a big endian machine (SPARC), you will have anyway to inspect each
the fields to convert them from the endianess of the file to your
the endianess of your machine (and vice-versa) using specific library
functions.

--
Regis



Sat, 10 Jan 2004 19:17:03 GMT  
 Portability Gripe... (Structures)

Quote:

> I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
> Solaris (SPARC).

> My main gripe is there seems to be no way to specify in a portable manner
> that all structures should be packed to 1 byte (unless I'm missing
> something).

There is indeed no such way.

Quote:

> The structures need to be packed to ensure they align with fixed data in
> disk files, the format of which is not under my control.

Ah, but you don't have to align the structs with the disk files. What
you can do is read your records from disk into an array of unsigned
char, and unpack them into structure members yourself.

Quote:

> So far, I have found the following:

> Solaris cc:
> #pragma pack(1)
> in the source code.

> HP-UX cc:
> #pragma pack 1
> in the source code.

> Win32 gcc:
> -fpack-struct
> on the compile parameters

> Dynix/PTX cc:
> -qalign=packed
> on the compile parameters

> Win32 Visual C:
> Haven't cracked this one yet.

FWIW (and OT), it's a compilation parameter: /Zp1

Quote:
> Does anyone else get irritated by this sort of thing or is it just me? (or
> am I missing something)

It's generally less annoying to use text files. If you are required to
interface with binary files, it's probably best to get them as an array
of bytes and then translate them into structs within your program, "by
hand" so to speak.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sat, 10 Jan 2004 19:11:35 GMT  
 Portability Gripe... (Structures)

Quote:

> I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
> Solaris (SPARC).

> My main gripe is there seems to be no way to specify in a portable manner
> that all structures should be packed to 1 byte (unless I'm missing
> something).

Apart from what the others have said: the reason why this is so is that
it is not always efficiently possible or feasible to do this. Some
machines are so much slower unaligned than aligned that the attempt
isn't worth the trouble.

Richard



Sat, 10 Jan 2004 20:17:16 GMT  
 Portability Gripe... (Structures)

Quote:


> > I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
> > Solaris (SPARC).

> > My main gripe is there seems to be no way to specify in a portable manner
> > that all structures should be packed to 1 byte (unless I'm missing
> > something).

> Apart from what the others have said: the reason why this is so is that
> it is not always efficiently possible or feasible to do this. Some
> machines are so much slower unaligned than aligned that the attempt
> isn't worth the trouble.

> Richard

Just to be redundant, I was about to respond with this exact info!
I wrote an application this way quite a few years ago, which was then
passed to others to maintain.  There is now a need to extract a large
amount of performance from it and the first thing we are doing is
removing all of the byte aligned data transfers.  The improvements
are in the order of 20TPS byte aligned, 2500TPS character data stream!
I don't know the cost yet of parsing the data stream, but I'm pretty
sure I can do it with less cost than a 99% decrease!

KT



Sun, 11 Jan 2004 05:57:12 GMT  
 Portability Gripe... (Structures)

Quote:

>I have to make a program portable over Windows NT, HP-UX, AIX, Dynix/PTX and
>Solaris (SPARC).

>My main gripe is there seems to be no way to specify in a portable manner
>that all structures should be packed to 1 byte (unless I'm missing
>something).

It's not the intent of C structures to serve as a portable external
representation. The purpose of C structures is to aggregate a group
of related values into one object, to form some complex (internal)
data representation.

Quote:
>The structures need to be packed to ensure they align with fixed data in
>disk files, the format of which is not under my control.

The portable way to do this is to convert between the internal
representation and a buffer which contains a portable, byte level
representation (as an array of unsigned char). The buffer
format is what is moved between memory and file storage.

The conversion routines are then written in a highly portable manner,
resulting in a program which ports, and whose data files port
along with it.



Sun, 11 Jan 2004 14:07:47 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Structures, padding, fwrite() and portability?

2. Portability question for structures.

3. Portability of passing/operating on structures...

4. Portability of variable length structures

5. More structure portability

6. gripe ...

7. Any gripes about PVOID arguments?

8. Turbo C++ 3.0 Text Mode Page Swap Gripe...

9. VC7 IDE Gripes

10. BC++4.0 gripe and Watcom C opinion requested

11. Microsoft C ANSI Question/Gripe...

12. Public Gripe

 

 
Powered by phpBB® Forum Software