Re: nil messaging? Is it safe?
Re: nil messaging? Is it safe?
- Subject: Re: nil messaging? Is it safe?
- From: Dietrich Epp <email@hidden>
- Date: Sat, 28 Sep 2002 08:22:11 -0700
On Friday, September 27, 2002, at 09:14 , Simon Stapleton wrote:
[localhost:~] simon% more test.c
#include <stdio.h>
main ()
{
union {
int a;
float b;
double c;
} test;
test.a = 0;
printf ("%f\n", test.b);
printf ("%f\n", test.c);
}
[localhost:~] simon% cc test.c
[localhost:~] simon% ./a.out
0.000000
0.000000
I fail to see how this is random, although I can see how it is destined
to failure on platforms where float and double aren't magically 4 bytes
long (or, to be more general, the same size as the number of zero bytes
returned). I must have missed something here, I feel.
<more snippage>
That code snippet works... it should. However, the return value of a
function is not in the stack like your union is. The return value is in
GPR3 for pointers, integers, and structs, and in some FPR (I forget
which) for floats & doubles. The reason integers are 0 is because
nil == 0. The reason others aren't is because they are in different
registers, which are not cleared.
I would consider it bad programming practice to depend on behavior that
is 'undefined', even in the current situation where nil == 0. What
happens when your program runs on a different proc with a different
ABI? It crashes, that's what.
The implementation of making messages to nil return nil is dead simple,
none of this "keeping track of selectors", the message-sending function
will just check for a nil target and return nil. The reason other types
are junk is that the compiler expects the result to be somewhere else.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.