C++ sizeof(some pointer) 总是等于四吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/399003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:08:25  来源:igfitidea点击:

Is the sizeof(some pointer) always equal to four?

c++cpointersmemorysizeof

提问by Joel

For example: sizeof(char*)returns 4. As does int*, long long*, everything that I've tried. Are there any exceptions to this?

例如: sizeof(char*)returns 4. 和int*, 一样long long*,我尝试过的一切。这有什么例外吗?

采纳答案by David Thornley

The guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *).

你得到的保证是sizeof(char) == 1。没有其他保证,包括不保证sizeof(int *) == sizeof(double *)

In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there's nothing to be gained in relying on a given size.

实际上,指针在 16 位系统上的大小为 2(如果你能找到的话),在 32 位系统上为 4,在 64 位系统上为 8,但是依赖于给定的尺寸。

回答by Eclipse

Even on a plain x86 32 bit platform, you can get a variety of pointer sizes, try this out for an example:

即使在普通的 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;
}

Under Visual C++ 2008, I get 4, 12 and 8 for the sizes of the pointers-to-member-function.

在 Visual C++ 2008 下,成员函数指针的大小分别为 4、12 和 8。

Raymond Chen talked about this here.

陈峰在这里讲了这个。

回答by dmityugov

Just another exception to the already posted list. On 32-bit platforms, pointers can take 6, not 4, bytes:

只是已发布列表的另一个例外。在 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;
}

If you compile this program with Open Watcom and run it, you'll get 6, because far pointers that it supports consist of 32-bit offset and 16-bit segment values

如果你用 Open Watcom 编译这个程序并运行它,你会得到 6,因为它支持的远指针由 32 位偏移和 16 位段值组成

回答by FryGuy

if you are compiling for a 64-bit machine, then it may be 8.

如果你是为 64 位机器编译,那么它可能是 8。

回答by Joseph Garvin

Technically speaking, the C standard only guarantees that sizeof(char) == 1, and the rest is up to the implementation. But on modern x86 architectures (e.g. Intel/AMD chips) it's fairly predictable.

从技术上讲,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.

您可能听说过处理器被描述为 16 位、32 位、64 位等。这通常意味着处理器使用 N 位作为整数。由于指针存储内存地址,而内存地址是整数,这有效地告诉您将有多少位用于指针。sizeof 通常以字节为单位,因此为 32 位处理器编译的代码将报告指针大小为 4(每字节 32 位 / 8 位),64 位处理器的代码将报告指针大小为 8 (每字节 64 位/8 位)。这就是 32 位处理器的 4GB RAM 限制的来源——如果每个内存地址对应一个字节,要寻址更多内存,您需要大于 32 位的整数。

回答by Rndp13

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^32address space and 64 bit machines can have upto 2^64address 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 位指针的大小在 64 位机器中为 4 字节(32 位)和 8 字节(64 位)。机器中的位类型只不过是它可以拥有的内存地址。32 位机器可以有2^32地址空间,64 位机器最多可以有2^64地址空间。因此,指针(指向内存位置的变量)应该能够指向2^32 for 32 bit and 2^64 for 64 bit机器拥有的任何内存地址 ( )。

Because of this reason we see the size of a pointer to be 4 bytes in 32 bit machine and 8 bytes in a 64 bit machine.

由于这个原因,我们看到指针的大小在 32 位机器中为 4 个字节,在 64 位机器中为 8 个字节。

回答by Darron

In addition to the 16/32/64 bit differences even odder things can occur.

除了 16/32/64 位差异之外,甚至可能发生更奇怪的事情。

There have been machines where sizeof(int *) will be one value, probably 4 but where sizeof(char *) is larger. Machines that naturally address words instead of bytes have to "augment" character pointers to specify what portion of the word you really want in order to properly implement the C/C++ standard.

有些机器的 sizeof(int *) 将是一个值,可能是 4,但 sizeof(char *) 更大。自然寻址字而不是字节的机器必须“增加”字符指针以指定您真正想要的字的哪个部分,以便正确实现 C/C++ 标准。

This is now very unusual as hardware designers have learned the value of byte addressability.

现在这是非常不寻常的,因为硬件设计人员已经了解了字节可寻址性的价值。

回答by Kobor42

8 bit and 16 bit pointers are used in most low profile microcontrollers. That means every washing machine, micro, fridge, older TVs, and even cars.

大多数薄型微控制器使用 8 位和 16 位指针。这意味着每台洗衣机、微型机、冰箱、旧电视,甚至汽车。

You could say these have nothing to do with real world programming. But here is one real world example: Arduino with 1-2-4k ram (depending on chip) with 2 byte pointers.

你可以说这些与现实世界的编程无关。但这是一个真实世界的例子:Arduino 带有 1-2-4k ram(取决于芯片)和 2 字节指针。

It's recent, cheap, accessible for everyone and worths coding for.

它是最新的,便宜的,每个人都可以使用,值得编码。

回答by Steve Jessop

In addition to what people have said about 64-bit (or whatever) systems, there are other kinds of pointer than pointer-to-object.

除了人们所说的关于 64 位(或其他)系统的内容之外,还有其他类型的指针而不是指向对象的指针。

A pointer-to-member might be almost any size, depending how they're implemented by your compiler: they aren't necessarily even all the same size. Try a pointer-to-member of a POD class, and then a pointer-to-member inherited from one of the base classes of a class with multiple bases. What fun.

指向成员的指针几乎可以是任意大小,这取决于编译器如何实现它们:它们甚至不一定都是相同的大小。尝试指向 POD 类的成员指针,然后尝试从具有多个基类的基类之一继承的指向成员的指针。多么有趣。

回答by finalsemester.co.in

Size of pointer and int is 2 bytes in Turbo C compiler on windows 32 bit machine.

在 Windows 32 位机器上的 Turbo C 编译器中,指针和 int 的大小为 2 个字节。

So size of pointer is compiler specific. But generally most of the compilers are implemented to support 4 byte pointer variable in 32 bit and 8 byte pointer variable in 64 bit machine).

所以指针的大小是特定于编译器的。但通常大多数编译器都实现为支持 32 位机器中的 4 字节指针变量和 64 位机器中的 8 字节指针变量)。

So size of pointer is not same in all machines.

所以指针的大小在所有机器上都不一样。