Operating System - HP-UX
1832235 Members
2861 Online
110041 Solutions
New Discussion

Re: Structure alignment in 10.20 C (bundled)

 
SOLVED
Go to solution
Gilles Allard
Advisor

Structure alignment in 10.20 C (bundled)

I need to use the bundled version of C compiler in HPUX 10.20. My problem is with the alignment of fields in a structure. It seems they are word-aligned but I need byte-alignment.
How can I do it?
Is there any online manual for the bundled compiler?
Thanks
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Structure alignment in 10.20 C (bundled)

Hi Gilles:

To my great surprise the 10.20 bundled compiler recogizes the same pragma as the Ansi C compiler.

Use
#pragma HP_ALIGN NOPADDING

Sample code:

int main()
{
struct
{
int x;
char y;
short z;
} zz;

printf("size = %d\n",sizeof(zz));
return(0);
}

produces 8 bytes without the pragma and 7 bytes
with the pragma. Just what you wanted. You know you are being a bad boy producing non-portable code.

Regards, Clay
If it ain't broke, I can fix that.
Gilles Allard
Advisor

Re: Structure alignment in 10.20 C (bundled)

Hi Clay
For now, I do not know if you are a king (your icon), but, for sure, you are a rabbit. I was not expecting any reply today (Sunday).
Your last remark "You know you are being a bad boy producing non-portable code" is interesting and bring a new question.
I have to decode a stream of network data that is based on byte-aligned (variable size) structures. Is there a better way rather than using pointers and "non-portable" structures?

A. Clay Stephenson
Acclaimed Contributor

Re: Structure alignment in 10.20 C (bundled)

Hi again Gilles:

You need to look into xdr (External Data Representation); it's the foundation of rpc (Remote Procedure Calls). It allows one to represent data in a platform independent way and thus allows a procedure and data on Machine A to be executed and accessed on Machine B even if A is a Unix server and B is a PC under Windows. You could man xdr for details if you had a development compiler. You really need to get a devopment compiler for any serious work. If you choose not to use one of the HP compilers, you can download gcc from one of the HP-UX Porting Centers.

Regards, Clay
If it ain't broke, I can fix that.
Gilles Allard
Advisor

Re: Structure alignment in 10.20 C (bundled)

Thanks Clay
To verify operation of the HP_ALIGN pragma, I did the following program:
------------------------------------------#pragma HP_ALIGN NOPADDING

typedef struct{
unsigned char a;
unsigned char b;
unsigned long c;
unsigned long d;
unsigned short e;
} PS;

main( argc, argv)
int argc;
char *argv[];
{

PS s;

printf ("Offsets in PS: %d, %d, %d, %d, %d, %d\n", &s.a - &s, &s.b - &s.a, &s.c - &s.a, &s.d - &s.a, &s.e - &s.a, sizeof( s));

if (&s.c > &s.a) printf( "a comes before c\n");

memcpy( s, "ABCDEFGHIJKLMN", sizeof(s));
printf( "%x, %x, %x, %x, %x\n", s.a, s.b, s.c, s.d, s.e);
}
-------------------------------------------
Results are:
Offsets in PS: 0, 1, 0, 1, 5, 12
a comes before c
41, 42, 43444546, 4748494a, 4b4c

The pointers in PA-RISC seem unusual: both the "a" and "c" members have the same offset.
According to K&R, subtracting pointers to members of the same structure should be permitted. However they do not state what the result should be. With PA_RISC, it seems that the result is not significant.
I was amazed to see that pointer comparison worked fine (&s.c is greater than &s.a while the difference is zero).
Can you bring some light on this
Thanks
Gregory Fruth
Esteemed Contributor

Re: Structure alignment in 10.20 C (bundled)

Try this instead:

printf ("Addrs in PS: %d, %d, %d, %d, %d, %d\n",
&s, &s.a, &s.b, &s.c, &s.d, &s.e);

printf ("Offsets in PS: %d, %d, %d, %d, %d, %d\n",
(int) (&s.a) - (int) (&s),
(int) (&s.b) - (int) (&s.a),
(int) (&s.c) - (int) (&s.a),
(int) (&s.d) - (int) (&s.a),
(int) (&s.e) - (int) (&s.a),
sizeof(s));

The output is:

Addrs in PS: 2063839952, 2063839952, 2063839953, 2063839954, 2063839958, 2063839962
Offsets in PS: 0, 1, 2, 6, 10, 12
a comes before c
41, 42, 43444546, 4748494a, 4b4c
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Structure alignment in 10.20 C (bundled)

Hi Gilles,

Sorry I didn't notice your last posting until Gregory's answer. The results of pointer arithmetic can be quite confusing.

For example, using your struct PS example
let's declare an array struct PS xx[20];

let's also assume that the base address of xx
(i.e &(xx[0]) = 1000. Since these structs are 12 bytes xx[1] = 1012, xx[2] = 1024, ... .

Now let's declare 2 pointers:

struct PS *p0 = &(xx[0]),*p2 = &(xx[2]);

However, if doing pointer arithmetic
p2 - p0 does not equal 24 as you might expect but instead equals 2.

Hope this helps, Clay






If it ain't broke, I can fix that.
Gilles Allard
Advisor

Re: Structure alignment in 10.20 C (bundled)

Thanks to Gregory and Clay.
I'm ashamed. I's always essential to remember the basics.
The case is closed (at least for me) but other questions will follow in separate threads.