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

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.

Leave a Comment