I'm afraid you've lost me completely.  I thought the OP was
complaining about mps430-gcc not generating correct code for
accessing misaligned fields in packed structs.

Where is the test case? there are many ways to generate misaligned access, an example (with Makefile or command line arguments) that replicates the problem would make it easier to find. I have seen one case and not been able to replicate it all the time, I think it was something to do with passing pointers to structures to functions.

But how efficient it can be? Better to have good _design_
right from the start.
If you don't get to define the layout of the data in memory, I
don't see how you can "design" your way around doing byte by
byte access of misaligned values.
char array[128] is always in sequence and you have control of how that is aligned in memory. You put them in in sequence and incrementing a pointer to them always gets you the characters in sequence.

Right.

now,

struct
{
        char x;
        int y;
        char z;
        long b;
        char v[128];
}

Can be in any sequence the compiler likes to lay it out, even
'packed' (use minimum memory).

No, it can't.  The C standard requires the fields to be layed
out in memory in the sequence in which they're declared, and
that's what gcc does.  The compiler is allowed to place padding
"between" fields, but it isn't allowed to rearrange.
Technically, I believe it can if it can be known at compile
time that such a rearrangment will not affect program
execution.  But I'm not aware of any compilers that can do
that.

Ok if a rearrangement (and I agree it may not be allowed) does not affect the code the compiler is building, and another compiler also does the same but in a different way then its ok.

The same problems happen with big end-in and little end-in both will pack the long in a different order, both are functional for the code but copying the memory from one machine (byte for byte) to another wont work.

Incrementing a pointer to this struct will not always get you
x then y high, y low etc...

Incrementing a pointer to that struct will get you a pointer to
the memory _after_ that struct, not a pointer members of that
struct.

Ok incrementing a pointer to a byte array won't.

Using packed structs for things like communication protocol
structures works very well (at least on unbroken compilers).
Using an array instead make the code hard to maintain.

Generally it works, although its not always going to work (even on non-broken compilers).

If your using pointers to type cast between variable types (or
structs) then you should be carful, type casting with pointers
is dangerous, and the compiler tries to warn you of this where
it can.

I wasn't aware we were discussing type casting...

Ok I thought that you were trying to use your packed structure to get an array of bytes to send/receive over a communications channel. This is a type conversion between a structure and an array of bytes.



--
Peter Jansen
STS
Antarctic Division
203 Channel Highway
Kingston
TAS  7050
AUSTRALIA
Phone +61 3 6232 3533

___________________________________________________________________________

   Australian Antarctic Division - Commonwealth of Australia
IMPORTANT: This transmission is intended for the addressee only. If you are not 
the
intended recipient, you are notified that use or dissemination of this 
communication is
strictly prohibited by Commonwealth law. If you have received this transmission 
in error,
please notify the sender immediately by e-mail or by telephoning +61 3 6232 
3209 and
DELETE the message.
       Visit our web site at http://www.antarctica.gov.au/
___________________________________________________________________________

Reply via email to