last day (15 days later) » 

5:12 PM
 
int foo(restrict int *a, restrict int *b)
{
int *a1 = a+1;
if (a1==b) *a1=1;
return *b;
}
void kaboom(void)
{
int arr[2] = {0,0};
foo(arr, arr+1);
}
 
jxh
I see, but you have created an alias. But, why is reading *b UB?
 
The restrict qualifer.
 
jxh
The promise b is not aliased?
 
Correct.
If a1 is never dereferenced, it's not an alias of b even though it's equal.
 
jxh
5:20 PM
Interesting, but that is not really the use case I am trying to validate.
Is the issue that answering my specific case is hard because of the general issues of aliasing?
 
No, but my point is that C has lots of nasty aliasing rules, and the fact that two things identify the same address does not mean they can be used interchangeably.
My first example had more to do with the kinds of problems that arise with type-punning pointers, but my last example was intended to illustrate that even pointers of the same type which compare equal are not equivalent.
 
jxh
I think the first example is valid. The definition of compatible types allows for this, I believe.
 
Given typedef {int a;} m0, struct {int a;} m1; void *p = malloc(sizeof m0); m0 *p0 = (m0*)p; m1 *p1 = (m0*)p;
 
jxh
Oops, I was wrong. The compatible definition says they have to have the same tag name too.
 
A fundamental problem with C is that the language was originally designed to specify an abstract machine with some number of amount of storage cells of type unsigned char[], whose state was entirely defined by the call stack and the contents of those storage cells.
 
jxh
5:26 PM
I am after pointer conversions between compatible types of objects
 
Such a language definition allows programmers to perform many optimizations which are not possible in other languages, but limits the optimizations compilers can do. Around 1999, someone decided that C should be replaced with a new language, with semantics that are similar to C but don't quite match, but the new language should still be called "C".
In your example, you compare p to &x.z.r, but give no indication what you would expect to do on the basis of that comparison. Under the "storage cells of type char[]" abstract machine, it wouldn't matter, but on "new C" abstract machine it does. The Standard says that the comparison will yield 1, but the fact that two pointers compare equal does not imply equivalence.
Incidentally, I rather liked the old language. I despise the new one. The goal of a good compiler should be to help a programmer produce an executable that meets requirements. The goal of hyper-modern compilers is to produce the most "efficient" executable whose behavior would be justifiable under the Standard, without regard for whether such an executable would serve any useful purpose.
If one wants to have a language which doesn't allow memory to be treated as a bunch of char[] storage, one should start with something like Modula-2 and add ways of packing and unpacking binary storage, rather than starting with a language whose design philosophy is so thoroughly predicated on the "everything boils down to char[]` notion.
 
jxh
5:42 PM
Sorry, pulled away. Let me mull this over and perhaps I'll strike up the conversation again in the future.
 
jxh
6:14 PM
@supercat: I realize the aliasing rules kind of throw a wrench into the analysis, but it does not negate that the standard states a pointer to a structure also points to its initial member and vice versa. That statement should mean you can cast the pointer to the initial member, use it, and cast it back. It seems you are taking exception to even that interpretation, regardless of the use of void *.
 
6:43 PM
By my understanding of the Strict Aliasing rule, the compiler can assume that a program will never receive input that would cause memory written using one type of pointer to be read using another, outside of some non-transitively-defined cases.
Casting a pointer to the type of the first element of a structure should work. Accessing the first element of a structure by dereferencing a pointer to the structure is not equivalent, however. One would think that if foo->bar is an int, then writing to foo->bar would be equivalent to writing to (int*)&(foo->bar), but it's not.
A compiler is allowed to make assumptions about what objects might be modified by a write to foo->bar, which go beyond those it could make about what objects might be modified by a write to an arbitrary int*; I think, though I'm not quite sure, that the cast to int* would eliminate a compiler's authority to make some of those assumptions about aliasing, though I'm not quite clear on the cases where compilers are allowed to extend such inferences through typecasts.
 

  last day (15 days later) »