PDA

View Full Version : allocate apecified memory location for a c variable



BharathKumar
06-01-2007, 01:16 AM
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.

MacGyver
06-01-2007, 01:57 AM
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.

AverageSoftware
06-01-2007, 04:00 AM
In C++, this can be done with placement new. (http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10)

Not sure about plain C.

Addendum:
Hmm, looking again at the way you phrased it, this probably isn't what you want. Sorry.

CornedBee
06-01-2007, 07:06 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.

Salem
06-01-2007, 10:19 AM
Is there anything to be gained by doing this in a virtual address space?

CornedBee
06-01-2007, 03:47 PM
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.