gcc编译,然后运行下面代码的结果会是什么?
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
char *s1;
char *s2;
char src[]="Notification-ASN1Module";
s1=(char *)malloc(sizeof(src)+1);
if (0 == s1){
exit(-1);
}
s2=0;
strcmp(s1,s2);
return 0;
}
A>什么都不发生
B>发生段错误
答案:A。
以前不知道gcc编译器还可以优化这种情况,如果不使用strcmp的返回值的话,这 个函数压根就不会调用.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
char *s1;
char *s2;
char src[]="Notification-ASN1Module";
s1=(char *)malloc(sizeof(src)+1);
if (0 == s1){
exit(-1);
}
s2=0;
strcmp(s1,s2);
return 0;
}
A>什么都不发生
B>发生段错误
答案:A。
以前不知道gcc编译器还可以优化这种情况,如果不使用strcmp的返回值的话,这 个函数压根就不会调用.