PDA

View Full Version : <vector> and new

mickey
16th May 2006, 17:09
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

I I understand fine, this is a vector of Light with only one element(a pointer) that point to an array of 10 Light. Is it this?
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

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());

I prefer the last....
Wich is better? Thanks

wysota
16th May 2006, 18:05
But these are different. The first one stores arrays of objects and the last one stores objects -- the first one is twodimensional whereas the last one is onedimensional.

jacek
16th May 2006, 18:09
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

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();

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

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();

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[].

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());

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 );It will fill the vector with 10 copies of Light().

mickey
16th May 2006, 19:07
Anther question: the vector <Light> is member of a Class A; I putted only delete [] l inside the destructor of A; Is it stiil necessary the vector destructor as you write and clear()? If I destruct the class that contatin vector....
Thanks

mickey
16th May 2006, 19:41
Furthermore in the first case: coding delete [] *i; Are you deleting the same space of memory more time? There's only one array of Light[10]....

jacek
16th May 2006, 20:05
Anther question: the vector <Light> is member of a Class A; I putted only delete [] l inside the destructor of A; Is it stiil necessary the vector destructor as you write and clear()? If I destruct the class that contatin vector....
No, you don't have to --- the vector will clear itself when you destroy it.

Furthermore in the first case: coding delete [] *i; Are you deleting the same space of memory more time? There's only one array of Light[10]....
If there is only one array, you don't need the vector.

mickey
17th May 2006, 22:05
Hence, if I code as below is clear() necessary? Thanks

class Scene {
std::vector<Light> light;
};
Scene::Scene() {
for (int i=0; <10; i++)
light.push_back(Light());
}

Scene::~Scene() {
light.clear(); // necessary?
}

wysota
17th May 2006, 22:12
The vector will clear itself when it's destructed so an explicit clear() is not necessary (and remember clear() doesn't free the memory occupied by items stored in the vector).

mickey
18th May 2006, 15:32
Sorry I've still compile problem; the first compile (and seem works too), the 2th no;Why? What is happening?

class Scene {
std::vector<Light> light;
};

Scene::Scene() {
light.push_back(Light());
}

Scene::~Scene() {
light.clear();
}

class Scene {
std::vector<Light> light(1); //with (1) don't compile
};

Scene::Scene() {
light.push_back(Light());
}

Scene::~Scene() {
light.clear();
}

qsignalslotimp.h(66): fatal error C1903: unable to recover from previous error(s); stopping compilation
mainform.ui.h(75): error C2109: subscript requires array or pointer type
mainform.ui.h(75): error C2228: left of '.getPosition' must have class/struct/union type
mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
mainform.ui.h(959): error C2109: subscript requires array or pointer type
mainform.ui.h(959): error C2228: left of '.setXPosition' must have class/struct/union type
..........................................

jacek
18th May 2006, 15:53
It should be:

class Scene
{
public:
Scene();
// ...
private:
std::vector<Light> light;
};

Scene::Scene() : light(1)
{
// ...
}

mickey
18th May 2006, 16:18
OK It works! but I don't understand where's the problem in my solutions.. Can't I give dimension of vector inside class? thanks

jacek
18th May 2006, 16:27
Can't I give dimension of vector inside class?
No, you can't. You can only declare member variables there.