In this topic, the highest answer received a lot of votes and even generosity. He offers the following algorithm:
void RemoveSpaces(char* source) { char* i = source; char* j = source; while(*j != 0) { *i = *j++;
My reaction to the knee-jerk reaction was that this code causes undefined behavior because i and j point to the same place in memory, and an expression such as *i = *j++; , will access the same variable twice, for others than to determine what to store, without an intermediate point. Although they are two different variables, they initially point to the same memory location.
However, I'm not sure, since I do not quite understand how two disjoint accesses in the same memory location can do any harm in practice.
Am I claiming that this behavior is undefined? And if so, are there any examples of how to rely on such a UB can cause harmful behavior?
EDIT
The relevant part of standard C, denoting this as UB, is:
C99 6.5
Between the previous and the next point in the sequence, the object must have its stored value, changed no more than once by evaluating the expression. In addition, the previous value should only be read to determine the value to be stored.
C11 6.5
If the side effect of the scalar object is independent of another side effect on the same scalar object or the value of the calculation using the value of the same scalar object, the behavior is undefined. If there are several valid orders of expression subexpression, the behavior is undefined if such an Inconsistent side effect occurs in any of the orders.
The actual meaning of the text should be the same in both versions of the standard, but I find that C99 text is much easier to read and understand.