How does clang handle the pointer not accessed? (original) (raw)
I was working on a problem about address sanitizer and I found something intersting. Here is the issue.
Overall, the code below will have 50% chance to detect memory leak with -fsanitize=address
(0% without the line with comment).
struct Foo {
struct Foo *other;
};
int main() {
Foo *f1 = new Foo();
Foo *f2 = new Foo();
f1->other = f2;
f2->other = f1;
std::cout<<&f1<<" "<<&f2<<"\n"; // this line is necessary to make leak detection possiable with -fsanitize=address
return 0;
}
Why this happened? I checked the assembly clang generate here, and I found nothing special.
I’m not sure if this is caused by clang or llvm.
The main info I want to know is “will clang do something extra or optimization to the pointer that will be accessed or won’t be accessed?”
Could someone help? Any tips might be helpful!