c++ - New Operator With Vectors -


these questions relatively straight forward. when using vectors, should use new operator when pushing new element? , release method should call? here's mean:

// release method: 1. void releasemethodone( vector< int * > &thisvector ) {     // clear out vector.     thisvector.clear( );     return; }  // release method: 2. void releasemethodtwo( vector< int * > &thisvector ) {     // clear out vector.     for( unsigned uindex( 0 ); uindex < thisvector.size( ); uindex++ )     {         delete thisvector.at( uindex );     }     return; }  int main( ) {     vector< int * > vector;      // add new element.     vector.push_back( new int( 2 ) );      // more code...      // free elements before exiting. method should call here?     releasemethodone( vector ); // one?     releasemethodtwo( vector ); // or one?      return 0; } 

i've started learning vectors not long ago , book learning said vector's clear( ) method called each of elements destructor. apply new operator?

stl containers store copies of objects give them, pointers in example. never release memory you explicitly allocated. have deallocate memory yourself, second "release" method should used.

of course, don't need new every int. use vector<int> instead - won't have deal manual memory management @ all.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -