能否从指向子对象的指针中获得指向完整对象表示元素的指针? [英] Can one get a pointer to a complete object representation element from a pointer to a suboject?

查看:100
本文介绍了能否从指向子对象的指针中获得指向完整对象表示元素的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下代码:

int i;
int is[10]{};

unsigned char * p = reinterpret_cast<unsigned char*>(&i);
//p defined to point to the object-representation of the first element of array ints
unsigned char * ps = reinterpret_cast<unsigned char*>(&is[0]);

p += sizeof(int);
ps += sizeof(int);
//now ps points to the end of ints[0] and p point to the end of i;

p += sizeof(int); //Undefined behavior according to [expr.add]

ps += sizeof(int); //Undefined behavior?
unsigned char c = *ps;//Undefined behavior?

如果我们认为ps指向对象表示is[0]中的>,然后根据指针算术规则,该行为在最后两个代码行中未定义.

If we consider that ps points to the object-representation of is[0] then according to pointer arithmetic rule, the behavior is undefined at the two last code line.

尽管如此,如果ps还是int is数组的 object-representation 的指针,则行为已定义.

Nevertheless, if ps is also a pointer to the object-representation of the array of int is the behavior is defined.

所以我的问题是:是否指向object-representation 的指针="nofollow noreferrer">子对象还是指向包含完整对象的 object-representation 元素的指针吗?

So my question is: does a pointer to the object-representation of a suboject is also a pointer to an element of the object-representation of the containing complete object?

推荐答案

一般情况

是的,指向子对象的对象表示的指针也是指向包含完整对象的对象表示的一部分的指针.

Yes, the pointer to the object-representation of a suboject is also a pointer to a part of the object-representation of the containing complete object.

这可以从以下推论得出:

This can be deduced from:

1.8/1::对象是存储区域.

1.8/1: An object is a region of storage.

3.9/4:类型为T的对象的对象表示形式是由类型T的对象占用的N个无符号字符对象的序列,其中N等于sizeof(T).

3.9/4: The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).

5.3.3/1::sizeof运算符产生其操作数的对象表示形式中的字节数.

5.3.3/1: The sizeof operator yields the number of bytes in the object representation of its operand.

1.8/2::对象可以包含其他对象,称为子对象.子对象可以是成员子对象,基类子对象, 或数组元素.不是以下对象的子对象的对象 其他任何对象都称为完整对象.

1.8/2: Objects can contain other objects, called subobjects. A subobject can be a member subobject, a base class subobject, or an array element. An object that is not a subobject of any other object is called a complete object.

对象是存储区域并且子对象包含在存储区域中这一事实意味着子对象表示包含在对象表示中.

The fact that an object is a region of storage and that the subobject is contained within the region of storage means that the suboject representation is contained in the object representation.

其他答案中所述,指向对象表示的指针实际上就是指向对象的指针.

As explained in this other answer, the pointer to the object representation is in fact the pointer to the object.

阵列的特殊情况

是的,指向子对象的对象表示形式的指针(即,在这种情况下为数组元素)是对包含完整对象的对象表示形式的元素的指针(即,在此大小写数组).

Yes, the pointer to the object representation of a subobject (i.e. in this case an array element) is a pointer to an element of the object-representation of the containing complete object (i.e. in this case the array).

8.4.3/1:(...)数组类型的对象包含一个连续分配的非空的N个类型为T的子对象的集合. ..)

8.4.3/1: (...) An object of array type contains a contiguously allocated non-empty set of N subobjects of type T. (...)

因此,根据定义:

  • 数组的元素是连续分配的,这意味着元素之间没有空白.

  • the elements of the array are contiguously allocated, meaning that there is no empty space between the elements.

每个元素的对象表示形式是sizeof(T)个无符号字符的序列,而完整数组的对象表示形式是所得的N * sizeof(T)个无符号字符.

the object representation of each element is a sequence of sizeof(T) unsigned chars, and the object representation of the complete array is the resulting N*sizeof(T) unsigned chars.

然后我们可以推断:

  • 其他答案中所述,指向对象表示的指针实际上是指向对象表示的指针.对象.

  • As explained in this other answer, the pointer to the object representation is in fact the pointer to the object.

数组中第k个元素的地址对应于数组对象表示中的第k个元素,并且通过构造以k * sizeof(T)的偏移量构成数组的第k个元素的无符号字符.数组表示形式的地址.

the address of the k-th element in the array corresponds to the k-th element in the object representation of the array, and is by construction at an offset of k*sizeof(T) unsigned chars of the address of the array representation.

因此,只要您保持在is

这篇关于能否从指向子对象的指针中获得指向完整对象表示元素的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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