STRCMP行为 [英] strcmp behaviour

查看:132
本文介绍了STRCMP行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的code:

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{    INT p值= 0;    P = STRCMP(NULL,富);    返回0;}

我得到分段错误。回声$? 139.说但是当我运行

 的#include<&stdio.h中GT;INT主(INT ARGC,CHAR *的argv [])
{    INT p值= 0;    STRCMP(NULL,富);    返回0;}

我没有得到任何分段错误。可能有人请扔掉一些轻?

下面是我的gcc信息:

 > GCC --version
海湾合作委员会(GCC)3.4.6 20060404(红帽3.4.6-8)


解决方案

您可能正在使用的优化选项编译时。由于的strcmp()在第二个片段,结果被忽略编译器消除了这一函数调用,这就是为什么你的程序不会崩溃。此调用只能因为的strcmp()是一种内在函数,编译器知道这个函数没有任何副作用。被淘汰

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");

    return 0;

}

I dont 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.

这篇关于STRCMP行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆