c++ vector question.

Two questions:

1. Does .clear() member function in a vector free() memory called by new?

I wrote some code to check it out. I think the answer is no, because my destructor does not appear to be called. Am I right about this? (Code Below.)



2. When I go through a vector as in the loop below, I have to declare a const iterator like so:

for( vector<int>::const_iterator itr; blah; blah)

Is there anyway to declare itr by the variable name and not the variable's type? So:

for( points::const_iterator itr; blah; blah)

The reason for this, is because sometimes I want to change the variable's type, and now I have to go through and change my loop too. Plus writing vector<int>::const_iterator is rather verbose.


thanks!




/* vectorclear.cpp

does clearing a vector free allocated memory with new?
*/

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class ClearPoint {
public:
ClearPoint( string name);
~ClearPoint();
void print();
private:
string m_Name;
};


ClearPoint::ClearPoint( string name) {
m_Name = name;
}


void ClearPoint::print() {
cout << "name = " << m_Name << endl;
return;
}


ClearPoint::~ClearPoint() {
cout << "dtor() called for name = " << m_Name << endl;
}




int main() {
std::vector<ClearPoint*> points;

points.push_back( new ClearPoint( "alpha"));
points.push_back( new ClearPoint( "beta"));
points.push_back( new ClearPoint( "ceta"));

for( vector<ClearPoint*>::const_iterator itr = points.begin(); itr != points.end(); ++itr) {
ClearPoint &cp = **itr;
cp.print();
}


cout << endl << endl << "About to call clear." << endl;

points.clear();
cout << endl << endl << "Exitting." << endl;

return 0;
}
1. Does .clear() member function in a vector free() memory called by new?


No, it does not. It does, however, call the destructor for existing elements when cleared.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main() {
    std::vector<ClearPoint> points;

    points.push_back(ClearPoint( "alpha"));
    points.push_back(ClearPoint( "beta"));
    points.push_back(ClearPoint( "ceta"));

    for( vector<ClearPoint*>::const_iterator itr = points.begin(); itr != points.end(); ++itr) {
        itr->print() ;
    }


    cout << endl << endl << "About to call clear." << endl;

    points.clear();
    cout << endl << endl << "Exitting." << endl;

    return 0;
}



2. When I go through a vector as in the loop below, I have to declare a const iterator like so:



If your compiler supports the auto keyword:

for ( auto it = points.begin() ; it!=points.end(); ++it)


If it doesn't:

1
2
3
4
5
6
7
8
9
10
typedef std::vector<ClearPoint*> pointContainer ;

int main ()
{
    pointContainer points;
    // ...

    for ( pointContainer::const_iterator it = points.begin(); it!= points.end(); ++it )
    // ...
}
Last edited on
1. Does .clear() member function in a vector free() memory called by new?

I wrote some code to check it out. I think the answer is no, because my destructor does not appear to be called. Am I right about this? (Code Below.)

Part of code as follows:
std::vector<ClearPoint*> points;

Fatal error in understanding here - you have a vector of pointers.
when the vector does a clear() - it only clears out the pointers - the
destructors of the objects that the pointers are referencing are not going to get called.
Topic archived. No new replies allowed.