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

北慕城南 提交于 2019-12-12 02:41:59

问题


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. How would I do this in languages that treat a NULL pointer as special, but don't have a real NULL type?


回答1:


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.

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. Caught some errors that way ^^




回答2:


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. The only problem comes if you try to check the pointer for a null value, so don't do that.




回答3:


If it helps, register definitions (pointer to a specific address) are written (in my copy of CodeWarrior) as:

#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.

And replace 0x40100000 with your address of choice, natch.



来源:https://stackoverflow.com/questions/12849246/how-do-i-represent-a-pointer-to-the-beginning-of-my-address-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!