strong/weak typing and pointers

Diez B. Roggisch deetsNOSPAM at web.de
Thu Nov 4 12:12:15 EST 2004


> I'm sorry, I guess I still don't understand this example.  It sounds like
> you're just going between untyped (array of bytes) and typed (functions,
> data
> structures, etc.)  I'm not seeing how you're, for example, casting a
> function to a data structure, or a data structure of one type to a data
> structure of another type.
> 
> Anything can (and sometimes should) be treated just as an array of bytes,
> in an analogous method to how in an OO language with a base class Object,
> sometimes it's appropriate to treat a given instance of a class as an
> Object, rather than
> it's particular subclass of Object.  I wouldn't consider treating
> function, data structures, etc. as arrays of bytes (or vice versa) as
> something that takes advantage of "weak-typing", but rather something that
> takes advantage of the ability to treat data as untyped.
> 
> Hopefully this clarified my confusion and when you get a chance, you can
> try to
> explain it to me again?  Thanks,


I think what Alex means is that if you allow for re-interpreting data as
simply byte arrays and the other way round, you end up with exactly the
weak typing you defined before: The arbitrary reinterpretation of memory
portions.

Consider this simple example:

int main() {
  float f = 13345.0;
  void *mem = (void*)&f;
  int *i = ((int*)mem);
  printf("%f, %i, %d\n", f, mem, *i);
}

For read-only-cases (read-only with respect to the RAM) one might be able to
allow access to the memory without opening the weak-typing loophole. But if
you want read bytes into memory, you'll end up with an array of bytes
firsthand, and whatever interpretation you impose on it by casting, there
is no way of forbidding a wrong casting.


-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list