473,667 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14096
TB
yp*********@ind iatimes.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*********@ind iatimes.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*********@ind iatimes.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*******@gmai l.com"
<sy*******@gmai l.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*********@in diatimes.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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::cl ear() 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>(s ize); }
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(rectLi st.begin(), rectList.end(),
get_area<Rect*> ( size ));

return p.area;
}

Jan 12 '06 #9
>>yp*********@i ndiatimes.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
10018
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 this an unsafe transfer between heap and stack // or does the allocator handle it safely?
5
10643
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 ob objekt. It works in the following way
1
1816
by: cylin | last post by:
Dear all, Here is my code. ------------------------------ #include <iostream> #include <vector> using namespace std; class A { public:
10
4828
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
2894
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
2189
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 std::vector<T> { public:
18
9068
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 pointer, the size is not fixed. I remember, all new statement should be followed by a delete statement, is there some memory leak here?
2
35563
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 implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
18
1809
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
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.