是否假设reinterpret_cast< char *>(myTypePtr)指向数组? [英] Is reinterpret_cast<char*>(myTypePtr) assumed to point to an array?

查看:195
本文介绍了是否假设reinterpret_cast< char *>(myTypePtr)指向数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道char*可以为任何别名:根据 cppreference

We know that char* can alias anything: According to cppreference

每当尝试通过AliasedType类型的glvalue读取或修改DynamicType类型的对象的存储值时,除非满足以下条件之一,否则行为是不确定的: [...] AliasedType为std::bytecharunsigned char:这允许检查任何对象的对象表示形式(字节数组). [...]

Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedType, the behavior is undefined unless one of the following is true: [...] AliasedType is std::byte, char, or unsigned char: this permits examination of the object representation of any object as an array of bytes. [...]

n4659草案[6.10,(8.8)]中不存在黑体字. 由于未对不指向同一数组元素的指针进行指针算术运算是未定义的,我们真的可以仅使用reinterpret_cast访问除第一个字节以外的字节吗? 也许std::memcpy必须用于此目的?

The statement in boldface is not present in n4659 draft [6.10, (8.8)]. Since doing pointer arithmetic on pointers that don't point to elements of the same array is undefined, can we really access bytes other than the first one using only reinterpret_cast? Or maybe std::memcpy must be used for that purpose?

推荐答案

auto ptr = reinterpret_cast<char*>(myTypePtr);

该标准允许进行此转换,原因是:

The standard permit this conversion, due to:

可以将对象指针显式转换为其他类型的对象指针.73将对象指针类型的prvalue v转换为对象指针类型"pointer to cv T"时,结果为static_cast< cv T * >(static_cast< cv void *>(v)). [注:将类型为指向T1的指针"的prvalue转换为指向T2的指针"的类型(其中T1和T2是对象类型,并且T2的对齐要求不严格于T1的对齐要求),并返回其原始类型产生原始指针值. —尾注]

An object pointer can be explicitly converted to an object pointer of a different type.73 When a prvalue v of object pointer type is converted to the object pointer type "pointer to cv T", the result is static_­cast<cv T*>(static_­cast<cv void*>(v)). [ Note: Converting a prvalue of type "pointer to T1" to the type "pointer to T2" (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value.  — end note ]

因此,转换等效于:

假设myTypePtr没有任何简历限定符.

assume myTypePtr has no any cv qualifier.

auto ptr = static_­cast<char*>(static_­cast<void*>(myTypePtr))

由于以下原因,您被允许取消引用myTypePtr以访问对象(指针指向)中的值:

And you are permitted to dereference myTypePtr to access the value within the object(the pointer point to), due to:

如果程序尝试通过以下类型之一以外的glvalue访问对象的存储值,则行为未定义:

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

  • char,unsigned char或std :: byte类型.

如果myTypePtr不是char类型数组的对象,只要您对ptr应用加法,由于以下原因,它将导致未定义的行为:

If myTypePtr is not an object of array of char type, as long as you applied addition to ptr, It will result in undefined behavior, due to:

当将具有整数类型的表达式添加到指针或从指针中减去时,结果将具有指针操作数的类型.如果表达式P指向具有n个元素的数组对象x的元素x [i] 86,则表达式P + J和J + P(其中J的值为j)指向(可能是假设的)元素 如果0≤i +j≤n,则x [j + p];否则,行为是不确定的.同样,如果0≤i-j≤n,则表达式P-J指向(可能是假设的)元素x [i-j];否则,行为是不确定的.

When an expression that has integral type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the expression P points to element x[i] of an array object x with n elements,86 the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) element x[j + p] if 0 ≤ i+j≤n ; otherwise, the behavior is undefined. Likewise, the expression P - J points to the (possibly-hypothetical) element x[i - j] if 0 ≤ i - j≤n ; otherwise, the behavior is undefined.

对于加法或减法,如果表达式P或Q的类型为指向cv T的指针",而T和数组元素类型不相似,则行为是不确定的.

For addition or subtraction, if the expressions P or Q have type "pointer to cv T", where T and the array element type are not similar, the behavior is undefined.

因为myTypePtr的元素不是char类型.因此,对ptr应用加法会导致不确定的行为.

Because the element of myTypePtr is not of type char. Hence applying addition to ptr result in undefined behavior.

或者必须为此目的使用std :: memcpy?

Or maybe std::memcpy must be used for that purpose?

是,如果myTypePtr指向的对象符合以下规则:

Yes, If the object to which myTypePtr point subject to the following rules:

对于平凡可复制的类型T的任何对象(基类子对象除外),无论该对象是否持有类型T的有效值,组成该对象的基础字节([intro.memory])都可以是复制到char,unsigned char或std :: byte([cstddef.syn])数组中.43如果将该数组的内容复制回对象,则该对象随后应保留其原始值.

OR

对于任何平凡可复制的类型T,如果指向T的两个指针指向不同的T对象obj1和obj2,则obj1和obj2都不是基类子对象,如果构成obj1的基础字节([intro.memory])复制到obj2,44中obj2随后应保持与obj1相同的值.

For any trivially copyable type T, if two pointers to T point to distinct T objects obj1 and obj2, where neither obj1 nor obj2 is a base-class subobject, if the underlying bytes ([intro.memory]) making up obj1 are copied into obj2,44 obj2 shall subsequently hold the same value as obj1.

但是,很遗憾,我们不能在当前标准下实施这样的memcpy.

However, It's unfortunately we can't implement such a memcpy subject to the current standard.

这篇关于是否假设reinterpret_cast&lt; char *&gt;(myTypePtr)指向数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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