yolo
yolo

Reputation: 2795

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' ?

Upvotes: 0

Views: 488

Answers (3)

EvilTeach
EvilTeach

Reputation: 28837

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.

Upvotes: 1

cnicutar
cnicutar

Reputation: 182619

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");

Upvotes: 3

Alok Save
Alok Save

Reputation: 206518

Yes it is valid if 0xF0000 is the starting address of the four character string "four"

Upvotes: 5

Related Questions