[SDL] endian defines

Christian Biere christianbiere at gmx.de
Thu Jan 22 13:59:01 PST 2004


Glenn Maynard wrote:
> Huh?  You realize you're nitpicking dummy code, right?

I wasn't especially nit-picking. It's just that I happen to use systems
whith strict alignment rules and I often see software which works
flawlessly on Linux/x86 stumbling over it. And well for the dummy
part - excuse me - but it wasn't me who decided to use certain
C lines instead of just "/* code for little-endian systems */".

> (Programs that really do this usually increment byte_ptr two at a time,

Even worse, he didn't increase the pointer at all! Just kidding...

> so it remains
> even, but since this is only an example, that code doesn't exist.)

Yes, it *remains* even if the base address was even. Often it's not.
For example:

char buf[4096];

buf isn't necessarily aligned on any useful boundary. That's a real
world example and under Solaris the concerned program crashed sometimes
i.e., everytime buf (on the stack) wasn't aligned on a 4 byte boundary
because the code used a blatant (u_int32_t *) cast on it.

There are neat compiler dependend alignment directives but you can do
something like this to enforce a certain alignment:

u_int64_t buf64[4096 / sizeof(u_int64_t)];
char *buf = (char *)&buf64

Of course, if a function requires a certain alignment for its parameters
it should use a correct prototype i.e., not just (char *).

-- 
Christian
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040122/f057ef38/attachment.pgp>


More information about the SDL mailing list