Please make out a few lines of C codes for accessing a fixed memory location. Requirement is to write an int variable 0xaa55 into the fixed address 0x67a9.
answer:
int a = 0xaa55;
int *pa = 0x67a9;
*pa = 0xaa55;
这样写对吗?
...全文
47732打赏收藏
一道C笔试题,求助
Please make out a few lines of C codes for accessing a fixed memory location. Requirement is to write an int variable 0xaa55 into the fixed address 0x67a9. answer: int a = 0xaa55; int *pa = 0x67a9; *pa = 0xaa55; 这样写对吗?
为什么小于64KB就不行呢?
[/Quote]
进程地址空间中的0x00000000 -- 0x0000FFFF 共64KB属于“NULL指针分配的分区”。
进程地址空间的这个分区的设置是为了帮助程序员掌握N U L L指针的分配情况。如果你的进程中的线程试图读取该分区的地址空间的数据,或者将数据写入该分区的地址空间,那么C P U就会引发一个访问违规。保护这个分区是极其有用的,它可以帮助你发现N U L L指针的分配情况。
如果m a l l o c不能找到足够的内存来满足需要,它就返回N U L L。但是,该代码并不检查这种可能性,它认为地址的分配已经取得成功,并且开始访问0 x 0 0 0 0 0 0 0 0地址的内存。由于这个分区的地址空间是禁止进入的,因此就会发生内存访问违规现象,同时该进程将终止运行。这个特性有助于编程员发现应用程序中的错误。
[Quote=引用楼主 zqh2007 的回复:]
Please make out a few lines of C codes for accessing a fixed memory location. Requirement is to write an int variable 0xaa55 into the fixed address 0x67a9.