chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] More on aliasing.


From: Peter Keller
Subject: Re: [Chicken-users] More on aliasing.
Date: Mon, 8 Jul 2002 13:59:02 -0500
User-agent: Mutt/1.2i

On Mon, Jul 08, 2002 at 02:24:00PM -0400, Anthony Carrico wrote:
> I assume you are ok if you always assign the whole union (like this):
>   w2 = w1;
> or if you assign a known integer:
>   w2.integer = i;
>   j = w2.integer;
> or if you assign a known pointer:
>   w2.ptr = &i;
>   p = w2.ptr;
> I think you can only get in trouble if you assign an integer and retreive
> a pointer:
>   w2.integer = i;
>   bad = w2.ptr;
> or vise versa. In other words, when you actually do commit a (dynamic)
> type error. Is this correct?

Yes, you are correct. You get in trouble when you assign one type and
retrive another where the information intended to be valid in both types.
Mostly you get nasty problems in sign extension. Suppose 0xffffffff
integer but it is actually a pointer to 0x00000000ffffffff the assignment
from the integer to the pointer lvalvue might sign extend. Other times
you just get loss of data:

pointer: 0xaabbccdd11223344
integer: 0xaabbccdd
The least significant 4 bytes are lost when looking at the pointer as
an integer in the union since the types are "left justified" in the memory
allocated to the union.

> On Mon, 8 Jul 2002, felix wrote:
> 
> > Yep, many times. Internally everything is handled with C_word objects,
> > Also, the FFI does a lot of dirty casting between pointers and C_word's.
> 
> Is there any reason to assume the C compiler is better off casting and
> masking instead of using unions + bitfields + structs? It seems to me (at
> least in theory) that the more accurate the information you share with the
> compiler the better. This is the kind of thing C is supposed to be good
> at. (Obviously it would be a lot of work to actually change the code
> though).

Well, in machine generated C code, I can see why the current method was
used. Since a larger code generates the C code, it knows when type problems
might occur and you get a warning at the meta level. But in human written
code, people often use the struct+union method for this just to keep 
everything straight both in the code and the mind.

The thing that gets you is when things like long long aren't contiguous in
memory. This wouldn't have been even noticed in the struct+union method,
but would be a creeping error in the casting method.

Thank you.

-pete





reply via email to

[Prev in Thread] Current Thread [Next in Thread]