DLL shared memory

How does shared memory work in a DLL?

When a DLL connects to a process, it uses the same memory addresses as the process. Suppose we have the following function in a DLL:

int * data = 0;
int foo()
{
    if (!data) data = new int(random());
    return *data;
}

When process A calls this function, it creates a new object (int) and returns its value. But now process B attaches this DLL. It calls foo (), but I don’t understand how it works because it datais in the process. ' How could B use it directly?

+4
source share
3 answers

You are right, DLLs do not use shared access by all processes by default. In your example, both processes A and B will receive a separate instance of "data."

, DLL, , DLL, , . , .

+5

: dlls , , (-), (). , DLL , , , , . - .

, , - . , . .

+2

B , A. data B.

0

All Articles