这意味着sizeof(int*)、sizeof(char*)和sizeof(void*)在 32 位系统上都将返回 4,在 64 位系统上都将返回 8。 需要注意的是,sizeof对于指针类型返回的是指针本身的大小,而不是指针所指向的数据类型的大小。如果你需要计算指针所指向的数据类型的大小,可以使用sizeof(*pointer)。例如,如果你有一个int*类型...
意思是,在 GNU C中,指向 void 指针和指向函数的指针支持加法和减法操作。这是通过将 void 或函数的大小视为1来实现的。 因此,sizeof 操作符也可以用于 void 和函数类型,并返回 1。选项 -Wpointer-arith 会在使用这些扩展时发出警告。
A consequence of this is that sizeof is also allowed on void and on function types, and returns 1. The option -Wpointer-arith requests a warning if these extensions are used. 但是在其他编译器下结果怎么样,没有试过。 所以总结一下,sizeof(void)没什么实际意义。 但是void * 类型就非常的实用。
#include <iostream> int main() { std::cout << "Size of pointer: " << sizeof(void*) << " byte(s)" << std::endl; return 0; } 复制代码 在这个示例中,sizeof(void*) 计算的是 void* 类型指针的大小,void* 类型指针可以指向任何数据类型的对象。在不同的平台上运行这个程序,可能会输出不...
因此,从标准的角度看,试图对void进行大小计算是无效的。然而,GCC实际上允许这种操作,并将结果视作1字节。根据相关文献,GCC允许对void指针实施加减操作,这种操作会将void类型视为1字节大小,以使得算术运算有效,sizeof(void)返回1字节大小,而使用-Wpointer-arith选项将会警告开发者。
需要注意的是,sizeof对于指针类型返回的是指针本身的大小,而不是指针所指向的数据类型的大小。如果你需要计算指针所指向的数据类型的大小,可以使用sizeof(*pointer)。例如,如果你有一个int*类型的指针,那么sizeof(*pointer)将返回sizeof(int)。 总之,sizeof与指针类型的关联主要体现在指针类型的大小是固定的,而与...
voidfunc(inta[5]){printf("func: sizeof(a)=%d\n",sizeof(a));}intmain(intargc,char*argv[...
很多答案说取决于cpu 是不准确的:AMD64cpu 是64位的,64位虚拟地址. 可以在上面运行64位 Windowst ...
SIZEOF(void(*)(void));//function pointerSIZEOF(enum{E}); SIZEOF(char[0]);//zero-size arraySIZEOF(int[3]); SIZEOF(struct{});//empty structSIZEOF(struct{charc;}); SIZEOF(struct{charc;shorts;});//alignmentSIZEOF(struct{inti;charc;}); ...
void_size.c:15: error: invalid application of ‘sizeof’ to a void type void_size.c:16: error: ISO C++ forbids applying ‘sizeof’ to an expression of function type References 1.http://gcc.gnu.org/onlinedocs/gcc-4.4.6/gcc/Pointer-Arith.html...