Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. persistent pointer/memory allocation

persistent pointer/memory allocation

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancetutorial
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Ylno
    wrote on last edited by
    #1

    Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

    J L _ E A 5 Replies Last reply
    0
    • Y Ylno

      Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      You can used shared memory, though the second program would have to start and open the shared memory before the first exited.

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      1 Reply Last reply
      0
      • Y Ylno

        Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, Have a look at "memory mapped files"; these are files mapped into memory, or memory blocks mapped to an actual file, depending how you look at it. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        1 Reply Last reply
        0
        • Y Ylno

          Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          Ylno wrote:

          . When this program finished I do not want to free up this memory location

          This is not an option. It is freed or marked as free by the OS. And could possibly be immediately used by another program and the earlier contents erased. You should use persistent storage. Files, Registry ...

          «_Superman_» I love work. It gives me something to do between weekends.

          1 Reply Last reply
          0
          • Y Ylno

            Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #5

            Ylno wrote:

            When this program finished I do not want to free up this memory

            Reminds me of good ol TSR.

            Ylno wrote:

            want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up.

            We have something similar at work. Yes you can do it. But you should host an all-time "memory-server". It's a complex thing. You'll have to do quite a lot of marshalings. A new process *Cannot* just starting using memory from another application. I've used those "memory-serving" services but don't know the implementation details. But the abstract idea resembles DCOM.

            He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

            1 Reply Last reply
            0
            • Y Ylno

              Hello All, A little question about memory allocation. Yes, nos and look heres greatly accepted... I'm not sure how to word the question so I'll say what I want to do: I want to, from one program, allocate a block of memory noting its memory address/pointer. When this program finished I do not want to free up this memory location but rather be able to access it from a second program and be guarenteed of what I'll find there. Basically I want to be able to take this address and, in a completly seperate program, access this memory and then deacllocate/free it up. Is this possible? Thanks...

              A Offline
              A Offline
              abiemann
              wrote on last edited by
              #6

              I like to keep as much as possible in RAM for fast read/write operations... my two suggestions: Option 1 you could create a RAM Drive, then just save to a file. The two program share the data by accessing the same file in turn. Option 2 You could also create a third program (that is started before the first program, and ends after the 2nd program) that sets up a shared file with CreateFileMapping():

              char \* pMyFilePointer = NULL;
              HANDLE hMyFileHandle = CreateFileMapping((HANDLE)-1,
                                                    NULL,
                                                    PAGE\_READWRITE,
                                                    0,
                                                    10,
                                                    "MySharedFile");
              
              if (hMyFileHandle != NULL)
              {
                  pMyFilePointer = (char \*) MapViewOfFile(hMyFileHandle, FILE\_MAP\_WRITE, 0, 0, 10);
              
                  if (pMyFilePointer != NULL)
                  {
              
                      printf("file mapped to %p",pMyFilePointer);
                      
                      \*(pMyFilePointer+0) = (unsigned char)0;
                      \*(pMyFilePointer+1) = (unsigned char)0xbb;
              

              Then, your other two programs open the file with:

              HANDLE hMyFileHandle;
              char * pMyFilePointer;

              //open data share file
              hMyFileHandle = OpenFileMapping(FILE\_MAP\_WRITE, TRUE, "MySharedFile");
              
              if (hMyFileHandle != NULL)
              {
                  pMyFilePointer = (char \*) MapViewOfFile(hMyFileHandle,FILE\_MAP\_WRITE, 0, 0, 10);
              
                  //example write operation
                  \*pMyFilePointer = (unsigned char)0xAA;
              }
              
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups