请问C标准强制要求的平台不能定义超出标准给出的行为 [英] Does C standard mandate that platforms must not define behaviors beyond those given in standard

查看:127
本文介绍了请问C标准强制要求的平台不能定义超出标准给出的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准明确指出,一个编译器/库组合是允许做任何它具有以下code喜欢:

The C standard makes clear that a compiler/library combination is allowed to do whatever it likes with the following code:

int doubleFree(char *p)
{
  int temp = *p;
  free(p);
  free(p);
  return temp;
}

在一个编译器并不需要使用特定的捆绑库的事件,但是,有没有在C标准的任何事从定义一个有意义的行为,禁止出库? ;作为一个简单的例子,假设code是为一个平台,有引用计数指针,这样下面的 P =的malloc(1234)写__addref(P); __addref(P); 前两个电话给免费(P)将减少计数器的值而不是释放内存。对于这样的库使用任何书面code自然会和这样的库只工作(和 __的AddRef()通话可能会失败,在大多数人),但这样的一个功能可以在很多情况下,比如当有帮助有必要反复传递一个字符串,希望给予与的strdup 产生一个字符串,从而调用方法免费就可以了。

In the event that a compiler does not require use of a particular bundled library, however, is there anything in the C standard which would forbid a library from defining a meaningful behavior? As a simple example, suppose code were written for a platform which had reference-counted pointers, such that following p = malloc(1234); __addref(p); __addref(p); the first two calls to free(p) would decrement the counter but not free the memory. Any code written for use with such a library would naturally work only with such a library (and the __addref() calls would likely fail on most others), but such a feature could be helpful in many cases when e.g. it is necessary to pass the a string repeatedly to a method which expects to be given a string produced with strdup and consequently calls free on it.

在该图书馆将定义为像一些行动的有用的行为事件双重释放的指针,有没有在C标准的任何事授权编译单方面打破它?

In the event that a library would define a useful behavior for some action like double-freeing a pointer, is there anything in the C standard which would authorize a compiler to unilaterally break it?

推荐答案

有实际上是两个问题,在这里,你的正式声明之一,您更广泛的意义在你的注释所列被别人提出的问题。

There is really two question here, your formally stated one and your broader one outlined in your comments to questions raised by others.

您正式的问题是未定义行为和部分 4 上符合定义的答案。定义陈述(重点煤矿的):

Your formal question is answers by the definition of undefined behavior and section 4 on conformance. The definition says (emphasis mine):

行为,或者在错误数据的使用的不可移植或错误的程序构造,
  对于这本国际标准的并没有规定要求

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

通过强调的不可移植的和的并没有规定要求的。这确实说明了一切,编译器是免费的在不愉快的方式来优化或者也可以选择让行为记录,并明确,这当然意味着程序不再的严格符合的,这给我们带来第 4

With emphasis on nonportable and imposes no requirements. This really says it all, the compiler is free to optimize in unpleasant manners or can also chose to make the behavior documented and well defined, this of course mean the program is no longer strictly conforming, which brings us to section 4:

一个严格符合程序,只需要使用的语言和库的这些功能
  在这个国际Standard.2规定)不应产生依赖于任何输出
  不确定的,不确定的,或实现定义的行为,并不得超过任何
  实施最低限度。

A strictly conforming program shall use only those features of the language and library specified in this International Standard.2) It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit.

但符合标准的实现是允许的扩展,只要不破符合规范的程序:

but a conforming implementation is allowed extensions as long as they don't break a conforming program:

一个符合标准的实现可以有扩展(含追加
  库函数),只要它们不改变的任何行为严格符合
  program.3)

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.3)

由于Ç常见问题解答说

有极少数的现实,实用,严格符合程序。在另一方面,仅仅是符合规范的程序可以利用它想要的任何具体的编译器扩展。

There are very few realistic, useful, strictly conforming programs. On the other hand, a merely conforming program can make use of any compiler-specific extension it wants to.

与采取更积极的优化创造了机会与未定义行为的编译器和在长期的恐惧这将使现实世界的系统编程不可能你非正式的问题涉及。虽然我明白这是如何相对较新的积极的姿态<一个href=\"http://stackoverflow.com/questions/24296571/why-does-this-loop-produce-warning-iteration-3u-invokes-undefined-behavior-an/24297811#comment37567459_24297811\">seems非常不友好的程序员很多到底编译器不会持续很长时间,如果人们不能建立与它有用的程序。一个相关的博客文章由约翰Regehr:提案的C友好方言

Your informal question deals with compilers taking more aggressive optimization opportunies with undefined behavior and in the long run the fear this will make real world systems programming impossible. While I do understand how this relatively new aggressive stance seems very programmer unfriendly to many in the end a compiler won't last very long if people can not build useful programs with it. A related blog post by John Regehr: Proposal for a Friendly Dialect of C.

有人可能认为正好相反,编译器已经做了很多努力打造的扩展,以支持不同的需求不是由标准的支持。我认为,在Linux内核中演示了这口井的文章 GCC黑客。它进入Linux内核依赖于许多GCC的扩展和已普遍在努力支持尽可能多的 GCC 扩展成为可能。

One could argue the opposite, that compilers have made a lot of effort to build extensions to support varying needs not supported by the standard. I think the article GCC hacks in the Linux kernel demonstrates this well. It goes into the many gcc extensions that the Linux kernel relies on and clang has in general attempted to support as many gcc extensions as possible.

无论编译器已删除的这阻碍有效的系统编程未定义行为有用的处理是我也不清楚。我觉得具体问题上已在系统编程被剥削,不再工作,将社会有益和有趣的未定义行为个别情况下的替代品。

Whether compilers have removed useful handling of undefined behavior which hampers effective systems programming is not clear to me. I think specific questions on alternatives for individual cases of undefined behavior that has been exploited in systems programming and no longer work would be useful and interesting to the community.

这篇关于请问C标准强制要求的平台不能定义超出标准给出的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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