c++ - Who should clear a vector retrieved by reference? -
let's have simple code:
std::vector<int> getvector( int size ) { std::vector<int> res; ( size_t = 0; != size; ++i ) res.push_back( i* 23 ); return res; } int main () { std::vector<int> v; v = getvector(10); std::cout << "size1 " << v.size() << std::endl; v = getvector(15); std::cout << "size2 " << v.size() << std::endl; }
it outputs:
size1 10
size2 15
now, let's want change getvector
avoid useless object copies , optimize speed , memory usage. getvector
reference vector filled.
void getvector( int size, std::vector<int>& res ) { ( size_t = 0; != size; ++i ) res.push_back( i* 23 ); }
now, if change main function, without paying attention:
int main () { std::vector<int> v; getvector(10,v); std::cout << "size1 " << v.size() << std::endl; getvector(15,v); std::cout << "size2 " << v.size() << std::endl; }
it outputs:
size1 10
size2 25
which not had.
i've done kind of change (replaced returned value object passed @ reference) tones of times, , i've been asking myself same question: who should clear vector?
is there "guidline" or "general rule" saying should clear vector in case?
should function it?
void getvector( int size, std::vector<int>& res ) { res.clear(); ( size_t = 0; != size; ++i ) res.push_back( i* 23 ); }
or should caller it?
int main () { std::vector<int> v; getvector(10,v); std::cout << "size1 " << v.size() << std::endl; v.clear(); getvector(15,v); std::cout << "size2 " << v.size() << std::endl; }
edit:
example may mischosen because it's "bad optimization", reported many persons. there may cases vector retrieved reference , question remains. example:
bool hasdata( std::vector<int>& retrieveddata );
or
void splitvector( const std::vector<int>& originalvector, std::vector<int>& part1, std::vector<int>& part2 );
...
in first example function called "getvector" appropriate describe function does. returns vector number of elements, expected.
in second example same not apply. function still called "getvector" want clear vector. not describe function anymore because you're passing vector , doing things it. it's more "takeavectorclearitandinitializeit". , don't want name function or use in code. (naturally i'm exaggerating little bit, idea)
to make function easy understand should have 1 clear function , not hidden things in background (like clearing client's vector). if use function i'd confused why vector got cleared.
usually it's client code responsible managing own variables. if client code calls function clear vector responsibility still in client code intention of clearing vector. in case, know client code "getvector" function because made them both not confuse you, when write code needs 1 of 2 functions (clear or initialize), may changes affect code used function , create more problems.
on answering problem more specifically.
if don't want copy vector passing reference fine name function more descriptive "initializevector" it's more clear you're doing.
you should call 'clear' client code though, first example better until have performance issues. (the clear implicit you're getting different vector)
Comments
Post a Comment