Is there any function to allocate memory at a given virtual address to a c/c++ variable ?
I have tried using mmap with flag MAP_FIXED for the same but it gives me a segmentation fault when I run it.
Printable View
Is there any function to allocate memory at a given virtual address to a c/c++ variable ?
I have tried using mmap with flag MAP_FIXED for the same but it gives me a segmentation fault when I run it.
I believe it's compiler specific to get a variable to be at a specific address. Consult your compiler's documentation or perform a google search for this type of stuff.
I know it's been discussed on the forums before, so you might want to search the forums here.
In C++, this can be done with placement new.
Not sure about plain C.
Addendum:
Hmm, looking again at the way you phrased it, this probably isn't what you want. Sorry.
Sounds exactly like what he wants. Of course, he first needs to make sure he can use the memory at that virtual address, e.g. by using mmap with MAP_FIXED.
You might also want to look into the Boost.Shmem library.
Is there anything to be gained by doing this in a virtual address space?
Yes. If several processes use shared memory or a shared mmapped file at the same virtual address, pointers into the memory are valid in both processes.