473,221 Members | 1,708 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,221 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 14021
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.