From: Todd V. <tve...@ex...> - 1999-03-08 18:32:36
|
Matthias Troyer writes: > Today's question regards call by value of functions with array arguments > like in: > > #include <iostream.h> > #include <complex.h> > #include <blitz/array.h> > > using namespace blitz; > using namespace std; > > Array<double,1> kolu(Array<double,1>); > > int main() > { > Array<double,1> BB(1); > Array<double,1> CC(1); > BB=1; > > cout<<BB<<endl; > CC=kolu(BB); > cout<<CC<<endl; > cout<<BB<<endl; > } > > Array<double,1> kolu(Array<double,1> B) > { > B*=2; > return B; > } > > Assuming normal C++ semantics the argument of the kolu function should not > be changed by the function, but it is changed as can be seen from the > following output: > > [ 1 ] > [ 2 ] > [ 2 ] > > My question to you: is this a bug or a feature of Blits++? I'd hesitate to call it a "feature," because obviously it creates some problems like the one you describe. It's a result of reference-counting. Basically you have to think of array objects as being "handles" to underlying arrays. The function kolu(..) doesn't receive a copy of the array data, but rather a copy of the handle. So kolu::B is the same array data as main::BB. The alternative would be to copy the array data when passing by value, which would be grossly inefficient. Cheers, Todd --------------------- blitz-support list -------------------------------- * To subscribe/unsubscribe: mail to maj...@oo..., with "subscribe blitz-support" or "unsubscribe blitz-support" in the body of the message * Blitz++ web page: http://oonumerics.org/blitz/ |