Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can I Allocate a specitic memory address using pointers in c++ ?

For example: Allocate This memory address 25D4C3FA and put 4 in it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
456 views
Welcome To Ask or Share your Answers For Others

1 Answer

Allocating a specific address in your process's address space is a bit tricky and platform-specific. On Unix systems, mmap() is probably the closest you're going to get. The Windows equivalent is VirtualAlloc(). There are, of course, no guarantees since the address might already be in use.

Writing to a specific address is trivial:

char *p = (char*)0x25D4C3FA;
*p = 4;

I assume you have good reasons to want to do that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...