I read more than once that passing by reference is simply passing a pointer, which is automatically dereferenced within a function. How true is this?
For example, I wrote the following dll (in Visual Studio):
extern "C" void __declspec(dllexport) test(int* i) { cout << "test(" << *i << ")\n"; (*i)++; } And such a program to work with it:
typedef void (*myfunc)(int&); int main() { HMODULE dll; if ((dll = LoadLibrary("Dll.dll")) != NULL) { myfunc f = (myfunc)GetProcAddress(dll,"test"); int q = 5; cout << "q = " << q << endl; f(q); cout << "q = " << q << endl; FreeLibrary(dll); } } Everything works, it turns out that I wanted:
q = 5 test(5) q = 6 Just how much can you rely on this transfer by reference as a pointer? What could be the trouble?
And I also immediately wanted to ask, for example, I use cout in a dll - is it the same cout as in the main program or not? And if I pass a pointer to it, then all the same, the code in the dll will be used, and not in the program? Or if I allocate memory in dll, then how to properly free it in the program? For example, if I return the unique_ptr in a function in the dll?