ARM-software / android-nn-driver

MIT License
151 stars 60 forks source link

Is this push_back to vector expected? #3

Closed zhenhuaw-me closed 6 years ago

zhenhuaw-me commented 6 years ago

https://github.com/ARM-software/android-nn-driver/blob/deb3bdbe028a59da0759dd7a560387d03a11d322/ArmnnDriver.cpp#L286

result has reserve model.operations.size() before the loop, if we are still pushing to result, will the size of result and model.operations be the same?

MatteoArm commented 6 years ago

Hi @jackwish,

The answer to your question is yes, as std::vector::reserve only increases the capacity of the vector to the desired amount, but it does not alter the size itself (see https://en.cppreference.com/w/cpp/container/vector/reserve for reference). It is used in that piece of code to prevent unnecessary reallocations for result. Since the size that we are reserving for result is the same as model.operations, pushing a number of items into result equal to model.operations.size() will ensure in both result and model.operations have the same size at the end of the loop. The above link to the documentation also features an example with a similar case (reserve + push_back in a loop).

I hope that helps Regards

zhenhuaw-me commented 6 years ago

cool~