char* from memory address
Suppose some memory location 0xF0000 contains four character string 开发者_JS百科"four"
.
so is this valid:
char *mem = (char*) 0xF0000;
and then is mem[0] == 'f' ?
Yes it is valid if 0xF0000 is the starting address of the four character string "four"
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 of mem
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"
);
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.
精彩评论