Re: simple delete question
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/20/04
- Next message: Derek: "Re: design - class to represent a variable"
- Previous message: Paul Schmidt: "Re: Which is easier to learn - .NET or J2EE?"
- In reply to: cppaddict: "Re: simple delete question"
- Next in thread: cppaddict: "Re: simple delete question"
- Reply: cppaddict: "Re: simple delete question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 20 May 2004 18:56:06 +0100
"cppaddict" <hello@hello.com> wrote in message
news:khnpa0pv4gkcm9jg8irqtgr1ci2evh1k64@4ax.com...
> >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.
>
> Thank you all for the clarification.
>
> John,
>
> To answer your question, my erroneous thought process was simply this:
> Since the number of Points in a Char object varies, that means the
> memory must be dynamically allocated at runtime, and therefore that
> means I will have to use the new operator. Or, to break it down:
>
> Don't know how many Points are in a Char //step 1 in my reasoning
> implies==>dynamic allocation //step 2
> imples==> use new operator //step 3
>
> Correct me if I'm wrong, but looking back at the code now it seems to
> me that my logical error was in step 2.
I think the conceptual error is that you are confusing the container with
the contained objects. It is true that the container (i.e. the vector) has
to use new (or something like it) in order hold a varying number of Points.
But that use of new is in the vector code which has already been written
for you, it not necessary to use it in your code.
Consider a vector of integers, would you have tried to allocate each integer
using new, like this?
vector<int> x;
x.push_back(*(new int(5));
Or would you have just written
vector<int> x;
x.push_back(5);
>
> There was an additional consideration too, which someone else
> mentioned, and which you can't see from the posted code, which I
> simplified in order to give focus to my question. That additional
> consideraton was my belief that the cost of copying an object (even a
> small one) was more than the cost of dynamic allocation -- indeed, I
> was not aware that dynamic allocation had a high cost at all.
No, definitely not, the cost of allocating large numbers of small objects is
going to greatly exceed the cost of copying those objects.
>
> Originally, the signature of addPoint() looked like this:
>
> void addPoint(Point& const);
>
> Thus I believed that I was efficiently creating a new Point object, as
> well as avoiding any overhead involved in copying an object (since I
> was passing by reference). Please let me know if there are additional
> logical erros in THAT statement.
Right, so you were coding like this?
Char c;
c.addPoint(*(new Point(x, y)));
You are right that in general using a const reference is a good idea to
avoid unecessary copies. But again the same change should be made
Char c;
c.addPoint(Point(x, y));
>
> Finally, you mention that even if the Point object was large, it would
> be better to use smart pointers rather than to store a vector of
> pointers which you delete yourself. Why is this so? Wouldn't the
> cost of those things be the same?
>
Similar, in fact the smart pointer will be fractionally more expensive. The
point of smart pointers is that they are much less error prone. Because a
smart pointer takes care of the burden of deleteing an object for you its
impossible to forget to delete a pointer (thus causing a memory leak) or
delete a pointer twice (probably crashing your program).
john
- Next message: Derek: "Re: design - class to represent a variable"
- Previous message: Paul Schmidt: "Re: Which is easier to learn - .NET or J2EE?"
- In reply to: cppaddict: "Re: simple delete question"
- Next in thread: cppaddict: "Re: simple delete question"
- Reply: cppaddict: "Re: simple delete question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|