Thread: allocate apecified memory location for a c variable

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    allocate apecified memory location for a c variable

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  5. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM