指针 *变量分配了多少内存?
我的问题:
int
float
char
- str str
这些var类型每个分配了为其分配的字节的标准化分配。但是我从来没有为指针了解这一点:
这些“地址”存储变量,其内容似乎只是一系列字母数字字符。
C ++代码:
int *ptr1;
char *ptr2;
void *ptr3;
那么,上面列出的指针的标准内存分配是什么?总的来说?
My question:
int
float
char
str
These var types each have a standardized allotment of bytes allocated for them. But I never learned about this for pointers:
These "address" storing variables, whose contents seem like just a series of alphanumeric characters.
C++ code:
int *ptr1;
char *ptr2;
void *ptr3;
So what is the standard memory allocation for the pointers listed above? And in general?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
指针具有大小
sizeof(ptr1)
字节,这大约是所有标准指定的。从理论上讲,不同的对象指针可以具有不同的大小,并且不同的功能指针也可能具有不同的尺寸。它归结为引擎盖下面的是,当说32位CPU等时,我们通常意味着CPU数据大小(最大块可以在单个指令中进行处理)不一定与地址总线宽度/可寻址内存空间相对应。后者决定了指针的大小。
在实践中(简化):
8和16位微控制器系统,以及旧的16位PC可能具有多种指针尺寸,通常在8到32位之间,其中16位最常见。他们通常使用非标准关键字,例如指针质质 或
far
来指示这一点。某些ISA可能具有“零页”,这意味着第一个256个地址的8位指针,他们可以在其中更快地执行代码/访问数据(通常这将在Von Neumann McUs上)。同样,它们可以超出普通65536字节的扩展内存,这可能使执行/数据访问较慢。 24位指针并不少见。
32位系统通常具有32位指针。
64位系统通常具有64位指针。
如果您出于某种原因需要抓住绝对地址,则
uintptr_t
是便携式整数类型,该类型应该足够大,可以在给定系统上保存任何指针地址。A pointer has the size
sizeof(ptr1)
bytes and that's about all the standard specifies. In theory, different object pointers can have different sizes, and different function pointers may have different sizes as well.What it boils down to underneath the hood is that the CPU data size that we usually mean when speaking of 32 bit CPU etc (the max chunk it can process in a single instruction) does not necessarily correspond to the address bus width/addressable memory space. The latter determines the size of pointers.
In practice (simplification):
8 and 16 bit microcontroller systems, as well as old 16 bit PC may have diverse pointer sizes, typically between 8 and 32 bits, where 16 bits is most common. They usually use non-standard keyword such as pointer qualitifers
near
orfar
to indicate this.Some ISA might have a "zero page", meaning 8 bit pointers to the first 256 addresses, where they can execute code/access data faster (usually this will be on von Neumann MCUs). Similarly, they can have extended memory beyond the normal 65536 bytes, which may give slower execution/data access. 24 bit pointers aren't uncommon.
32 bit systems typically have 32 bit pointers.
64 bit systems typically have 64 bit pointers.
In case you need to grab the absolute address for some reason, then
uintptr_t
is the portable integer type which should be large enough to hold any pointer address on the given system.