具有静态存储时间和平等,恒初始化常量对象可以被合并? [英] Can constant objects with static storage duration and equal, constant initializers be coalesced?

查看:229
本文介绍了具有静态存储时间和平等,恒初始化常量对象可以被合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑使用静态存储持续时间和平等的,恒定的初始化两个对象:

静态const int的一个= 50;
静态const int的B = 50;

有效期是一个编译器等,这些是结合&放大器;一个==&安培; B

(对于情况下,我在考虑使用静态常量对象获得唯一的地址为定点指针值使用,如果是合法的编译器这样的对象结合,我用了两个这样的对象相同的恒定值,则地址可能是平等的,我不能把它们作为定点值。)


解决方案

该指针必须比较不等于。见C99 6.5.9条第6款:


  

两个指针比较相等当且仅当两者都为空指针,​​两者都指向同一对象(包括一个指针指向一个物体,并在其开头的子对象)或功能,二者是指向一个过去的最后一个元素相同的数组对象,或者一个是指向一点一数组对象的端部,而另一个是一个指向碰巧紧跟在地址空间中的第一阵列对象的不同阵列对象的开始。

Consider two objects with static storage duration and equal, constant initializers:

static const int a = 50;
static const int b = 50;

Is it valid for a compiler to combine these such that &a == &b?

(For context, I was thinking of using static constant objects to get unique addresses to use as sentinel pointer values. If it is legal for a compiler to combine such objects and I use the same constant value for two such objects, then the addresses could be equal and I cannot use them as sentinel values.)

解决方案

The pointers must compare not-equal. See C99 6.5.9 paragraph 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, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

这篇关于具有静态存储时间和平等,恒初始化常量对象可以被合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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