Re: Diff b/w 32bit & 64-bit

From: Albert D. Cahalan (acahalan@cs.uml.edu)
Date: Mon Jul 08 2002 - 15:34:24 EST


Jamie Lokier writes:

>>>> don't cast from "foo *" to "bar *" if sizeof(foo)<sizeof(bar)
...
> Oliver Neukum explained that you shouldn't dereference a pointer to a
> larger type because of alignment issues on some machines.
> sizeof(foo)<sizeof(bar) captures this rule just fine for the basic data
> types (char, int etc.).

Yes, that was the intent.

> But for structures, it's actually possible to have a smaller type with a
> larger alignment requirement, and vice versa:
>
> struct small { double x; };
> struct large { char y [11]; };
>
> Also, it is certainly permitted to cast "char *" to "int *" if you know
> that the underlying object is an "int" or something compatible with one.
>
> So, the general rule `don't cast from "foo *" to "bar *" if
> sizeof(foo)<sizeof(bar)' is wrong, and is routinely not followed.
>
> An alternative rule might be `never dereference a "bar *" if it might
> not have the correct alignment for "bar" on any platform'.

Got a shorter way to say that? In less than one line, give
a rule that will keep x86-centric programmers from getting
hurt by the alignment restrictions.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Mon Jul 15 2002 - 22:00:13 EST