3 ms·
Can you elaborate on this? Does "unrelated" have any special meaning in this context? Why is this operation undefined?
by ghostpepper 7y ago
Can you elaborate on this? Does "unrelated" have any special meaning in this context? Why is this operation undefined?
- saagarjha 7y agoIn C, accessing data outside the bounds of an object is undefined (like, you can’t legally “go past the end” of an array and end up in some other object). The “start” and “end” pointers, as far as the compiler is concerned, are totally different objects, so it may optimize a loop from one pointer to the other out since it’s impossible to increment an address so it’ll go from pounding at one thing to another.
- ghostpepper 7y agoSo is the idea that just because the two objects happen to appear sequentially in memory under a certain implementation, the compiler (or linker in this case?) has no obligation to ensure that assumption holds?
- saagarjha 7y agoCorrect. (Although, the pointers being defined that way seems to make it quite unlikely that the compiler could optimize this incorrectly…)
- 08-15 7y agoWhere is data outside any object accessed? Note that forming a pointer just past the end of an object is well-defined; only dereferencing it isn't.