When I run the following code:
#include <stdio.h>
int main(int argc, char *argv[])
{
int p = 0;
p = strcmp(NULL,"foo");
return 0;
}
I get segmentation fault. echo $? says 139. But when I run
#include <stdio.h>
int main(int argc, char *argv[])
{
int p = 0;
strcmp(NULL,"foo"); // Note removed assignment
return 0;
}
I don't get any segmentation fault. Could someone please throw some light?
Here is my gcc info:
> gcc --version
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-8)
You are probably using optimization options when compiling. Since the result of strcmp()
in the second snippet is ignored the compiler eliminates this function call and this is why your program does not crash. This call can be eliminated only because strcmp()
is an intrinsic function, the compiler is aware that this function does not have any side effects.