例如: Sizeof (char*)返回4。还有int* long long*,我试过的所有方法。有什么例外吗?
当前回答
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字节。
其他回答
指针大小为4字节的原因是因为您正在为32位体系结构编译。正如FryGuy所指出的,在64位架构上你将看到8。
在windows 32位机器上的Turbo C编译器中,指针和int的大小为2字节。
所以指针的大小是编译器特定的。但一般大多数编译器实现为支持32位4字节指针变量和64位8字节指针变量)。
所以指针的大小在所有机器上都是不一样的。
即使是在普通的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在这里讲过。
8位和16位指针用于大多数低规格微控制器。这意味着每一台洗衣机、微型电脑、冰箱、老式电视甚至汽车。
你可以说这些与现实世界的编程毫无关系。 但这里有一个真实的例子: Arduino带有1-2-4k ram(取决于芯片),带有2字节指针。
它是最新的,便宜的,每个人都可以使用,值得为之编程。
只是已经发布的列表中的另一个例外。在32位平台上,指针可以占用6字节,而不是4字节:
#include <stdio.h>
#include <stdlib.h>
int main() {
char far* ptr; // note that this is a far pointer
printf( "%d\n", sizeof( ptr));
return EXIT_SUCCESS;
}
如果你用Open Watcom编译这个程序并运行它,你会得到6,因为它支持的远指针由32位偏移量和16位段值组成
推荐文章
- 什么是“参数依赖查找”(又名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的想法?
- 有效,但毫无价值的语法在开关情况下?