From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Ing-Simmons To: tim@franck.Princeton.EDU Cc: egcs@egcs.cygnus.com, george@moberg.com Subject: Re: Clarification anyone? -- was Re: Linux and aliasing? Date: Tue, 08 Jun 1999 10:33:00 -0000 Message-id: <199906081732.SAA13273@tiuk.ti.com> References: <199906081731.KAA16675@franck.Princeton.EDU> <199906081731.KAA16675@franck.Princeton.EDU> X-SW-Source: 1999-06/msg00293.html Tim Hollebeek writes: > >Let me try to give a simple definition, since the discussion so far >has been dominated by technical details. It has nothing to do with >pointer casts, though they figure prominently in lots of code that >violates the ANSI assumptions. The "main idea" is that when you >declare something to be of a particular type, that's what it's type >actually is. When you access that object, you must access it through >pointers with the correct type (pointer to whatever) or a pointer type >designed to represent raw memory (pointer to char). > >This is actually suprisingly useful information for a compiler. >Consider something like: > >void count_and_set(blah *foo, double *z, double v) { > while (*z) { > foo->bar->count++; > *z++ = p; > } >} > >(&foo->bar) is obviously a loop invariant and can be hoisted out of >the loop, but not in "traditional" C! z might point into foo >somewhere, so the write through z might change the value of foo->bar. >ANSI C says such code is nonsense, and if you really meant to play >fast and loose with the type system, you should use char *'s, >memcpy's, etc. Does not sound too bad so far - so can someone give an example of the code that would be broken by that? So ... just to check my understanding. I assume unions are exempt from this? e.g. typedef union {float f; struct { #if BIG_ENDIAN unsigned sgn:1; unsigned exp:8; unsigned mant:23; #else unsigned mant:23; unsigned exp:8; unsigned sgn:1; #endif } s; } ieee_sp; What is proposed is that any mem-assign can be considered to "clobber" any value of the same type, and an assign via a char * to clobber anything ? And thus problem is that if I do this: int a; *((short *) &a) = 123; Then 'a' is not considered clobbered? Even in the same function? -- Nick Ing-Simmons Via, but not speaking for: Texas Instruments Ltd. From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nick Ing-Simmons To: tim@franck.Princeton.EDU Cc: egcs@egcs.cygnus.com, george@moberg.com Subject: Re: Clarification anyone? -- was Re: Linux and aliasing? Date: Wed, 30 Jun 1999 15:43:00 -0000 Message-ID: <199906081732.SAA13273@tiuk.ti.com> References: <199906081731.KAA16675@franck.Princeton.EDU> X-SW-Source: 1999-06n/msg00293.html Message-ID: <19990630154300._IphRfSt9q77cdVEp-kqb8vKHpRtoe67X9yFPAwFVZc@z> Tim Hollebeek writes: > >Let me try to give a simple definition, since the discussion so far >has been dominated by technical details. It has nothing to do with >pointer casts, though they figure prominently in lots of code that >violates the ANSI assumptions. The "main idea" is that when you >declare something to be of a particular type, that's what it's type >actually is. When you access that object, you must access it through >pointers with the correct type (pointer to whatever) or a pointer type >designed to represent raw memory (pointer to char). > >This is actually suprisingly useful information for a compiler. >Consider something like: > >void count_and_set(blah *foo, double *z, double v) { > while (*z) { > foo->bar->count++; > *z++ = p; > } >} > >(&foo->bar) is obviously a loop invariant and can be hoisted out of >the loop, but not in "traditional" C! z might point into foo >somewhere, so the write through z might change the value of foo->bar. >ANSI C says such code is nonsense, and if you really meant to play >fast and loose with the type system, you should use char *'s, >memcpy's, etc. Does not sound too bad so far - so can someone give an example of the code that would be broken by that? So ... just to check my understanding. I assume unions are exempt from this? e.g. typedef union {float f; struct { #if BIG_ENDIAN unsigned sgn:1; unsigned exp:8; unsigned mant:23; #else unsigned mant:23; unsigned exp:8; unsigned sgn:1; #endif } s; } ieee_sp; What is proposed is that any mem-assign can be considered to "clobber" any value of the same type, and an assign via a char * to clobber anything ? And thus problem is that if I do this: int a; *((short *) &a) = 123; Then 'a' is not considered clobbered? Even in the same function? -- Nick Ing-Simmons Via, but not speaking for: Texas Instruments Ltd.