Structure Packing

Hi,

Simple question. I am trying to read some databases made on a different
OS which uses structure packing (ie a byte takes up just 1 bytes of
space, not the next 4) but on QNX, I seem to be unable to force it to do
this. I used #pragma pack(1) under gcc and that seems to fix it on my
linux box. Any suggestions/direction? I have had a look through the
help viewer but to no avail.
BTW I am using neutrino but I guessed this was a simple qnx type
question

Thanks
Simon

Simon Wakley

Simon Wakley <Simon@nospammcameracontrol.com> wrote:

Hi,

Simple question. I am trying to read some databases made on a different
OS which uses structure packing (ie a byte takes up just 1 bytes of
space, not the next 4) but on QNX, I seem to be unable to force it to do
this. I used #pragma pack(1) under gcc and that seems to fix it on my
linux box. Any suggestions/direction? I have had a look through the
help viewer but to no avail.
BTW I am using neutrino but I guessed this was a simple qnx type
question

#pragma pack(1)

should work for gcc. Take a quick look at /usr/nto/include/_pack*.h

As a quick test, I wrote & compiled the following program:

#include <stdio.h>

#pragma pack(1)
struct a { char byte;
long word;
char byte2;
long word2;
} a_var;

#pragma pack(4)
struct b { char byte;
long word;
char byte2;
long word2;
} b_var;
#pragma pack()

void main()
{
printf(“sizeof a_var: %d, sizeof b_var: %d\n”, sizeof(a_var),
sizeof(b_var));
printf(“address of a_var.byte, a_var.word: %d, %d\n”, &a_var.byte,
&a_var.word);
printf(“address of b_var.byte, b_var.word: %d, %d\n”, &b_var.byte,
&b_var.word);
}

The output was:

sizeof a_var: 10, sizeof b_var: 16
address of a_var.byte, a_var.word: 134530784, 134530785
address of b_var.byte, b_var.word: 134530768, 134530772

That sure looks like the pragmas are working as expected.

-David