Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple delete question

Reply
Thread Tools

simple delete question

 
 
cppaddict
Guest
Posts: n/a
 
      05-20-2004
Hi,

I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I'm
not sure exaclty what I'm doing wrong.

I'd apprecitate any clarification and suggestions for rewriting the
below properly.

Thanks,
cpp

PS: The Char class below is used to store a pixel representation of a
character for display. A character in this context is simply a
collection of points.

#include <vector>
#include "Point.h"

class Char {
private:
char _ch;
std::vector<Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int, int);
std::vector<Point>& const getPixelPoints();
char getChar() {return _ch;}
};

Char::Char(char ch) :
_ch(ch)
{ }

Char::~Char() {
std::vector<Point>::iterator iter;
for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
iter++)
delete iter;
}

void Char::addPoint(int x, int y) {
_pixelPoints.push_back(*( new Point(x,y) ));
}

std::vector<Point>& const Char::getPixelPoints() {
return _pixelPoints;
}


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-20-2004

"cppaddict" <> wrote in message
news:...
> Hi,
>
> I am deleting some objects created by new in my class destructor, and
> it is causing my application to error at runtime. The code below
> compiles ok, and also runs fine if I remove the body of the
> destructor. So I think I am somehow using delete incorrectly, but I'm
> not sure exaclty what I'm doing wrong.


What you are doing wrong is using new and delete at all. There seems to be
an epidemic of needless use of new on this group at the moment.

Corrections below.

>
> I'd apprecitate any clarification and suggestions for rewriting the
> below properly.
>
> Thanks,
> cpp
>
> PS: The Char class below is used to store a pixel representation of a
> character for display. A character in this context is simply a
> collection of points.
>
> #include <vector>
> #include "Point.h"
>
> class Char {
> private:
> char _ch;
> std::vector<Point> _pixelPoints;
> public:
> Char(char);
> ~Char();
> void addPoint(int, int);
> std::vector<Point>& const getPixelPoints();
> char getChar() {return _ch;}
> };
>
> Char::Char(char ch) :
> _ch(ch)
> { }
>
> Char::~Char() {
> std::vector<Point>::iterator iter;
> for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
> iter++)
> delete iter;
> }


Don't need the destructor, remove it.

>
> void Char::addPoint(int x, int y) {
> _pixelPoints.push_back(*( new Point(x,y) ));


Don't need to use new

_pixelPoints.push_back(Point(x,y));

> }
>
> std::vector<Point>& const Char::getPixelPoints() {
> return _pixelPoints;
> }
>


Should be fine now.

john


 
Reply With Quote
 
 
 
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      05-20-2004
cppaddict wrote:

> I am deleting some objects created by new in my class destructor, and
> it is causing my application to error at runtime. The code below
> compiles ok, and also runs fine if I remove the body of the
> destructor. So I think I am somehow using delete incorrectly, but I'm
> not sure exaclty what I'm doing wrong.


A question: Why you "delete" something that you do not "new" ?

In addPoint you add:
*( new Point(x,y) )
and not:
new Point(x,y)
so you cannot delete it.

Probably you can rewrite your addPoint as:

void Char::addPoint(int x, int y) {
Point * p = new Point(x,y);
_pixelPoints.push_back(*p));
delete p;
}

and avoid to delete anything in ~Char().

Just my 2 cents...

- Dario
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-20-2004

"John Harrison" <> wrote in message
news:...
>
> "cppaddict" <> wrote in message
> news:...
> > Hi,
> >
> > I am deleting some objects created by new in my class destructor, and
> > it is causing my application to error at runtime. The code below
> > compiles ok, and also runs fine if I remove the body of the
> > destructor. So I think I am somehow using delete incorrectly, but I'm
> > not sure exaclty what I'm doing wrong.

>
> What you are doing wrong is using new and delete at all. There seems to be
> an epidemic of needless use of new on this group at the moment.
>


I'm curious as to what led you to use new at all. After all there are no
pointers in your code, so why did you consider using new?

Seems to be happening a lot lately so I'm interested in what makes new and
delete (and pointers generally) so attractive to newbies. Maybe you can
explain.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-20-2004

"Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <> wrote in
message news:c8i6nt$ahr$...
> cppaddict wrote:
>
> > I am deleting some objects created by new in my class destructor, and
> > it is causing my application to error at runtime. The code below
> > compiles ok, and also runs fine if I remove the body of the
> > destructor. So I think I am somehow using delete incorrectly, but I'm
> > not sure exaclty what I'm doing wrong.

>
> A question: Why you "delete" something that you do not "new" ?
>
> In addPoint you add:
> *( new Point(x,y) )
> and not:
> new Point(x,y)
> so you cannot delete it.
>
> Probably you can rewrite your addPoint as:
>
> void Char::addPoint(int x, int y) {
> Point * p = new Point(x,y);
> _pixelPoints.push_back(*p));
> delete p;
> }
>


That works but why?? Needless use of new, inefficient and error prone.

_pixelPoints.push_back(Point(x,y));

What's so hard about the above that newbies fail to use it?

john


 
Reply With Quote
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      05-20-2004
John Harrison wrote:

> "Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <> wrote in
> message news:c8i6nt$ahr$...
>
>> cppaddict wrote:
>>
>>> I am deleting some objects created by new in my class destructor, and
>>> it is causing my application to error at runtime. The code below
>>> compiles ok, and also runs fine if I remove the body of the
>>> destructor. So I think I am somehow using delete incorrectly, but I'm
>>> not sure exaclty what I'm doing wrong.

>>
>> A question: Why you "delete" something that you do not "new" ?
>>
>> In addPoint you add:
>> *( new Point(x,y) )
>> and not:
>> new Point(x,y)
>> so you cannot delete it.
>>
>> Probably you can rewrite your addPoint as:
>>
>> void Char::addPoint(int x, int y) {
>> Point * p = new Point(x,y);
>> _pixelPoints.push_back(*p));
>> delete p;
>> }

>
> That works but why?? Needless use of new, inefficient and error prone.
>
> _pixelPoints.push_back(Point(x,y));


Good point.

> What's so hard about the above that newbies fail to use it?


The explanation (at least for me) is the fact that I'm used
(since many years) to write in Java, where the only way to create
a new Object is via the "new" operator.
So (at least for me) the natural way is always
new Point(x, y)

But definitively in C++ the example mu be written as you said.

- Dario
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-20-2004

"Dario (drinking co?ee in the o?ce.)" <> wrote in message
news:c8i7td$ddi$...
> John Harrison wrote:
>
> > "Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <> wrote

in
> > message news:c8i6nt$ahr$...
> >
> >> cppaddict wrote:
> >>
> >>> I am deleting some objects created by new in my class destructor, and
> >>> it is causing my application to error at runtime. The code below
> >>> compiles ok, and also runs fine if I remove the body of the
> >>> destructor. So I think I am somehow using delete incorrectly, but I'm
> >>> not sure exaclty what I'm doing wrong.
> >>
> >> A question: Why you "delete" something that you do not "new" ?
> >>
> >> In addPoint you add:
> >> *( new Point(x,y) )
> >> and not:
> >> new Point(x,y)
> >> so you cannot delete it.
> >>
> >> Probably you can rewrite your addPoint as:
> >>
> >> void Char::addPoint(int x, int y) {
> >> Point * p = new Point(x,y);
> >> _pixelPoints.push_back(*p));
> >> delete p;
> >> }

> >
> > That works but why?? Needless use of new, inefficient and error prone.
> >
> > _pixelPoints.push_back(Point(x,y));

>
> Good point.


Ho ho!

>
> > What's so hard about the above that newbies fail to use it?

>
> The explanation (at least for me) is the fact that I'm used
> (since many years) to write in Java, where the only way to create
> a new Object is via the "new" operator.
> So (at least for me) the natural way is always
> new Point(x, y)
>


Yes that explains some of it. But I definitely get the impression that
non-Java programmers also make similar mistakes.

john


 
Reply With Quote
 
Jeff Flinn
Guest
Posts: n/a
 
      05-20-2004

"John Harrison" <> wrote in message
news:...
>
> "Dario (drinking co?ee in the o?ce.)" <> wrote in

message
> news:c8i7td$ddi$...
> > John Harrison wrote:
> >
> > > "Dario (drinking coï¬?ee in the oï¬fceâ?¦)" <>

wrote
[snip]
> >
> > > What's so hard about the above that newbies fail to use it?

> >
> > The explanation (at least for me) is the fact that I'm used
> > (since many years) to write in Java, where the only way to create
> > a new Object is via the "new" operator.
> > So (at least for me) the natural way is always
> > new Point(x, y)
> >

>
> Yes that explains some of it. But I definitely get the impression that
> non-Java programmers also make similar mistakes.


When I joined my current employer 2 years ago, our product was rife with
unnecessary new/delete usage. My predecessor along with most of the other
employees are long time C programmers. At least they did use new/delete
rather than malloc/free.

Jeff F


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      05-20-2004
John Harrison wrote:
>
> Seems to be happening a lot lately so I'm interested in what makes new and
> delete (and pointers generally) so attractive to newbies. Maybe you can
> explain.
>


Java pollution.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
Stefan Pantos
Guest
Posts: n/a
 
      05-20-2004
I'm not sure that you understand what happens when you add something to
a vector. The vector contructs a copy of the object you pass it and
stores this copy. This is why you do not need to delete what you have
added.

This might seem like it will be slow and well it could be. To get around
this you could store a vector of pointers vector<Point*> now it will
make a copy of your pointer not the object. Now you would have to delete
what you have added to your vector.

In java you could view all you objects as pointers and that is way you
need a new and also why you can assing them the value nil.

Stefan

In <> cppaddict wrote:
> Hi,
>
> I am deleting some objects created by new in my class destructor, and
> it is causing my application to error at runtime. The code below
> compiles ok, and also runs fine if I remove the body of the
> destructor. So I think I am somehow using delete incorrectly, but I'm
> not sure exaclty what I'm doing wrong.
>
> I'd apprecitate any clarification and suggestions for rewriting the
> below properly.
>
> Thanks,
> cpp
>
> PS: The Char class below is used to store a pixel representation of a
> character for display. A character in this context is simply a
> collection of points.
>
> #include <vector>
> #include "Point.h"
>
> class Char {
> private:
> char _ch;
> std::vector<Point> _pixelPoints;
> public:
> Char(char);
> ~Char();
> void addPoint(int, int);
> std::vector<Point>& const getPixelPoints();
> char getChar() {return _ch;}
>};
>
> Char::Char(char ch) :
> _ch(ch)
> { }
>
> Char::~Char() {
> std::vector<Point>::iterator iter;
> for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
> iter++)
> delete iter;
>}
>
> void Char::addPoint(int x, int y) {
> _pixelPoints.push_back(*( new Point(x,y) ));
>}
>
> std::vector<Point>& const Char::getPixelPoints() {
> return _pixelPoints;
>}
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To delete or not to delete? Lethal Computer Support 15 07-24-2005 11:47 PM
Question on delete [] vs just plain delete DamonChong C++ 11 01-31-2005 01:27 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM
Cannot Delete, (The Delete Key Won't Work) Lee Something Computer Support 13 10-15-2003 09:51 PM
delete on delete ! Sandeep Grover C++ 19 07-22-2003 10:09 AM



Advertisments