类型安全 [英] Type safety

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

问题描述



在文章 http:// en。 wikipedia.org/wiki/Type_safety


它写成


原型类型不安全的语言是C因为(对于例子)

可以将一个整数视为一个函数指针,然后可以跳转到并执行
,导致分段等错误

错误,或(更隐蔽地)无声失败。


即使C语言的语义明确允许这些类型违反这些类型的b $ b系统(事实上,对于操作系统的性能来说,以类型不安全的方式编写的b / b
语言可能至关重要)语言永远不会定义什么时候会发生什么,

例如,浮点值被视为指针。


现在我的问题是任何人都可以为此提供示例/解释


In the article http://en.wikipedia.org/wiki/Type_safety

it is written as

The archetypal type-unsafe language is C because (for example) it is
possible for an integer to be viewed as a function pointer, which can
then be jumped to and executed, causing errors such as segmentation
faults, or (more insidiously) silent failures.

Even though the semantics of the C language explicitly allow for these
violations of the type system (indeed, it can be critical for the
performance of operating systems to be written in type-unsafe
languages) the language never defines what should happen when,
for example, a floating-point value is treated as a pointer.

now my question is can anybody give examples/explanations for this

推荐答案

El Fri,2006年2月24日09:20:28 -0800,aarklonescribi:
El Fri, 24 Feb 2006 09:20:28 -0800, aarklon escribió:

即使C语言的语义明确允许这些类型系统的违反(事实上,它对于操作系统的性能以类型不安全的语言编写是至关重要的)语言永远不会定义应该发生什么什么时候,例如,一个浮点值被视为一个指针。

现在我的问题是任何人都可以举例说明/解释

Even though the semantics of the C language explicitly allow for these
violations of the type system (indeed, it can be critical for the
performance of operating systems to be written in type-unsafe
languages) the language never defines what should happen when,
for example, a floating-point value is treated as a pointer.

now my question is can anybody give examples/explanations for this




int main()

{

int a = 0xFFFFFF;


void(* p)( )=(void(*)())a;

p();


返回0;

}



int main()
{
int a = 0xFFFFFF ;

void (*p)() = (void(*)()) a;
p();

return 0;
}


aa*****@gmail.com 写道:
在文章 http://en.wikiped ia.org/wiki/Type_safety

它写成

原型类型不安全的语言是C,因为(例如)它是
可以将一个整数视为一个函数指针,然后可以将其跳转并执行,从而导致错误,例如分段错误,或(更隐蔽地)静默失败。
语言)语言永远不会定义应该发生什么,例如,浮点值被视为指针。

现在我的问题是任何人都可以举例说明/解释
In the article http://en.wikipedia.org/wiki/Type_safety

it is written as

The archetypal type-unsafe language is C because (for example) it is
possible for an integer to be viewed as a function pointer, which can
then be jumped to and executed, causing errors such as segmentation
faults, or (more insidiously) silent failures.

Even though the semantics of the C language explicitly allow for these
violations of the type system (indeed, it can be critical for the
performance of operating systems to be written in type-unsafe
languages) the language never defines what should happen when,
for example, a floating-point value is treated as a pointer.

now my question is can anybody give examples/explanations for this




好​​吧,这篇文章有点误导。对于一个没有准备的读者,它可能会产生一种C语言的印象,允许在函数指针上下文中隐式使用整数

值,但事实并非如此。在许多

的情况下,一个人肯定可以使用显式转换来使用C类型控制系统而不使用

(例如,但依赖于隐含的to void *;

和from void *指针转换),但通常不是一步式的

过程。


事实是C语言只是提供某些手段来执行这些违反类型系统的行为。在程序中使用这些方法

在大多数情况下仍然需要用户的有意识的努力,特别是当涉及混合来自数据领域的值时。和来自代码领域的
值(如上面的整数作为函数

指针示例)。


-

祝你好运,

Andrey Tarasevich



Well, the article is a bit misleading. For an unprepared reader it might
create an impression of C language allowing implicit use of an integer
value in a function pointer context, which is not the case. In many
cases one can definitely work around the C type-control system without
using explicit cast (for example, but relying on the implicit "to void*"
and "from void*" pointer conversions), but it is normally not a one-step
process.

The truth is C language merely provides certain means for performing
these violations of the type system. Using these means in the program
still requires a conscious effort from the user in most cases,
especially when it comes to mixing values from the realm of "data" and
values from the realm of "code" (as in the above "integer as function
pointer" example).

--
Best regards,
Andrey Tarasevich


aa ***** @ gmail .com 写道:
在文章 http ://en.wikipedia.org/wiki/Type_safety

它写成

原型类型不安全的语言是C,因为(例如)它是否可以将一个整数视为一个函数指针,然后可以将其跳转并执行,从而导致诸如分段错误或(更隐蔽的)静默失败等错误。 br />
即使C语言的语义明确允许这些类型系统的违反(事实上,它对于操作系统的性能写入类型至关重要) -unsafe
语言)语言从来没有定义应该发生什么,例如,浮点值被视为指针。
In the article http://en.wikipedia.org/wiki/Type_safety

it is written as

The archetypal type-unsafe language is C because (for example) it is
possible for an integer to be viewed as a function pointer, which can
then be jumped to and executed, causing errors such as segmentation
faults, or (more insidiously) silent failures.

Even though the semantics of the C language explicitly allow for these
violations of the type system (indeed, it can be critical for the
performance of operating systems to be written in type-unsafe
languages) the language never defines what should happen when,
for example, a floating-point value is treated as a pointer.




这在很大程度上是无稽之谈。除非你*明确*转换它(

转换的结果是实现定义的),否则不能将整数视为

函数指针。该语言不允许

从浮点数到poitner类型的转换,无论是隐式还是

明确。


- -

Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www .ghoti.net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



This is largely nonsense. It is not possible to treat an integer as a
function pointer unless you *explicitly* convert it (the result of the
conversion is implementation-defined). The language doesn''t allow
conversions from floating-point to poitner types, either implicitly or
explicitly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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