指针的大小 [英] size of a pointer

查看:99
本文介绍了指针的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是出于好奇,是指针的大小与它指向的类型的大小相同,还是指针总是具有固定大小?例如...

Just out of curiosity, is the size of a pointer the same as the size as the type its pointing to, or do pointers always have a fixed size? For example...

int x = 10;
int * myPtr = &x;
char y = 'a';
char * myPtr2 = &y;

std::cout << sizeof(x) << "\n";
std::cout << sizeof(myPtr) << "\n";
std::cout << sizeof(y) << "\n";
std::cout << sizeof(myPtr2) << "\n";

这会是什么输出?将myPtr返回4和myPtr返回1,或者2个指针实际返回相同的大小?我问这是因为指针是存储一个内存地址,而不是它们各自存储的地址的值。

What would the output of this be? Would myPtr return 4 and myPtr return 1, or would the 2 pointers actually return the same size? The reason I ask this is because the pointers are storing a memory address and not the values of their respective stored addresses.

推荐答案

指针一般有固定大小,例如。在32位可执行文件上,它们通常是32位。有一些例外,就像旧的16位窗口,当你不得不区分32位指针和16位...通常是很安全的假设他们将统一在现有的桌面操作系统上的给定可执行文件。

Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit. There are some exceptions, like on old 16-bit windows when you had to distinguish between 32-bit pointers and 16-bit... It's usually pretty safe to assume they're going to be uniform within a given executable on modern desktop OS's.

函数指针有所不同 - 请参阅 Jens'answer < a>获取更多信息。

Function pointers are a different story -- see Jens' answer for more info.

这篇关于指针的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆