ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – static_cast’d指针值

2019-10-07 21:07:41  阅读:338  来源: 互联网

标签:static-cast c language-lawyer void-pointers c17


在当前的标准草案(和C 17)中,this是关于static_casting a void *的:

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.

我想知道,转换是指针可互换的有什么区别?是否有一种情况,当将一个void *转换为某个指针时,interconvertible实际上会改变指针值?这种区别的意图是什么?

完整性pointer interconvertible

Two objects a and b are pointer-interconvertible if:

  • (4.1) they are the same object, or
  • (4.2) one is a union object and the other is a non-static data member of that object ([class.union]), or
  • (4.3) one is a standard-layout class object and the other is the first non-static data member of that object, or, if the object has no
    non-static data members, any base class subobject of that object
    ([class.mem]), or
  • (4.4) there exists an object c such that a and c are pointer-interconvertible, and c and b are pointer-interconvertible.

If two objects are pointer-interconvertible, then they have the same
address
, and it is possible to obtain a pointer to one from a pointer
to the other via a reinterpret_­cast.

解决方法:

您可能会误解“指针值”这个术语.该术语定义于[basic.compound]/3

Every value of pointer type is one of the following:

  • a pointer to an object or function (the pointer is said to point to the object or function), or

  • a pointer past the end of an object ([expr.add]), or

  • the null pointer value ([conv.ptr]) for that type, or

  • an invalid pointer value.

A value of a pointer type that is a pointer to or past the end of an object represents the address of the first byte in memory ([intro.memory]) occupied by the object or the first byte in memory after the end of the storage occupied by the object, respectively.

所以你可以看到标准中的术语“指针值”是一个非常抽象的术语.即使两个指针值表示相同的地址,它们也可能具有不同的值. cppreference中的示例很好地演示了“指针值”的概念:

struct S1 { int a; } s1;
struct S2 { int a; private: int b; } s2; // not standard-layout
union U { int a; double b; } u = {0};
int arr[2];

int* p1 = reinterpret_cast<int*>(&s1); // value of p1 is "pointer to s1.a" because s1.a
                                       // and s1 are pointer-interconvertible

int* p2 = reinterpret_cast<int*>(&s2); // value of p2 is unchanged by reinterpret_cast and
                                       // is "pointer to s2". 

int* p3 = reinterpret_cast<int*>(&u);  // value of p3 is "pointer to u.a": u.a and u are
                                       // pointer-interconvertible

double* p4 = reinterpret_cast<double*>(p3); // value of p4 is "pointer to u.b": u.a and u.b
                                            // are pointer-interconvertible because both
                                            // are pointer-interconvertible with u

int* p5 = reinterpret_cast<int*>(&arr); // value of p5 is unchanged by reinterpret_cast and
                                        // is "pointer to arr"

标签:static-cast,c,language-lawyer,void-pointers,c17
来源: https://codeday.me/bug/20191007/1868873.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有