我在看C++中的sizeof运算符。我遇到了意外的语义错误。我读过无效数据类型没有内存大小。但是,在我的程序中,sizeof运算符对于无效数据类型返回1。这怎么可能?
#include<iostream>
int main()
{
std::cout<<"Void: "<<sizeof(void)<<" bytes\n";
return 0;
}
我正在使用CodeBlocks编写代码,并且我的操作系统是Windows 10 x64。
最佳答案
这是一个GCC extension:
In GNU C, addition and subtraction operations are supported on pointers to
void
and on pointers to functions. This is done by treating the size of avoid
or of a function as 1.A consequence of this is that
sizeof
is also allowed onvoid
and on function types, and returns 1.The option
-Wpointer-arith
requests a warning if these extensions are used.
关于c++ - sizeof运算符为VOID运算符返回1个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61832014/