在原语类型指针之间进行转换 [英] Casting between primitive type pointers

查看:127
本文介绍了在原语类型指针之间进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是以下定义的:

char* charPtr = new char[42];
int* intPtr = (int*)charPtr;

charPtr++;
intPtr = (int*) charPtr;

intPtr 未正确对齐在两种情况中的至少一种情况下)。它是非法只是有它吗?是UB在任何阶段使用它吗?

The intPtr isn't properly aligned (in at least one of the two cases). Is it illegal just having it there? Is it UB using it at any stage? How can you use it and how can't you?

推荐答案

首先,当然:指针保证对齐
第一种情况(通过§5.3.4/ 10和§3.7.4.1/ 2),并且在两种情况下可以正确地对齐
。 (显然,如果 sizeof(int)== 1 ,但
即使不是这种情况,实现不会
必须对齐需要。)

First, of course: the pointer is guaranteed to be aligned in the first case (by §5.3.4/10 and §3.7.4.1/2), and may be correctly aligned in both cases. (Obviously, if sizeof(int) == 1, but even when this is not the case, an implementation doesn't necessarily have alignment requirements.)

为了清楚起见,你的演员都是 reinterpret_cast

And to make things clear: your casts are all reinterpret_cast.

除此之外,这是一个有趣的问题,因为对于
我可以告诉,两个演员没有区别,就标准而言,
。转换的结果是
未指定(根据§5.2.10/ 7);你甚至不能保证
将它转换回 char * 将导致
原始值。 (它显然不会,例如,在机器
其中 int * 小于 char * 。)

Beyond that, this is an interesting question, because as far as I can tell, there is no difference in the two casts, as far as the standard is concerned. The results of the conversion are unspecified (according to §5.2.10/7); you're not even guaranteed that converting it back into a char* will result in the original value. (It obviously won't, for example, on machines where int* is smaller than a char*.)

实际上,标准要求返回
的值 new char [N] 对于任何可能适合它的值
都是足够的,所以你可以保证能够做到:

In practice, of course: the standard requires that the return value of new char[N] be sufficiently aligned for any value which may fit into it, so you are guaranteed to be able to do:

intPtr = new (charPtr) int;

这与你的转换有完全相同的效果,假设
默认构造函数 int 是无操作。 (假设
sizeof(int)<= 42 。)因此很难想象一个实现
,其中第一部分失败。你应该能像任何其他合法获得的 intPtr 一样使用
intPtr 。而
的想法,将它转换回一个 char * 会以某种方式导致
与原来的 char *

Which has exactly the same effect as your cast, given that the default constructor for int is a no-op. (And assuming that sizeof(int) <= 42.) So it's hard to imagine an implementation in which the first part fails. You should be able to use the intPtr just like any other legally obtained intPtr. And the idea that converting it back to a char* would somehow result in a different value from the original char* seems preposterous.

在第二部分,所有投注都关闭:你绝对不能
解除引用指针(除非你的实现保证
否则),并且很可能将它转换回
char * 导致不同的东西。 (例如,假设一个字
寻址的机器,其中将 char * 转换为
int * 向上舍入,然后转换回结果
a char * 这是 sizeof(int)
,其中尝试转换未对齐指针总是在空指针中产生
。)

In the second part, all bets are off: you definitely can't dereference the pointer (unless your implementation guarantees otherwise), and it's also quite possible that converting it back to char* results in something different. (Imagine a word addressed machine, for example, where converting a char* to an int* rounds up. Then converting back would result in a char* which was sizeof(int) higher than the original. Or where an attempt to convert a misaligned pointer always resulted in a null pointer.)

这篇关于在原语类型指针之间进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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