Re: simple delete question

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/20/04


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



Relevant Pages

  • Re: How hard is socket programming?
    ... I have used 4000 MB of space under windows many times. ... Then you are using Win64. ... I don't want the conversion cost and the cost of 64-bit ... pointers right now. ...
    (microsoft.public.vc.mfc)
  • Re: Create EXE files
    ... >> supply them to clients without requiring that they have Access on the ... Could someone give me a few pointers, ... >> an InstallShield process for easy installation by my clients? ... >> each organisation saving them the cost of buying Access. ...
    (microsoft.public.access.developers.toolkitode)
  • Re: Function pointers: performance penalty?
    ... This comes down to the cost of a direct call vs. an indirect call. ... an indirect call requires that the CPU ... In the context of function pointers, if you use the same one over and ... Embedded CPUs may not have a branch target predictor at all because it ...
    (comp.lang.c)
  • Re: simple delete question
    ... >delete (and pointers generally) so attractive to newbies. ... small one) was more than the cost of dynamic allocation -- indeed, ... was not aware that dynamic allocation had a high cost at all. ... well as avoiding any overhead involved in copying an object (since I ...
    (comp.lang.cpp)
  • Re: Deleting objects obtained from a STL library
    ... >> besides using pointers rather than references for the STL library? ... then it may make sense to avoid copying it. ... dynamic allocation unless it is called for, ...
    (comp.lang.cpp)