Expanded: optimize any call to a known-impure function to a random valid return value for it, assuming the caller must produce same result regardless.
-
-
Show this threadThanks. Twitter will use this to make your timeline better. UndoUndo
-
-
-
Why would this be valid? Gcc docs only say that __attr__((pure)) functions must have no effects except the return value, and return value depends only on params and global variables. Compiler cannot prove that malloc might fail during an __attr__((pure)) function call.
-
Indeed gcc's definition allows global reads so it's questionable. I was thinking something in between gcc's pure & const. Surely the optimization is valid for const though.
-
If the function is attr-const it must return the same thing it would if called in a context where malloc would fail.
-
What if no such context occurs in my program?
-
It's not under the program's control. Aside from necessarily failing after somewhere on the order of 2^(CHAR_BIT*sizeof(void*)) objects are in existence, it's up to the implementation if/when malloc fails. There is no context where it "can't fail".
-
The program is under no obligation to attempt to create so many objects simultaneously. Even if it did so, it's under no obligation to call that __attr__((const)) function in such a state.
-
I think you misread. The point was that under extreme conditions the implementations is forced to make malloc fail, but it's never forced to make it succeed. Failure is always an option.
-
In particular on any implementation where the invoking user can control resource limits, it's always possible for any particular call to malloc to fail.
- 11 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.