Consider the following code:
#include <iostream>
struct A {};
struct B {};
int func1(A *a, B *b, int c, double *d) {
int tmp = 0;
tmp = tmp;
return 1;
}
int func2(A *a, B *b, int c, double *d) {
return 1;
}
int main(int argc, char* argv[]) {
if (func1 == func2) {
std::cout << "equal" << std::endl;
} else {
std::cout << "not equal" << std::endl;
}
return 0;
}
When compiled in Release configuration in VS2013 it prints out “equal”. I have a library that depends on comparison of function addresses. You can imagine that it doesn't work quite alright in Release. Is there a way to prevent this kind of optimization in VC++? Or should I file a bug?
This is a "feature" of Microsoft's linker, and the documentation warns you that
Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members.
You can turn it off by passing /opt:noicf
to the linker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With