473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 14050
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
5
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor...
1
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
13
by: morz | last post by:
My question is,this code is valid or not? can this code cause memory leaks? #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv) {
7
by: Vincent RICHOMME | last post by:
Hi, I would like to create a class called ArrayPtr that could only store pointers. How can I be sure that arguments passed is a pointer ? template<typename Tclass ArrayPtr : public...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.