简体   繁体   中英

char* from memory address

Suppose some memory location 0xF0000 contains four character string "four" .

so is this valid:

char *mem = (char*) 0xF0000;

and then is mem[0] == 'f' ?

是的,如果四个字符串“ four”的起始地址为0xF0000,则它是有效的

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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