strcmp的NULL参数 [英] NULL argument to strcmp

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

问题描述

关于传递给strcmp的NULL值的标准是什么?

例如,

static char * s1 =" xxx" ;;

static char * s2 = NULL;

int n = strcmp(s1,s2);

这应该合法,还是UB?


我的惠普处理好了 - 手册页说它认为空值

与空字符串相同。


我的Sun Solaris崩溃 - 其手册页没有说明NULL值。

-

Fred L. Kleinschmidt

波音助理技术研究员
技术架构师,通用用户界面服务

M / S 2R-94(206)544-5225

解决方案



" Fred L. Kleinschmidt" < fred.l.kleinschmidt@nospam_boeing.com>写在

消息新闻:41C0A010.87BB89B2@nospam_boeing.com ...

标准对于传递给strcmp的NULL值有什么看法?


行为未定义。

例如,
static char * s1 =" xxx";
static char * s2 = NULL;
int n = strcmp(s1,s2);
这应该是合法的,还是UB?


UB。

我的HP处理好了 - 手册页说它认为NULL值和空字符串一样。


这是标准不需要添加的功能。

我的Sun Solaris崩溃 - 其手册页没有说明NULL值。




首先请注意,您描述的行为不是这些系统固有的,而是每个系统使用的C实现。


如果你想让你的代码更健壮和可移植,请检查

并阻止传递NULL指针。


if(s1&& s2)

int n = strcmp(s1,s2);

else

puts("你在做什么,戴夫?\ n"

"我的记忆正在......我可以

" feeeeel it .... ;


当然这只是部分保护,它不允许

NULL指针,但不会阻止''垃圾''指针

(例如未初始化或''免费()d'')。


-Mike


Mike Wahler写道:
当然这只是部分保护,它不允许空指针,但不会阻止''垃圾'指针
(例如uninitialized或''free()d'')。




如何检测到free''d指针,我想知道?

(对于未初始化的指针,它们通常由编译器检测,

不是它们)


" Fred L. Kleinschmidt" ;写道:


关于将值传递给
strcmp的标准是什么?例如,
static char * s1 =" xxx";
static char * s2 = NULL;
int n = strcmp(s1,s2);
这是否合法或者它是UB吗?

我的HP处理它没问题 - 手册页说它认为NULL值和空字符串一样

我的Sun Solaris崩溃了 - 它的手册页没有说明NULL
值。




标准说字符串函数的空参数产生

未定义的行为。这意味着表现得好像是一个空的

字符串是合法的。所以是沉闷的砰砰声。因此,对伊朗的入侵发起了




-

Chuck F(cb ******** @ yahoo.com)(cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http ://cbfalconer.home.att.net>使用worldnet地址!


What does the standard say about a NULL value being passed to strcmp?
For example,
static char *s1 = "xxx";
static char *s2=NULL;
int n = strcmp(s1,s2);
Should this be legal, or is it UB?

My HP handles it OK - the man page says it considers NULL values the
same as empty strings.

My Sun Solaris crashes - its man page says nothing about NULL values.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225

解决方案


"Fred L. Kleinschmidt" <fred.l.kleinschmidt@nospam_boeing.com> wrote in
message news:41C0A010.87BB89B2@nospam_boeing.com...

What does the standard say about a NULL value being passed to strcmp?
The behavior is undefined.
For example,
static char *s1 = "xxx";
static char *s2=NULL;
int n = strcmp(s1,s2);
Should this be legal, or is it UB?
UB.

My HP handles it OK - the man page says it considers NULL values the
same as empty strings.
That''s an added ''feature'' not required by the standard.
My Sun Solaris crashes - its man page says nothing about NULL values.



First note that the behaviors you describe are not inherent to
those systems, but to the C implementations you use on each.

If you want your code to be more robust and portable, check for
and prevent passing a NULL pointer.

if(s1 && s2)
int n = strcmp(s1,s2);
else
puts("What are you doing, Dave?\n"
"My memory is going... I can"
" feeeeel it....");

Of course this is only partial protection, it disallows
NULL pointers, but does not prevent ''garbage'' pointers
(e.g. uninitialized or ''free()d'').

-Mike


Mike Wahler wrote:

Of course this is only partial protection, it disallows
NULL pointers, but does not prevent ''garbage'' pointers
(e.g. uninitialized or ''free()d'').



How would a free''d pointer be detected, I was wondering ?
(as to uninitialised pointers, they are usually detected by a compiler,
aren''t they)


"Fred L. Kleinschmidt" wrote:


What does the standard say about a NULL value being passed to
strcmp? For example,
static char *s1 = "xxx";
static char *s2=NULL;
int n = strcmp(s1,s2);
Should this be legal, or is it UB?

My HP handles it OK - the man page says it considers NULL values
the same as empty strings.

My Sun Solaris crashes - its man page says nothing about NULL
values.



The standard says that null arguments to string functions produce
undefined behaviour. That means that acting as if it were an empty
string is legal. So is crashing with a dull thud. So is launching
an invasion of Iran.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


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

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