退化strcmp [英] Degenerate strcmp

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

问题描述

我见过strcmp(char * s1,char * s2)实现的一种方法是:如果s1 == s2(指针相等),则立即返回

;否则通常会在s1和s2搜索内存。


当然这样做的原因是为了节省时间以防等于

指针传递给strcmp。但在我看来,这可能会造成

在退化情况下的不一致,当s1指向内存时,

不是空终止的,即通过一些怪异的机会,所有从
s1到计算机到达其所有内存页面的结尾(但是

有效)的内存不包含单个空字节。在这种情况下,strcmp

不应该说s1和s2是相等的字符串。因为两者都不是

实际上是一个字符串(因为没有空终止)。


我的想法是否正确?


-

问:每个阅读障碍者心灵的问题是什么?
存在主义者?

答:是有狗吗?

One way I''ve seen strcmp(char *s1, char *s2) implemented is: return
immediately if s1==s2 (equality of pointers); otherwise do the usual
thing of searching through the memory at s1 and s2.

Of course the reason for doing this is to save time in case equal
pointers are passed to strcmp. But it seems to me that this could create
an inconsistency in the degenerate case when s1 points to memory that is
not null-terminated, i.e. by some freak chance, all of the memory from
s1 till the computer reaches the end of all its memory pages (however
that works) don''t contain a single null byte. In this case, strcmp
should not say that s1 and s2 are "equal strings" since neither is
actually a string (because not null terminated).

Is my thinking correct?

--
Q: "What is the burning question on the mind of every dyslexic
existentialist?"
A: "Is there a dog?"

推荐答案

fi ** ****@invalid.com 写道:

>

我见过strcmp的一种方式(char * s1,char * s2)实现的是:如果s1 == s2(指针相等),则立即返回

;否则通常会在s1和s2搜索内存。


当然这样做的原因是为了节省时间以防等于

指针被传递给strcmp。

但在我看来,这可能会导致

在s1指向

到内存是

没有空终止,即通过一些怪异的机会,所有内存从

s1直到计算机到达所有结束它的内存页面(但是有效的
)不包含单个空字节。在这种情况下,strcmp

不应该说s1和s2是相等的字符串。因为两个都不是
实际上是一个字符串(因为没有空终止)。


我的想法是否正确?
>
One way I''ve seen strcmp(char *s1, char *s2) implemented is: return
immediately if s1==s2 (equality of pointers); otherwise do the usual
thing of searching through the memory at s1 and s2.

Of course the reason for doing this is to save time in case equal
pointers are passed to strcmp.
But it seems to me that this could create
an inconsistency in the degenerate case when s1 points
to memory that is
not null-terminated, i.e. by some freak chance, all of the memory from
s1 till the computer reaches the end of all its memory pages (however
that works) don''t contain a single null byte. In this case, strcmp
should not say that s1 and s2 are "equal strings" since neither is
actually a string (because not null terminated).

Is my thinking correct?



No.

strcmp的行为仅定义为

情况,当s1和s2都指向字符串。

如果它不是空终止,它不是一个字符串。


如果没有定义代码的行为,

正在运行的程序可以做任何想做的事。

这就是C编程语言的规则。


-

pete

No.
The behavior of strcmp is only defined for
cases when s1 and s2 both point to strings.
If it''s not null terminated, it''s not a string.

In cases where the behavior of the code is not defined,
the running program can do whatever it wants.
That''s the rules of the C programming language.

--
pete


2007年8月17日23:01,pete写道:
On 17 Aug 2007 at 23:01, pete wrote:
fi******@invalid.com 写道:

>>
我见过strcmp(char * s1,char * s2)实现的一种方法是:如果s1 == s2(指针相等),立即返回
;否则通常在s1和s2搜索内存。

当然这样做的原因是为了节省时间,以防将相同的指针传递给strcmp 。
但在我看来,这可能会在退化的情况下产生不一致的情况,当s1指向没有空终止的记忆时,即通过一些怪异的机会,从
s1到计算机到达所有内存页面的所有内存(但是
有效)不包含单个空字节。在这种情况下,strcmp
不应该说s1和s2是相等的字符串。因为它们实际上都不是一个字符串(因为没有终止)。

我的想法是否正确?
>>
One way I''ve seen strcmp(char *s1, char *s2) implemented is: return
immediately if s1==s2 (equality of pointers); otherwise do the usual
thing of searching through the memory at s1 and s2.

Of course the reason for doing this is to save time in case equal
pointers are passed to strcmp.
But it seems to me that this could create
an inconsistency in the degenerate case when s1 points
to memory that is
not null-terminated, i.e. by some freak chance, all of the memory from
s1 till the computer reaches the end of all its memory pages (however
that works) don''t contain a single null byte. In this case, strcmp
should not say that s1 and s2 are "equal strings" since neither is
actually a string (because not null terminated).

Is my thinking correct?



No.

strcmp的行为仅定义为

情况,当s1和s2都指向字符串。

如果它不是空终止,它不是一个字符串。


No.
The behavior of strcmp is only defined for
cases when s1 and s2 both point to strings.
If it''s not null terminated, it''s not a string.



你的权利,一个字符串必须为空终止,但对于随机的

内存也许偶然的机会就是没有任何空字节。


例如,对于程序


main(){printf("%d \ n",strlen(malloc) (0)));这将打印出一个随机数,但原则上strlen调用

可能永远不会终止,如果malloc返回指针的内存

没有任何空字节。 (好吧,这不太可能,但理论上可能会发生
......)

Your right that a string has to be null terminated, but for random
memory maybe by chance there just isn''t any null byte.

For example, for the program

main() { printf("%d\n",strlen(malloc(0))); }

this will print out a random number, but in principal the strlen call
might never terminate, if the memory at the pointer returned by malloc
doesn''t have any null bytes. (OK, it''s very unlikely, but it could
happen in theory...)


>

如果没有定义代码的行为,

正在运行的程序可以做任何想做的事。

这就是C的规则编程语言。


-

pete
>
In cases where the behavior of the code is not defined,
the running program can do whatever it wants.
That''s the rules of the C programming language.

--
pete



-

Hlade的定律:

如果你有困难的任务,把它交给一个懒人 -

他们会找到一个更容易的方法。

--
Hlade''s Law:
If you have a difficult task, give it to a lazy person --
they will find an easier way to do it.


fi******@invalid.com 已写:

>

2007年8月17日23:01,pete写道:
>
On 17 Aug 2007 at 23:01, pete wrote:


但是对于随机的

内存可能是偶然的,没有任何空字节。
but for random
memory maybe by chance there just isn''t any null byte.


如果没有定义代码的行为,

正在运行程序可以做任何想做的事。

这就是C编程语言的规则。
In cases where the behavior of the code is not defined,
the running program can do whatever it wants.
That''s the rules of the C programming language.



你明白我在这里引用了什么吗?


-

pete

Do you understand what I''ve quoted of myself here?

--
pete


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

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