在C语言中,是const的变量保证在内存不同? [英] In C, are const variables guaranteed to be distinct in memory?

查看:150
本文介绍了在C语言中,是const的变量保证在内存不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串发言时,C99的标准说(6.4.5.6):

Speaking of string literals, the C99 standard says (6.4.5.6):

这是不确定的,这些阵列是否提供了不同的元素具有适当的值。如果程序试图修改这样的阵列,其行为是不确定的。

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

我找不到任何类似的警告或常量变量的明确保证。能恩pression &放大器; X ==放大器; Y 在上下文 const int的X = 12; const int的Y = 12; 评估为 1 ?怎么样一个const变量和字符串(即是&放大器; X = =\\ 014 \\ 000 \\ 000保证是 0 即使是在32位小端平台)?

I couldn't find either a similar warning or an explicit guarantee for const variables. Can the expression &x == &y in the context const int x=12; const int y=12; evaluate to 1? What about a const variable and a string literal (i.e. is &x == "\014\000\000" guaranteed to be 0 even on a 32-bit little-endian platform)?

有关它的价值,在的这个博客帖子给出了问题的情况下。

For what it's worth, the section "String literals" in this blog post gives the context of the question.

推荐答案

在标准的,平等的§6.5.9平等运营商的讨论,&安培; 是在§6.5.3.2地址和间接运营商的讨论,而常量中§6.7.3类型的限定进行了讨论。关于指针平等有关段落是§6.5.9.6:

In the standard, equality is discussed in §6.5.9 "Equality operators", & is discussed in §6.5.3.2 "Address and indirection operators", and const is discussed in §6.7.3 "Type qualifiers". The relevant passage about pointer equality is §6.5.9.6:

两个指针比较平等的,当且仅当两个是空指针,两者都指向了
  同一对象(包括指向一个对象,并在其开始的子对象)或函数的 [或过去的指针数组的末尾]

Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, [or pointers past the end of an array]

&放的唯一定义; 是一元&安培; 运营商产生的操作数的地址。 [...]的结果是一个指针通过其操作数所指定的对象或功能。(§6.5.3.2.3)。有不幸的是单词地址中没有正式的定义;但不同的对象(由 == 规定的平等)有不同的地址,因为地址指针是由上述平等的定义是不同的。

The only definition of & is that "The unary & operator yields the address of its operand. […] The result is a pointer to the object or function designated by its operand." (§6.5.3.2.3). There is unfortunately no formal definition of the word "address"; but distinct objects (for the equality defined by ==) have distinct addresses, because the addresses are pointers that are distinct by the definition of equality above.

至于常量的意思,§6.7.3并不表明常量有任何关于轴承是什么使一个对象(它是由§3.14的数据存储在所述执行环境中的区域中,其内容可以重新present值)。脚注还指出,如果它的地址是永远不会使用的实现不需要这样一个对象分配存储空间。虽然这是不规范的,这是一个强烈的信号,如果使用的地址,然后存储必须分配的为每个对象

As for the meaning of const, §6.7.3 doesn't indicate that const has any bearing on what makes an object (which is "a region of data storage in the execution environment, the contents of which can represent values" by §3.14). A footnote further indicates that "the implementation need not allocate storage for such an object if its address is never used". Although this is non-normative, it is a strong indication that if the address is used then storage must be allocated for each object.

请注意,如果对象是 const的挥发,那么它是相当明确(明确的,因为挥发性可以永远定),他们不能有相同的地址,因为 const的挥发对象是由实现可变的。 (§6.7.3.10有使用的例子 const的挥发

Note that if the objects are const volatile, then it is fairly clear (as clear as volatile can ever be) that they can't have the same address, because const volatile objects are mutable by the implementation. (§6.7.3.10 has an example of use of const volatile.)

即使在非易失性的情况下,常量仅表示该计划的这部分是不允许修改的对象,而不是对象只读一般。要合并常量与别的对象的存储,大胆的实施者必须保证没有什么可以修改的对象。这对于带有独立编译一个实现(当然我们从标准到越来越远的不需额外-发生,在实践境内)。

Even in the non-volatile case, const only indicates that this part of the program is not allowed to modify the object, not that the object is read-only in general. To merge the storage of a const object with something else, the audacious implementer would have to guarantee that nothing can modify the object. This is fairly difficult for an object with external linkage in an implementation with separate compilation (but of course we're getting away from the standard and into the won't-happen-in-practice territory).

如果这是写一个C程序,那么你可以通过给物体不同的值增加你的机会:

If this is about writing a C program, then you can increase your chances by giving the objects different values:

const int x = __LINE__;
const int y = __LINE__;

如果这是关于C的理论模型,我会去制作的对象不同。你必须通过在一个段落这里总结的答案(加长版),您的论文来证明这个选​​择。

If this is about a theoretical model of C, I'd go for making the objects distinct. You'll have to justify this choice by summarizing the answers here in a paragraph in (the extended version of) your paper.

在另一方面,如果这是有关编写一个优化编译器,我怀疑这会伤害许多现实世界的方案合并常量。我会去一个嵌入式的编译器,在那里用户可以用来玩它的安全与边缘情况下,而且保存的合并可能是不可忽略的内存。我会去反对托管平台合并,其中的任何收益可忽略不计。

On the other hand, if this is about writing an optimizing compiler, I doubt it would hurt many real-world programs to merge constants. I'd go for merging in an embedded compiler, where users are used to playing it safe with edge cases and where the memory saved could be non-negligible. I'd go against merging in a hosted platform where any gain would be negligible.

(从 N1256 又名C99 + TC3。我不参考'T认为版本有差别。)

(References from N1256 a.k.a. C99+TC3. I don't think the version makes a difference.)

这篇关于在C语言中,是const的变量保证在内存不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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