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

问题描述

我刚开始C.我看了一下在各种书籍/教程指针和我了解的基础知识。但有一件事我还没有看到解释是什么数字。

I'm just starting C. I have read about pointers in various books/tutorials and I understand the basics. But one thing I haven't seen explained is what are the numbers.

例如:

int main(){
   int anumber = 10;
   int *apointer;

   apointer = &anumber;

   printf("%i", &apointer);
   }

可能会返回一个像4231168.这是什么号码重present?它是在RAM存储一些指定?

may return a number like 4231168. What does this number represent? Is it some storage designation in the RAM?

推荐答案

PC编程的很多回答一如既往。下面是从一个普通的编程点的视图答复。

Lots of PC programmer replies as always. Here is a reply from a generic programming point-of-view.

做任何形式的硬件相关的节目时,您会在地址的实际数值颇感兴趣。例如,可以访问硬件寄存器中的计算机通过以下方式:

You will be quite interested in the actual numerical value of the address when doing any form of hardware-related programming. For example, you can access hardware registers in a computer in the following way:

#define MY_REGISTER (*(volatile unsigned char*)0x1234)

这code假定您知道有位于地址0x1234一个特定的硬件寄存器。在计算机中的所有地址都是由传统的/在十六进制格式pssed方便EX $ P $。

This code assumes you know that there is a specific hardware register located at address 0x1234. All addresses in a computer are by tradition/for convenience expressed in hexadecimal format.

在这个例子中,该地址是16位长,这意味着地址总线的使用的计算机上是宽16位。在您的计算机的每个存储单元都有一个地址。这样一个16位地址总线上可以最大2 ^ 16 = 65536寻址存储器单元。有
在例如PC,该地址通常是32位长,给你4.29十亿寻址存储器单元,即4.29技嘉。

In this example, the address is 16 bits long, meaning that the address bus on the computer used is 16-bits wide. Every memory cell in your computer has an address. So on a 16-bit address bus you could have a maximum of 2^16 = 65536 addressable memory cells.On a PC for example, the address would typically be 32 bits long, giving you 4.29 billion addressable memory cells, ie 4.29 Gigabyte.

要解释一下宏细节:


  • 0x1234的是寄存器/内存单元的地址。

  • 我们需要通过一个指针来访问该存储单元,所以我们因此类型转换整型常量为0x1234成一个无符号的字符指针=指向一个字节。

  • 这是假设寄存器,我们感兴趣的是1个字节大。如果这是两个字节大,我们或许会使用了无符号短吧。

  • 硬件寄存器可以随时更新自己(他们的内容是动荡),所以程序不能被允许做任何假设/什么是存储在他们里面的优化。该方案已在每一次登记是在code用来读取从寄存器中的值。要实施这种行为,我们使用volatile关键字。

  • 最后,我们想访问,就好像它是一个普通的变量寄存器。因此*被加入,采取指针的内容。

现在的特定的存储位置可以由程序来访问:

Now the specific memory location can be accessed by the program:

MY_REGISTER = 1;
unsigned char var = MY_REGISTER;

例如,code这样使用的处处的嵌入式应用。

For example, code like this is used everywhere in embedded applications.

(但是,正如已在其他答复中提到,你不能做这样的事情在现代个人电脑,因为他们使用的是一种叫做虚拟寻址,让您的手指一巴掌,你应该尝试它。)

(But as already mentioned in other replies, you can't do things like this in modern PCs, since they are using something called virtual addressing, giving you a slap on the fingers should you attempt it.)

这篇关于C指针和物理地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 10:40