Hi John,
Thanks. When I said cast, I indeed phrased wrongly: I had meant &v[0] as you suggested. However, I think it is still not the best thing to do: C++98 standard does not guarantee that the memory allocated by std::vector will be contiguous, that requirement was added in TR1. Luckily, I think all compilers do it contiguously these days, that is why people can use &v[0].
But please note that if the size of the std::vector changes, std::vector might carry the elements elsewhere in memory and the memory address for v.begin() might change. Furthermore, my question might not be simply a std::vector to c-array dereferencing question, it is related to how the ROOT authors implemented std::vectors in TTree and whether TTree->SetBranchAddress() works safely when I give a pointer to a c-array instead of a pointer to an std::vector to it.
Thanks for the input though.
Cheers,
e.
On 17 Jan 2011, at 01:42, John Idarraga wrote:
> Hello Erkcan, > > This is fine because there is not really a cast taking place. If you create a std::vector of floats for instance: > > vector<float> v; > > you can always safely reference the array of floats inside by doing &v[0] (say for example in the constructor of a TGraph ... that kind of situations). > > So it is safe as long as you deal correctly with the size of the array. You probably have a variable for that. In case you don't there is always the clever trick of sizeof(array)/sizeof(float) ;) > > cheers ! > > John Idarraga > > On 01/16/2011 05:59 PM, Erkcan Ozcan wrote:
-- In case they are not written explicitly, please be aware that my greetings and farewell are inherently implied in this email. V. Erkcan Ozcan Research Fellow University College London Dept. of Physics & Astronomy -- In case they are not written explicitly, please be aware that my greetings and farewell are inherently implied in this email. V. Erkcan Ozcan Research Fellow University College London Dept. of Physics & AstronomyReceived on Mon Jan 17 2011 - 18:57:30 CET
This archive was generated by hypermail 2.2.0 : Mon Jan 17 2011 - 23:50:01 CET