Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

Natural alignment of 32-bit values within structures (gcc 3.4.1)

Natural alignment of 32-bit values within structures (gcc 3.4.1)

2004-11-30 by dpbevin2k

I've got a problem accessing 32-bit values within nested structures
using gcc 3.4.1.

I don't know whether it is a problem with compiler flags or because
the output object is fairly large (600Kb) or whether it is because the
nested structure is quite complicated (see below).

Having debugged the source, I can see that a 32-bit value within the
structure is not aligned to a 32-bit boundary (only 16-bit) and
therefore, the STR operation on that element fails.

The structures are as follows:-


<-- BEGIN CODE -->

typedef unsigned char	UBYTE;
typedef unsigned short	UWORD;
typedef unsigned long	ULONG;

typedef enum  
{
  ENUM_ONE,    
  ENUM_TWO,    
  ENUM_THREE,
  ENUM_FOUR
} SOME_ENUMS;

typedef union
{
  float  alg_val;
  ULONG  dig_val;

  struct
  {
    UBYTE    fourA;
    UBYTE    fourB;
    UWORD    fourC;
  } indirect;
} OBJECT_FOUR;

typedef struct
{
  OBJECT_FOUR   twoA;
  OBJECT_FOUR   twoB;
  OBJECT_FOUR   twoC;
  OBJECT_FOUR   twoD;
  OBJECT_FOUR   twoE;
  UBYTE         twoF;
} OBJECT_TWO;

typedef struct
{
  SOME_ENUMS    threeA;
  UBYTE         threeB;
  UWORD         threeC;
  OBJECT_FOUR   threeD;
  ULONG         threeE;
  ULONG         threeF;

  OBJECT_FOUR   threeG;

  UBYTE         threeH;
  UBYTE         threeJ;

} OBJECT_THREE;

typedef struct 
{
  UWORD         oneA;
  OBJECT_TWO    oneB;
  UBYTE         oneC;
  OBJECT_THREE  oneD;
} OBJECT_ONE;

<-- END CODE -->



When I use a pointer to an OBJECT_ONE object, any accesses to members
within oneB (OBJECT_TWO) fail because the compiler hasn't put a
two-byte pad between oneA and oneB (to preserve natural aligment).

Any help would be greatly appreciated.

Thanks in advance.

David Bevin

Re: Natural alignment of 32-bit values within structures (gcc 3.4.1)

2004-11-30 by dpbevin2k

Sincere apologies to all... I am a donkey/arse (sorry ass)!

I found a "#pragma pack" well hidden within the bowels of the source.




--- In lpc2000@yahoogroups.com, "dpbevin2k" <dbevin@s...> wrote:
Show quoted textHide quoted text
> 
> 
> I've got a problem accessing 32-bit values within nested structures
> using gcc 3.4.1.
> 
> I don't know whether it is a problem with compiler flags or because
> the output object is fairly large (600Kb) or whether it is because the
> nested structure is quite complicated (see below).
> 
> Having debugged the source, I can see that a 32-bit value within the
> structure is not aligned to a 32-bit boundary (only 16-bit) and
> therefore, the STR operation on that element fails.
> 
> The structures are as follows:-
> 
> 
> <-- BEGIN CODE -->
> 
> typedef unsigned char	UBYTE;
> typedef unsigned short	UWORD;
> typedef unsigned long	ULONG;
> 
> typedef enum  
> {
>   ENUM_ONE,    
>   ENUM_TWO,    
>   ENUM_THREE,
>   ENUM_FOUR
> } SOME_ENUMS;
> 
> typedef union
> {
>   float  alg_val;
>   ULONG  dig_val;
> 
>   struct
>   {
>     UBYTE    fourA;
>     UBYTE    fourB;
>     UWORD    fourC;
>   } indirect;
> } OBJECT_FOUR;
> 
> typedef struct
> {
>   OBJECT_FOUR   twoA;
>   OBJECT_FOUR   twoB;
>   OBJECT_FOUR   twoC;
>   OBJECT_FOUR   twoD;
>   OBJECT_FOUR   twoE;
>   UBYTE         twoF;
> } OBJECT_TWO;
> 
> typedef struct
> {
>   SOME_ENUMS    threeA;
>   UBYTE         threeB;
>   UWORD         threeC;
>   OBJECT_FOUR   threeD;
>   ULONG         threeE;
>   ULONG         threeF;
> 
>   OBJECT_FOUR   threeG;
> 
>   UBYTE         threeH;
>   UBYTE         threeJ;
> 
> } OBJECT_THREE;
> 
> typedef struct 
> {
>   UWORD         oneA;
>   OBJECT_TWO    oneB;
>   UBYTE         oneC;
>   OBJECT_THREE  oneD;
> } OBJECT_ONE;
> 
> <-- END CODE -->
> 
> 
> 
> When I use a pointer to an OBJECT_ONE object, any accesses to members
> within oneB (OBJECT_TWO) fail because the compiler hasn't put a
> two-byte pad between oneA and oneB (to preserve natural aligment).
> 
> Any help would be greatly appreciated.
> 
> Thanks in advance.
> 
> David Bevin

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.