How do C++ compilers actually pass reference parameters?
- by T.E.D.
This question came about as a result of some mixed-langauge programming. I had a Fortran routine I wanted to call from C++ code. Fortran passes all its parameters by reference (unless you tell it otherwise).
So I thought I'd be clever (bad start right there) in my C++ code and define the Fortran routine something like this:
extern "C" void FORTRAN_ROUTINE (unsigned & flag);
This code worked for a while but (of course right when I needed to leave) suddenly started blowing up on a return call. Clear indication of a munged call stack.
Another engineer came behind me and fixed the problem, declaring that the routine had to be deinfed in C++ as
extern "C" void FORTRAN_ROUTINE (unsigned * flag);
I'd accept that except for two things. One is that it seems rather counter-intuitive for the compiler to not pass reference parameters by reference, and I can find no documentation anywhere that says that. The other is that he changed a whole raft of other code in there at the same time, so it theoretically could have been another change that fixed whatever the issue was.
So the question is, how does C++ actually pass reference parameters? Is it perhaps free to do copy-in, copy-out for small values or something? In other words, are reference parameters utterly useless in mixed-language programming? I'd like to know so I don't make this same code-killing mistake ever again.