简体   繁体   English

如何代表指向我的地址空间开头的指针?

[英]How do I represent a pointer to the beginning of my address space?

Let's consider some strange hypothetical embedded system where I need to produce and use a pointer that points to, or might point to, the first byte of my address space. 让我们考虑一些奇怪的假设嵌入式系统,我需要在其中生成并使用一个指向或可能指向地址空间第一个字节的指针。 That is, a pointer equal to zero. 即,指针等于零。 Not a NULL pointer, but a perfectly valid zero pointer, which might get dereferenced or incremented or treated as an array just like any other pointer. 不是NULL指针,而是完全有效的零指针,就像其他任何指针一样,它可能会被取消引用,递增或被视为数组。 How would I do this in languages that treat a NULL pointer as special, but don't have a real NULL type? 在将NULL指针视为特殊但没有真正的NULL类型的语言中,我该如何做呢?

The whole "NULL pointer having a special value different than 0 treated special" is a possibility on some rare old machines, but in practice, your NULL/0 pointer pointing to adress 0 will have value 0 and really point to address 0 on most machines. 在某些罕见的旧机器上,可能会出现整个“ NULL指针的特殊值不同于0的特殊”的情况,但实际上,指向地址0的NULL / 0指针将具有值0,并且实际上在大多数机器上都指向地址0 。

It will be perfectly dereferenceable, and you can write on that address if there is some memory mapped to this area. 这将是完全可取消引用的,如果有一些内存映射到该区域,则可以在该地址上写入。

I used to have that on an embedded platform I worked with. 我曾经在与之合作过的嵌入式平台上使用它。 So I used to put a debugger watchpoint on address 0, since all accesses to this area were NULL-pointers dereferences. 所以我以前将调试器监视点放在地址0上,因为对该区域的所有访问都是NULL指针取消引用。 Caught some errors that way ^^ 以这种方式捕获了一些错误^^

In most architectures there's nothing special about a NULL pointer at all, it's just a regular pointer with an address of 0. If you're working with an embedded system that has actual memory there, it should work properly. 在大多数体系结构中,NULL指针完全没有什么特别的,它只是地址为0的常规指针。如果您使用的是带有实际内存的嵌入式系统,则它应该可以正常工作。 The only problem comes if you try to check the pointer for a null value, so don't do that. 唯一的问题是,如果您尝试检查指针是否为空值,请不要这样做。

If it helps, register definitions (pointer to a specific address) are written (in my copy of CodeWarrior) as: 如果有帮助,则将寄存器定义(指向特定地址的指针)写入(在我的CodeWarrior副本中)为:

#define REGISTER_XYZ      (*(vuint32 *)(0x40100000))

Note the vuint , as it's a volatile location (may change outside of our control) which may or may not be relevant to you. 请注意vuint ,因为它是一个不稳定的位置(可能会在我们的控制范围之外更改),可能与您无关。

And replace 0x40100000 with your address of choice, natch. 并将0x40100000替换为您选择的地址,natch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM