
Globally Shared Memory
First of all, you should be aware that global memory and
MFC don’t work very well. This is because, although the physical memory itself
is in common to all applications everywhere, the address at which the
applications themselves think that the memory resides are expressed in virtual
terms. In this case, the physical and the virtual are not necessarily the same.
Therefore, any pointers inside of the global memory are unreliable. MFC uses a
lot of pointers. Oops.
Having said that, here’s how to make globally shared
memory:
First, tell the compiler that you are going to start a new
data segment, include variables as needed, end the segment and, finally, tell
the linker that the segment is to be placed into globally accessible memory at
run time.
There is one special gotcha about global variables. They
have to be initialized or they’ll end up in process local memory space despite
being defined as being a member of global memory space.
#pragma
data_seg(".MYSEC")
int iSharedVar = 0;
#pragma data_seg()
The Dev Studio C++ compiler allows you to set the linker switch in your code
that will tell the linker to put the defined segment into a common global memory
space at run time. To do this, include the following line in your code,
preferably near the #pragma data_seg(".MYSEC") line:
#pragma comment(linker, "/SECTION:.MYSEC,RWS")

|