pointer to a vector? shall i delete it or clear it?

Y

ypjofficial

Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

regards,
Yogesh Joshi
 
T

TB

(e-mail address removed) sade:
Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

regards,
Yogesh Joshi

delete vptr;

will be sufficient.

TB
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

If you allocate memory you also should delete it => delete vptr.

Regards, Stephan
 
C

Clark S. Cox III

Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

Just like any other object that you've allocated via the new operator,
you should delete it when you are finished with it:

delete vptr;

calling vptr-clear() will not delete the vector itself; it will merely
destroy the objects contained within the vector.
 
R

Rolf Magnus

Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

Why do you allocate the vector dynamically?
and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?

Just like any other pointer to a dynamically allocated object.
shall i do delete vptr or
vptr->Clear() is enough?

If you mean vptr->clear(), this will only remove all the elements from the
vector. It will not destroy the vector itself.
 
S

sylcheung

You said "Why do you allocate the vector dynamically?".
Is it a bad practice to do that? If yes, why?
 
W

W Marsh

You said "Why do you allocate the vector dynamically?".
Is it a bad practice to do that? If yes, why?

Because you then have to make sure that it is cleared up properly, and
that the pointer referencing the vector isn't accidentally used
elsewhere, and that when it is used you don't accidentally do
something invalid, and... the list goes on.

If you don't need to use a pointer and dynamic allocation with new and
delete (and believe me, you rarely do) then just don't do it.
 
M

Mike Wahler

Hello All,

In my program I am using a pointer to a vector
vector<XYZ> * vptr = new vector<XYZ>;

and also the XYZ class has a char* as one of its member.I have created
all the copy constructor, assignment operator and destructor (The Big
Three ) for XYZ.

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

(In both the cases the destructor of the XYZ objects are called..)

std::vector::clear() removes all the vector's elements.
The vector object itself is still there. If you created
it with 'new', you need to destroy it with 'delete'.
This applies to any object created with 'new'.

-Mike
 
S

sylcheung

But this is the same for "new" any class, not just STL containers. Am I
correctly?

I have a function which go thru a list of Rect ( list<Rect*>), call the
'area' attribute and put that in a vector. Is there a better way to
achieve what I want?

Here is what I did:

template<class T> struct get_area : public unary_function<T, void>
{
get_area(int size) { area = new vector<float>(size); }
void operator() (T x) {
area->push_back(x->area);

}

public:
vector<float>* area;
};

vector<float>* getArea(const RectList& rectList) {
int size = rectList.size();

get_area<Rect*> p = for_each(rectList.begin(), rectList.end(),
get_area<Rect*>( size ));

return p.area;
}
 
S

Sandeep

My question is how to free the memory held by vptr?
shall i do delete vptr or
vptr->Clear() is enough?

For memory management there is a concept of smart pointers. boost
library has an implementation of it as well - do have a look at the
concept and the library .. A simple smart pointer can be implemented
very easily....
 
E

Earl Purple

But this is the same for "new" any class, not just STL containers. Am I
correctly?

I have a function which go thru a list of Rect ( list<Rect*>), call the
'area' attribute and put that in a vector. Is there a better way to
achieve what I want?

Here is what I did:

template<class T> struct get_area : public unary_function<T, void>
{
get_area(int size) { area = new vector<float>(size); }
void operator() (T x) {
area->push_back(x->area);

}

public:
vector<float>* area;
};

vector<float>* getArea(const RectList& rectList) {
int size = rectList.size();

get_area<Rect*> p = for_each(rectList.begin(), rectList.end(),
get_area<Rect*>( size ));

return p.area;
}

It is not necessarily wrong to call new for a vector type. In your
example, there are many dangers. Firstly you are returning a vector
created with new but the user is not calling new themselves and are
likely to forget that they must call delete. Presumably you are
expecting your user to store the result in a smart pointer, possibly
auto_ptr.

I would use std::transform rather than for_each by the way. And then I
don't think you really need a function to do it, but if so and you
don't want to rely on RVO then have your function take a reference to a
vector.

struct AreaTransform
{
float operator()( const Rect* pRect ) const
{
return pRect->area;
}
};

std::vector< float > vect; // as parameter std::vector<float> & if in a
function)
vect.reserve( rectList.size() ); // if in a function, possibly clear
the vector first too
std::transform( rectList.begin(), rectList.end(), std::back_inserter(
vect ), AreaTransform() );

// if in a function and relying on RVO you can simply return vect
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top