Originally Posted by
mickey
Hi I'm trying to do this (and it seem works):
std::vector<Light*> light; // it's a vector of pointer to Light
Light* l;
l = new Light[10];
light.push_back(l);
delete [] l; //delete is necessary
std::vector<Light*> light; // it's a vector of pointer to Light
Light* l;
l = new Light[10];
light.push_back(l);
delete [] l; //delete is necessary
To copy to clipboard, switch view to plain text mode
Here you use that vector as if it was a vector of arrays, but there is one problem --- if you delete the "l" pointer, the pointer you have appended to the vector will be invalid, because it will point to a deleted array.
It should be:
std::vector<Light*> light;
Light* l;
l = new Light[10];
light.push_back(l);
// ... use light ...
// delete the contents of light vector
for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
delete [] *i;
}
lisht.clear();
std::vector<Light*> light;
Light* l;
l = new Light[10];
light.push_back(l);
// ... use light ...
// delete the contents of light vector
for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
delete [] *i;
}
lisht.clear();
To copy to clipboard, switch view to plain text mode
Originally Posted by
mickey
Otherwise:
std::vector<Light*> light; // it's a vector of pointer to Light
Light* l;
l = new Light;
for (int i=0; < 10< i++)
light.push_back(l);
delete [] l; //delete is necessary
std::vector<Light*> light; // it's a vector of pointer to Light
Light* l;
l = new Light;
for (int i=0; < 10< i++)
light.push_back(l);
delete [] l; //delete is necessary
To copy to clipboard, switch view to plain text mode
Here you use the same variable as if it was a vector of pointers to Light instance, but again there is a problem --- all elements point to the same instance, if you delete it, all pointers in the vector will be invalid.
std::vector<Light*> light; // it's a vector of pointer to Light
for (int i=0; < 10< i++) {
Light* l = new Light();
light.push_back(l);
// or simply:
// light.push_back( new Light() );
}
// ... use light ...
// delete the contents of light vector
for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
delete *i; // note no []
}
lisht.clear();
std::vector<Light*> light; // it's a vector of pointer to Light
for (int i=0; < 10< i++) {
Light* l = new Light();
light.push_back(l);
// or simply:
// light.push_back( new Light() );
}
// ... use light ...
// delete the contents of light vector
for( std::vector<Light*>::iterator i = light.begin(); i != light.end(); ++i ) {
delete *i; // note no []
}
lisht.clear();
To copy to clipboard, switch view to plain text mode
Another problem is that you can't use delete[] to free memory allocated with new --- always use the same kind of operator: delete after new and delete [] after new[].
Originally Posted by
mickey
Here I've a vecotr of 10 elements (pointer) that each one point to a Light;
std::vector<Light> light;
for (int i=0; <10; i++)
light.push_back(Light());
std::vector<Light> light;
for (int i=0; <10; i++)
light.push_back(Light());
To copy to clipboard, switch view to plain text mode
I prefer the last....
Wich is better?
This is the safest way to use a vector.
Note that you can achieve the same with this:
std::vector<Light> light;
light.resize( 10 );
// or even
// std::vector<Light> light( 10 );
std::vector<Light> light;
light.resize( 10 );
// or even
// std::vector<Light> light( 10 );
To copy to clipboard, switch view to plain text mode
It will fill the vector with 10 copies of Light().
Bookmarks