来自内存地址的 char*
假设某个内存位置 0xF0000 包含四个字符串 "four"
。
那么这是有效的:
char *mem = (char*) 0xF0000;
然后是 mem[0] == 'f' 吗?
Suppose some memory location 0xF0000 contains four character string "four"
.
so is this valid:
char *mem = (char*) 0xF0000;
and then is mem[0] == 'f' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,如果 0xF0000 是四个字符串“four”的起始地址,则有效
Yes it is valid if 0xF0000 is the starting address of the four character string "four"
当然是。如果内存映射了正确的权限(写入),那么它对操作系统应该没有任何影响。
测试这一点的一个简单方法是使用 gdb。您可以在指令尝试更改
mem
的值之前中断并更改它,使其指向某个内存。特别是,不要尝试修改字符串文字 (
char *readonly = "mystr"
);Sure it is. If the memory is mapped with the correct permissions (write) it should make no difference whatsoever to the operating system.
An easy way to test this is using
gdb
. You can break and change the value ofmem
an make it point to some memory, right before an instruction tries to change it.In particular, don't try to modify a string literal (
char *readonly = "mystr"
);是的。如果您使用 malloc 或在堆栈上分配它,那么它应该按预期运行。如果您刚刚选择了该地址,请注意,通常它可能随时被其他内容覆盖。
Yes it is. If you used malloc, or allocated it on the stack, then it should behave as expected. If you just picked that address, be aware that in general it may be overwritten by other things at any time.