lkml.org 
[lkml]   [2002]   [Jul]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: Diff b/w 32bit & 64-bit
Thunder from the hill wrote:
> > > don't cast from "foo *" to "bar *" if sizeof(foo)<sizeof(bar)
> >
> > What is the reason for this? I do it quite routinely ("poor man's
> > inheritance").
>
> This should only be OK if you pad bar before.

Erm, crossed wire :-)

I do it for inheritance in the same way as, say, Xlib with X events.
That's perfectly safe and commonplace.

I didn't understand Albert's reason for prohibiting the mere cast.
(Notions of old machines where the char * representation is different
from int * came to mind, but Linux doesn't run on those... does it?)

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.).

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'.

-- Jamie
-
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/

\
 
 \ /
  Last update: 2005-03-22 13:22    [W:0.273 / U:0.152 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site