将空指针解释为空(空字符串) [英] interpreting a null pointer as an empty (null string)

查看:99
本文介绍了将空指针解释为空(空字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个C库(当前和历史)将空指针解释为

a指向null(即空)字符串的指针?

解决方案




Dennis Allison写道:

哪个C库(当前和历史)将空指针解释为
指向null(即空)字符串的指针?




至少有一个实现(前C-89),其中

花了很大的力气将0x0放在零位置

的数据段中。


因此,NULL指针(在该实现上为零)

总是指向一个位置,内容为''\0''

表示char *,其他指针的所有零位表示

那个位置。


毋庸置疑,当

代码被移植到另一个实现时,事情以无数方式爆发。


-



这是重要的可以做任何万无一失的事,因为傻瓜是这样的b $ b巧妙。 - A. Bloch


2004年3月5日星期五19:13:35 GMT,Nick Landsberg< hu ***** @ NOSPAM.att .net>

写道:



Dennis Allison写道:

其中C库(当前和历史)将空指针解释为指向null(即空)字符串的指针?



至少有一个实现(前C) -89),在数据段中将0x0放在零位置时非常痛苦。

因此,NULL指针(在该实现上为零)
总是指向一个内容为''\0''
的位置为char *,其他指针的所有零位为
该位置。

不用可以说,当代码被移植到另一个实现时,事情以无数种方式破裂。




甚至/那个/更接近于平台试图最小化

底池在C程序(用户代码或lib函数)的情况下的实际损坏

错误地将空指针视为指向

某事的有效指针。 />

我感觉到OP正在询问是否有任何字符串处理

库总是在交给char时检查特殊情况为0 *,

并在这种情况下做一些合理的事情。


我不知道,但是如果有那么那就必须考虑一些

一种非标准的扩展,它甚至可能会冒犯人们使用字符串lib函数来回答
,因为它意味着额外的特殊情况

开销正确使用这些指针会使其变得不必要。


无论如何,作为尼克的言论的后记,我被迫发出荣誉

给早期的MSVC团队建立我认为非常好的
小hack在调试模式下的运行时系统中:它们在零位置放置一个神奇的
值,并在程序执行后检查它是否已更改

。如果是这样,一个很好的诊断(NULL指针赋值...)是

发出的。这可能为我节省了无数个小时的头脑刮擦......
-leor


Leor Zolman

BD软件
le**@bdsoft.com
www.bdsoft.com - 使用C / C ++,Java,Perl& Sons进行现场培训。 Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html


Leor Zolman< le * *@bdsoft.com>写道:

无论如何,作为尼克的言论的后记,我被迫向早期的MSVC团队表示赞赏,以确定我认为的是一个很好的小黑客在调试模式下的运行时系统中:它们在零位置放置一个神奇的值,并在程序执行后检查它是否已经改变。如果是,则发出良好的诊断(NULL指针分配...)。这可能为我节省了无数个小时的工作时间崩溃......




这比MSVC历史悠久。它绝对是在

甚至相当早期版本的Turbo C for DOS,而且我不记得
记得他们发明了这个想法的任何说法。

-

为了充分利用这本书,我强烈建议你阅读它。

- 理查德希思菲尔德


Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?

解决方案



Dennis Allison wrote:

Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?



There was at least one implementation (pre C-89), which
took great pains into putting 0x0 at location zero
in the data segment.

Thus, the NULL pointer (on that implemenation a zero)
would always point to a location with the contents ''\0''
for a char *, and all zero bits for other pointers to
that location.

Needless to say, things broke in myriad ways when
code was ported to another implementation.

--
?
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch


On Fri, 05 Mar 2004 19:13:35 GMT, Nick Landsberg <hu*****@NOSPAM.att.net>
wrote:



Dennis Allison wrote:

Which C libraries (current and historical) interpret a null pointer as
a pointer to a null (that is, empty) string?



There was at least one implementation (pre C-89), which
took great pains into putting 0x0 at location zero
in the data segment.

Thus, the NULL pointer (on that implemenation a zero)
would always point to a location with the contents ''\0''
for a char *, and all zero bits for other pointers to
that location.

Needless to say, things broke in myriad ways when
code was ported to another implementation.



And even /that/ is more to the tune of "the platform tries to minimize the
potential damage in the case when a C program (user code or lib function)
mistakenly treats a null pointer as if it were a valid pointer to
something".

I get the feeling the OP was asking if there are/were any string-handling
libraries that always check for a special-case of 0 when handed a char *,
and do some reasonable thing in that case.

I don''t know, but if there were then that would have to be considered some
sort of non-standard extension, and it might even be offensive to folks
using the string lib functions because it implies extra special-case
overhead that proper use of those pointers would have rendered unnecessary.

Anyway, as a postscript to Nick''s remarks, I''m compelled to give out kudos
to the early MSVC team for establishing what I think was an excellent
little "hack" in their runtime system in debug mode: They place a magic
value at location zero, and after program execution check to see if it has
changed. If so, a nice diagnostic ("NULL pointer assignment...") is
emitted. This has probably saved me countless hours of head-scratching over
runtime meltdowns...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


Leor Zolman <le**@bdsoft.com> writes:

Anyway, as a postscript to Nick''s remarks, I''m compelled to give out kudos
to the early MSVC team for establishing what I think was an excellent
little "hack" in their runtime system in debug mode: They place a magic
value at location zero, and after program execution check to see if it has
changed. If so, a nice diagnostic ("NULL pointer assignment...") is
emitted. This has probably saved me countless hours of head-scratching over
runtime meltdowns...



This has a much older history than MSVC. It was definitely in
even fairly early versions of Turbo C for DOS, and I don''t
remember any claims that they invented the idea.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


这篇关于将空指针解释为空(空字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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