It seems like the C/C++ compiler (clang, gcc, etc) produces different output related to the optimization level. You may as well check the online link included in this post.
http://cpp.sh/5vrmv (change output from none to -O3 to see the differences).
Based on the following piece of code, could someone explain a few questions I have:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = (int *)malloc(sizeof(int));
free(p);
int *q = (int *)malloc(sizeof(int));
if (p == q) {
*p = 10;
*q = 14;
printf("%d", *p);
}
return 0;
}
- Is it certain that the execution will always get into the if statement? How do we know the addresses of the two pointers, p and q, will be the same?
- Why does no-optimization has output 14, while -O3 has output 10 for the same instructions?