例如: Sizeof (char*)返回4。还有int* long long*,我试过的所有方法。有什么例外吗?
当前回答
除了16位/32位/64位的差异之外,还会发生更奇怪的事情。
曾经有一些机器sizeof(int *)是一个值,可能是4,但sizeof(char *)更大。自然地处理单词而不是字节的机器必须“增加”字符指针,以指定您真正想要的单词的哪一部分,以便正确地实现C/ c++标准。
现在这是非常不寻常的,因为硬件设计师已经了解了字节可寻址性的价值。
其他回答
据我所知,这是基于内存地址的大小。所以在一个32位地址方案的系统上,sizeof将返回4,因为那是4个字节。
即使是在普通的x86 32位平台上,你也可以得到不同大小的指针,试试这个例子:
struct A {};
struct B : virtual public A {};
struct C {};
struct D : public A, public C {};
int main()
{
cout << "A:" << sizeof(void (A::*)()) << endl;
cout << "B:" << sizeof(void (B::*)()) << endl;
cout << "D:" << sizeof(void (D::*)()) << endl;
}
在Visual c++ 2008中,指向成员函数的指针的大小分别为4、12和8。
Raymond Chen在这里讲过。
出于完整性和历史兴趣,在64位世界中,对于长类型和长类型的大小有不同的平台约定,分别命名为LLP64和LP64,主要在unix类型系统和Windows之间。一个名为ILP64的旧标准也使int = 64位宽。
微软保留了LLP64,其中longlong = 64位宽,但long保留为32位宽,以便于移植。
Type ILP64 LP64 LLP64
char 8 8 8
short 16 16 16
int 64 32 32
long 64 64 32
long long 64 64 64
pointer 64 64 64
来源:https://stackoverflow.com/a/384672/48026
The size of the pointer basically depends on the architecture of the system in which it is implemented. For example the size of a pointer in 32 bit is 4 bytes (32 bit ) and 8 bytes(64 bit ) in a 64 bit machines. The bit types in a machine are nothing but memory address, that it can have. 32 bit machines can have 2^32 address space and 64 bit machines can have upto 2^64 address spaces. So a pointer (variable which points to a memory location) should be able to point to any of the memory address (2^32 for 32 bit and 2^64 for 64 bit) that a machines holds.
由于这个原因,我们看到指针的大小在32位机器中是4字节,在64位机器中是8字节。
一般来说,当你在不同的平台上编译时,sizeof(几乎任何东西)都会改变。在32位平台上,指针的大小总是相同的。在其他平台上(64位是最明显的例子),这种情况可能会发生变化。
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- C“int”的大小是2字节还是4字节?
- 多维数组如何在内存中格式化?
- printf()和puts()在C语言中的区别是什么?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 有效,但毫无价值的语法在开关情况下?