指针大小:相关因素 [英] Size of pointers: Dependent factors

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

问题描述

我发现在理解 C 中指针变量的大小所依赖的因素方面存在困难.我查阅了一些参考资料,直到现在我得到的唯一信息是pointer 大小取决于处理器架构.我想知道以下详细信息

  • 请详细说明架构如何影响指针大小.
  • 一般来说,如果指针是 x 位,那么 0 到 2^(X)-1 个地址位置应该在那里.我正在迷失方向将地址位置的数量与程序可用的实际内存量相关联.

解决方案

指针是保存另一个内存位置地址的变量.

现在,如果您在 32 位架构上运行,那么保存内存引用的 CPU 寄存器(很可能还有所有其他寄存器)将是 32 位长度;这基本上就是 32 位(寄存器是 32 位字长)的意思,因此一个指针(这是一个内存位置)通常是 32 位长(4 字节)>

同样适用于 64 位 CPU,因此为 64 位 CPU 编译的 C 程序中的指针通常有 8 个字节的长度(64 位)


还请注意,在大多数现代架构中,您并没有真正用代码寻址物理内存;您运行并寻址所谓的虚拟内存.

基本概念是 CPU/OS 组合让您的程序相信您拥有完整的地址空间.

同样,地址空间(您可以在内存中寻址的空间)长度将取决于 CPU 可以寻址多远的位置,并且(在一般情况下)取决于其字长.

I am finding difficulties in understanding the factors on which the size of pointer variables in C is dependent on. I checked few references, the only information I got until now is pointer size is dependent on the processor architecture.I would like to know the following details

  • Please explain more on how the architecure impacts the pointer size.
  • In general, if the pointer is of x bits then 0 to 2^(X)-1 number of address locations should be there.I am losing track while relating the number of address locations and the actual amount of memory available to the program.

解决方案

A pointer is a variable that holds the address of another memory location.

Now if you are running on a 32-bit architecture, the CPU's registers that hold memory references(and most likely, all other registers too) will be of 32-bit length; that's basically what's meant by 32-bit(the registers are of 32-bit word length) and hence a pointer(which is a memory location) would usually be 32-bits long(4 bytes)

Same applies to 64-bit CPUs, and hence the pointers in a C program compiled for 64-bit CPUs will usually have 8 bytes length(64 bits)

EDIT:
Please also note that in most modern architectures you don't really address physical memory with your code; you run and address what's called a Virtual Memory.

The basic concept is that the CPU/OS combination illude your program that you have the full address space for you.

Again, the address-space(the space you can address in memory) length will depend on how far the CPU can address locations and that (in the general case) would depend on its word-size.

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

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