This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: structure alignment question


Gary Thomas wrote:
On Thu, 2004-02-05 at 22:45, mohanlal jangir wrote:

I have some questions regarding structure alignment. If I declare a
structure like

struct structure {
 unsigned short  a;
 unsigned char   b;
 unsigned char pad[1];
};

and then same structure like
struct structure {
 unsigned short  a;
 unsigned short   b;
};

Are both declaration free from alignment problem for any kind of access or
it's only later one?

If used as they've been declared they are free from alignment problems. If you access one via a pointer to the other, e.g. for the first structure:
struct structure foo;
((unsigned short *)&foo.b) = 1;


then that is illegal according to the C language definition - it's called pointer aliasing and there are rules that prevent it in all but a few exceptions. GCC could and has been known to miscompile code because of people doing things like the above.

Of course there's also the issue of endianness to consider.

Instead you should use a union if you want that type of feature, e.g.

struct structure {
  unsigned short a;
  union b {
     unsigned short bshort;
     unsigned char bchar[2];
  };
};

although that still doesn't address the endianness issue.

Another question is, if I have a structure like
struct structure {
   unsigned int a;
   unsigned short b;
   unsigned char c;
};

struct structure temp;
unsigned int *p = &(temp.a);
unsigned short *q = &(temp.b);
unsigned char *r = &(temp.c);

Do all pointers (p, q and r) need to be 4 byte boundary aligned or p at 4
byte boundary, q at 2 byte boundary and r at 1 byte boundary will do?

If you have defined temp as you have written, then you don't need to worry about alignment - the compiler will make sure the structure and its members are aligned correctly for you.


If however you did something like:

struct structure *ptemp;
ptemp=(struct structure *)0x87654321;
ptemp->a = 1;

then this wouldn't work as you've used a cast to explicitly misalign the structure.

The answers you seek are architecture (and even ABI) dependent. You should ask on the GCC list to get authoratative answers.

Not in this case as fortunately this type of thing is defined in the C standard.


Jifl
--
eCosCentric    http://www.eCosCentric.com/    The eCos and RedBoot experts
>>>>> Visit us in booth 2527 at the Embedded Systems Conference 2004 <<<<<
March 30 - April 1, San Francisco http://www.esconline.com/electronicaUSA/
--["No sense being pessimistic, it wouldn't work anyway"]-- Opinions==mine


-- Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]