int g_Value; // Note that the global is not initialized.
#pragma data_seg()
DLL提供两个接口函数:
int GetValue() { return g_Value; }
void SetValue(int n) { g_Value = n; }
然后启动两个进程A和B,A和B都调用了这个DLL,假如A调用了SetValue(5); B接着调用int m = GetValue(); 那么m的值不一定是5,而是一个未定义的值。因为DLL中的全局数据对于每一个调用它的进程而言,是私有的,不能共享的。假如你对g_Value进行了初始化,那么g_Value就一定会被放进MyData段中。换句话说,如果A调用了SetValue(5); B接着调用int m = GetValue(); 那么m的值就一定是5!这就实现了跨进程之间的数据通信!
// pragma_directive_data_seg.cpp
int h = 1; // stored in .data
int i = 0; // stored in .bss 等于0和没初始化的一样 int s; // stored in .bss
#pragma data_seg(".my_data1")
int j = 1; // stored in "my_data1"
#pragma data_seg(push, stack1, ".my_data2")
int l = 2; // stored in "my_data2"
#pragma data_seg(pop, stack1)// pop stack1 off the stack一直弹出栈,直到弹出identifier为stack1的为止;stored in "stack_data1"
int m = 3;
int main() {
}