Thread: Asking windows for memory on a fixed address

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Asking windows for memory on a fixed address

    Hi,

    I basically want to do something like this:

    Code:
    struct S
    {
        int* i;
        S():i(new int(5)){}
    }
    
    int main()
    {
    //time t:
        std::vector<char> M;
        ...
        S* pS1 = new S;   //I use my own version of new which puts the S object into my own memory M
        save_M_to_disc();
        return;
    
    
    //time t+x:
        load_M_from_disc();
        S* pS1 = &M[0];
        printf("%d\n", *(pS1->i) );  //prints 5
        return;
    }
    Of course this will not work, because the address there M lives in memory (&M[0]) is probably different on t and t+x.
    The solution would be to ask windows for a special range/page of memory which is always on the same address of the virtual process memory.

    I think I've seen something like this done on a posix system.
    Are there any known solutions for windows?

    Thank you in advance!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up the placement new operator.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Yes, I use the placement together with operator new to put my S-Objects into my manual-managed peace of memory.
    But I don't see how that solves the problem of saving/loading pointers to/from disc.
    I need my manual-managed peace of memory on a fixed address offset inside my process memory, else the access to *(S->i) will cause an access error. And I'm looking for a way how to achieve this.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    VirtualAlloc, though I'd be inclined to VirtualQuery the address first to make sure it's not already allocated. If you don't check, and the address you want is already allocated, VirtualAlloc will return succees and you'll have yourself a fun debugging session as your value gets overwritten by whatever else allocated the page.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Looks quite promising. I'll try it. Thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM