例如: Sizeof (char*)返回4。还有int* long long*,我试过的所有方法。有什么例外吗?
当前回答
8位和16位指针用于大多数低规格微控制器。这意味着每一台洗衣机、微型电脑、冰箱、老式电视甚至汽车。
你可以说这些与现实世界的编程毫无关系。 但这里有一个真实的例子: Arduino带有1-2-4k ram(取决于芯片),带有2字节指针。
它是最新的,便宜的,每个人都可以使用,值得为之编程。
其他回答
从技术上讲,C标准只保证sizeof(char) == 1,其余的取决于实现。但在现代x86架构(例如Intel/AMD芯片)上,这是相当可预测的。
You've probably heard processors described as being 16-bit, 32-bit, 64-bit, etc. This usually means that the processor uses N-bits for integers. Since pointers store memory addresses, and memory addresses are integers, this effectively tells you how many bits are going to be used for pointers. sizeof is usually measured in bytes, so code compiled for 32-bit processors will report the size of pointers to be 4 (32 bits / 8 bits per byte), and code for 64-bit processors will report the size of pointers to be 8 (64 bits / 8 bits per byte). This is where the limitation of 4GB of RAM for 32-bit processors comes from -- if each memory address corresponds to a byte, to address more memory you need integers larger than 32-bits.
指针只是一个地址的容器。在32位计算机上,您的地址范围是32位,因此指针总是4字节。在64位机器上,如果你的地址范围是64位,一个指针将是8字节。
指针大小为4字节的原因是因为您正在为32位体系结构编译。正如FryGuy所指出的,在64位架构上你将看到8。
如果您正在为64位机器编译,那么它可能是8。
在windows 32位机器上的Turbo C编译器中,指针和int的大小为2字节。
所以指针的大小是编译器特定的。但一般大多数编译器实现为支持32位4字节指针变量和64位8字节指针变量)。
所以指针的大小在所有机器上都是不一样的。
推荐文章
- 什么是“参数依赖查找”(又名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的想法?
- 有效,但毫无价值的语法在开关情况下?