comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Table of pointers question
Date: Fri, 25 Sep 2009 10:12:42 -0700 (PDT)
Date: 2009-09-25T10:12:42-07:00	[thread overview]
Message-ID: <71e6fb97-e9bb-410a-bc3a-442e9248a4e0@w37g2000prg.googlegroups.com> (raw)
In-Reply-To: e1061988-2243-40e0-a1c5-64f297131fad@e18g2000vbe.googlegroups.com

On Sep 25, 9:52 am, björn lundin <b.f.lun...@gmail.com> wrote:
> On 24 Sep, 03:34, Adam Beneschan <a...@irvine.com> wrote:
>
> > The advantages of using an array of pointers is
> > that a pointer is smaller than a DirectoryEntryType (and in a real-
> > life application, it could be MUCH smaller), so if NumberOfEntries is,
> > say, 15, allocating a fixed-size array of 1000 pointers and waiting
> > until runtime to allocate 15 DirectoryEntryTypes takes a lot less
> > memory than allocating 1000 DirectoryEntryTypes.
> .....
> > Also, when you do the
> > exchange, you're moving two pointers around rather than moving two
> > DirectoryEntryTypes, which is a lot faster since the pointers are
> > smaller.
>
> Is this true? Should one not expect the compiler to figure that out,
> so what is shuffled
> around, and memory allocated for, is for pointers, and not their
> content?
>
> Or do I have too much faith in compilers

Either you have way, way, way too much faith in compilers, or else
we're not on the same page.  If I have an array

   type Data_Record is
       ... something with a few hundred bytes in it
   end record;
   type Data_Array is array (natural range <>) of Data_Record;
   Arr : Data_Array (1 .. 1000);

and I write an exchange sort that includes this code to do the
exchange:

   Temp : Data_Record;
   ...
   Temp := Arr (I);
   Arr (I) := Arr (J);
   Arr (J) := Temp;

it will move several hundred bytes around, three times.  If you're
suggesting that the compiler should generate code to avoid this by
exchanging pointers instead---well, then you're basically asking the
compiler to figure out what algorithm the programmer is writing and
replace it with something it thinks is a better algorithm.  Which is
not only impossible in practice, it would be incredibly rude even if
it were possible.  Compilers can be expected to do some optimization,
but not to rewrite the program for you.

If your frame of reference is a language (like Lisp) where everything
is implemented as a pointer, then I could see how your comment might
make sense.  But Ada is not that kind of language.  Ada will give you
pointers if you tell it you want pointers, but (generally) not if you
don't.

                                -- Adam





  reply	other threads:[~2009-09-25 17:12 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-24  0:47 Table of pointers question Rob Solomon
2009-09-24  1:34 ` Adam Beneschan
2009-09-24  6:39   ` tmoran
2009-09-25 16:52   ` björn lundin
2009-09-25 17:12     ` Adam Beneschan [this message]
2009-09-24  2:00 ` (see below)
2009-09-24  3:46 ` Jeffrey R. Carter
2009-09-24  6:59 ` Georg Bauhaus
2009-09-24  7:06   ` Georg Bauhaus
2009-09-24 14:55   ` Adam Beneschan
2009-09-26 13:45     ` Rob Solomon
2009-09-24  6:59 ` Stephen Leake
2009-09-26 13:50   ` Rob Solomon
     [not found] ` <3cadnZif2YjGbyfXnZ2dnUVZ_tmdnZ2d@earthlink.com>
2009-09-24 12:49   ` Robert A Duff
2009-09-26 13:36   ` Rob Solomon
2009-09-26 14:51     ` John B. Matthews
     [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
2009-09-26 18:58       ` Rob Solomon
2009-09-26 21:00       ` Georg Bauhaus
2009-09-27  5:53     ` Stephen Leake
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox