
13 Apr
2011
13 Apr
'11
1:17 a.m.
On 4/12/2011 3:41 PM, Nevin Liber wrote:
To illustrate this, take the following code:
struct Silly { bool is_from(const char* p) { return buffer<= p&& p< buffer + sizeof(buffer); }
char buffer[1024]; };
I believe that a compiler could legally optimize the body of Silly::is_from to be: { return true; }
because in the defined behavior case it returns true, and the compiler can do whatever it wants in the undefined behavior case.
Once again if is_from() was implemented the following way: bool is_from(const char* p) { return p - buffer > 0 && p - buffer < sizeof(buffer); } Then the undefined behaviors would seem to go away. -Phil