@filpizlo Note the second calloc call is never made. (It will even get rid of the first if you don't make it volatile or otherwise force it to stick around).
@filpizlo Go compile your example in godbolt and look at what it actually outputs. It calls malloc twice, then stores 42 at the head of the first malloc call. Then it reads it back. It may skip the second malloc call entirely, depending on settings.
@filpizlo 6.5.6 Additive Operators, p2 and 8
Either both operands shall have arithmetic type, or one operand shall be a pointer to a
complete object type and the other shall have integer type
... added to or subtracted from a pointer, the result has the type of the pointer operand.
@filpizlo Fair. But all this is tangent to the observation that int+ptr and ptr+int are equally defined in the spec. They only are UB if it causes an overflow. That is independent of the UB-ness of dereferencing the resulting pointer.
Again, clang uses a[b-b] for its access.
@filpizlo I'm not sure what happens if you're on a platform where ptr is smaller than int. On normal platforms, ptr is at least as large as int, so it will be ptr sized registers used for the computation. If int is smaller than ptr, then it will get stuffed into a ptr sized register first.
@filpizlo Note that C makes the *sizeof(A) part implicit, and it's often implemented via a single special offsetting instruction (lea on amd64) as long as it isn't too big.
@MamaShellington @mathaiaus Yet the reciprocal term *has* made it into the UK lexicon about the US. If you cannot hypothesize about the correctness of a counter factual, I don't think there's anything I can do to help your abstract reasoning. So this is where I say "good day".
@filpizlo I hate to be the guy who says he's leaving, then doesnt. But pull up your b[a-b] example in Compiler Explorer. It *literally* simplifies it into a[0] at compile time. You need an example of asan failing to catch this error when the compiler doesn't out clever you in a valid way.
@filpizlo Anyway, I appreciate you saying you're intending to break from ISO C when you think it necessary. Hopefully you can stay close enough to let most programs work with both, since I think this project is neat, even if it is only used as a means to shake out more bugs.
@MamaShellington @mathaiaus Let's suppose Americans decided, en masse, that "Black and Tans" was the best slang to use to mean "anyone from the British Isles". You don't think that would be rather gauche? Especially when, by happenstance, they applied the term to a Dulbiner, or man from Belfast?
@filpizlo Clang does, if I simplify by returning instead of printing the value of a[0]. Marking a volatile doesn't solve this, since it still ends up skipping the b call. -O0 or making the function longer by leaving the print resolves this.
@filpizlo Note that 76 is a "magic value", which is how far apart calls to calloc land on my machine (amd64). You may need to adjust it for the target machine. You can deduce the value from the prior print statement. Also, at -O3 a smart compiler may remove the UB write since it's UB.
@MamaShellington @mathaiaus As I said, I don't hold it against Brits for not knowing any better. It's not like it's meant as an insult, and American politics are messy. Especially with the post reconstruction-era efforts at "unification", or at least at pretending we're unified.
@MamaShellington @mathaiaus You don't think that might have left some animosity toward the British? How do you think an Irishman would like to be called a "Black and Tan"? Because that is the rough cultrual equivalent to Yankee for the people who failed to kick out the Yankee occupiers.