当作废具有未知大小的指针运算 [英] Pointer arithmetic when void has unknown size
问题描述
在Visual Studio的C ++版本9(也可能是其它版本也行),以下code:
In Visual Studio C++ version 9 (and probably other versions too), the following code:
int a = sizeof(void);
void const *b = static_cast<void const *>("hello world");
b += 6;
error C2070: 'void': illegal sizeof operand
error C2036: 'const void *' : unknown size
这code下GCC的作品,对待的 的sizeof(无效)
为 1
。
This code works under GCC, which treats sizeof(void)
as 1
.
有没有解决这个限制某种方式,如明确铸造到的char *
为指针运算的目的,增加了混乱(无效*
是公认并用作无类型指针原始存储器)。
Is there some way around this limitation, as casting explicitly to char *
for purposes of pointer arithmetic adds to the confusion (void *
is well recognised and used as a typeless pointer to raw memory).
- 请注意,我很清楚的
该标准的存在。 - 我想做原始指针
算术。 - 我把
的sizeof(无效)
来
表明我很清楚的事实
它不是1
是
这个问题的原因。 - 的code
例子很简单,就是演示一下
需要产生的错误。 - 我知道这是不是一个正常的方式来使用
无效
,但是这是C,而这些事情发生。 - 是,人们需要在低级别的做到这一点。我不为什么,我以后怎么敢之后。如果你想知道为什么,看看一些内核源代码,或者你的友好的glibc。
- Please note, I'm well aware of the existence of the standard.
- I want to do raw pointer arithmetic.
- I take
sizeof(void)
to show that I'm well aware the fact that it isn't1
is the cause of the problem. - The code example is simply to demonstrate what is required to generate the errors.
- I know this isn't a "normal" way to use
void
, but this is C, and these things happen. - Yes, people need to do this at low-level. I'm not after why, I'm after how. If you want to know why, take a look at some kernel source, or your friendly glibc.
看来这个问题已经产生混乱的一个很大。现在的问题是不是的为什么的有的sizeof(无效)== 1
不是标准,而是做什么时,事实并非如此。
It seems this question has generated a great deal of confusion. The question is not about why having sizeof(void) == 1
is not standard, but what to do when it isn't.
在实例单字节指针运算都是做的,事实证明,铸造的char *
是正确的答案,不是因为 *(无效*)
没有大小,而是因为标准实际上确保了 *(字符*)
总是 1
。因此,使用的char *
的始终是正确的,并一致通过无效*
与海湾合作委员会延长的原用途指针运算。
In the instance that single-byte pointer arithmetic is to be done, it turns out that casting to char *
is the correct answer, not because *(void *)
has no size, but because the standard actually guarantees that *(char *)
is always 1
. Therefore the use of char *
is always correct, and congruent with void *
with the GCC extension for the purposes of raw pointer arithmetic.
要进一步加强这一点,无效*
是正确的选择指针为无类型的内存,和的char *
是正确的类型强制转换为作为原料指针运算
To further reinforce the point, void *
is the correct choice for pointers to typeless memory, and char *
is the correct type to cast to for raw pointer arithmetic.
推荐答案
这将出现在<一个href=\"http://stackoverflow.com/questions/1864956/byte-$p$pcision-pointer-arithmetic-in-c-when-sizeofchar-1\">correct回答是使用的char *
为指针运算,因为的sizeof(char)的
总是被定义为1,并且是在任何平台上最优秀的寻址粒度
It would appear the correct answer is to use char *
for pointer arithmetic, because sizeof(char)
is always defined to be 1, and to be of the finest addressable granularity on any platform.
因此,在短期,周围有限制没办法,的char *
其实是在做正确的方式。
So in short, there is no way around the limitation, char *
is in fact the proper way to do it.
马蒂亚斯·万德尔有<一个href=\"http://stackoverflow.com/questions/1864352/void-has-unknown-size-in-visual-c/1864369#1864369\">right回答但有不同的理由。
这篇关于当作废具有未知大小的指针运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!