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.
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.
Last edited by AverageSoftware; 06-01-2007 at 06:01 AM.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Is there anything to be gained by doing this in a virtual address space?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law