469,097 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

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

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

Jan 12 '06 #1
10 12995
TB
yp*********@indiatimes.com 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
Jan 12 '06 #2
If you allocate memory you also should delete it => delete vptr.

Regards, Stephan

Jan 12 '06 #3
On 2006-01-12 08:47:54 -0500, yp*********@indiatimes.com said:
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.
--
Clark S. Cox, III
cl*******@gmail.com

Jan 12 '06 #4
yp*********@indiatimes.com wrote:
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.

Jan 12 '06 #5
You said "Why do you allocate the vector dynamically?".
Is it a bad practice to do that? If yes, why?

Jan 12 '06 #6
On 12 Jan 2006 07:43:14 -0800, "sy*******@gmail.com"
<sy*******@gmail.com> wrote:
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.
Jan 12 '06 #7

<yp*********@indiatimes.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
Jan 12 '06 #8
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;
}

Jan 12 '06 #9
>>yp*********@indiatimes.com wrote:
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....

Jan 12 '06 #10

sy*******@gmail.com wrote:
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

Jan 12 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Steve Hill | last post: by
5 posts views Thread by Nils | last post: by
1 post views Thread by cylin | last post: by
10 posts views Thread by Bob | last post: by
13 posts views Thread by morz | last post: by
7 posts views Thread by Vincent RICHOMME | last post: by
18 posts views Thread by Goran | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.