C++ accessing shared memory

Posts: 592
Joined: 2002.12
Post: #1
Can anyone suggest / point me in the direction of a way of reading / writing shared memory in a platform independent environment using C++?
Quote this message in a reply
Posts: 1,140
Joined: 2005.07
Post: #2
I only know of the UNIX way to get shared memory, which is outlined at this page. You can look up the Windows way to do it, assuming it's possible, and use #ifdefs to separate the two.

Now when getting this to work in C++, this is a prime opportunity to overload the new and delete operators. For the objects that require shared memory, you can overload the new and delete operators to allocate shared memory instead of regular heap memory. Note that these are implicitly static member functions, so you can't store the ID. Fortunately, you can overload the new operator to take an additional argument after the one you need (the size) to take a reference to the ID, which you can then pass into the constructor using a local variable as a go-between. You would call it like so:
int memID;
Whatever *obj = new(memID) Whatever(normal, args, memID);

For deleting the object, you can safely typecast the void * that's passed in to the class type, though the destructor will have already been called. Assuming that you have the shared memory ID as a regular datafield, it should be easily accessible. (though I'm sure it would be considered a "messy" way of accessing it)

This page shows how to overload the new and delete operators.

Now that I've showed you how to do it, take my advice: find an alternative to shared memory. You only have about 4 MB or so available through the entire system at one time. If it gets leaked by accident, there's no recovering it until you restart. I used it before in Fractographer and found it too limiting, and used a temporary file instead. (I was using forks at that point, but I have since moved to just using threads after making an array of the information to access and having all the operators take a parameter as to the index, rather than having a global copy in each PID; I no longer need to use shared memory or a temporary file in this case since it's in the same process) If you can, I'd use some other methods such as a temporary file, a pipe, or socket to ferry your info across. (you could do something like have a constructor that takes a socket ID and reads the necessary info from the socket, and/or a synchronize method that sends info from one object to another) You may still find that you need to use shared memory in (very) small amounts just to share states, though. (I had to in order to share states between the forks in Fractographer after implementing the temporary file but before changing over to threads, for example, to know when each process was done) Keep in mind that each allocation to shared memory ends up taking up a rather large block regardless of your size, (in the order of at least 1 KB) so if you're moving several states around, it would be best to have a single array or single object.
Quote this message in a reply
Post Reply