comp.lang.ada
 help / color / mirror / Atom feed
* Table of pointers question
@ 2009-09-24  0:47 Rob Solomon
  2009-09-24  1:34 ` Adam Beneschan
                   ` (5 more replies)
  0 siblings, 6 replies; 19+ messages in thread
From: Rob Solomon @ 2009-09-24  0:47 UTC (permalink / raw)


I am working my way thru Ada As A Second Language by Norman Cohen (c)
1996

This confuses me.

It is a simple sorting routine that swaps pointers rather than the
data.  Note that the variables are more like Modula-2 syntax as I am
very comfortable with that.  And it is easier to type.

with Ada.Text_IO, Ada.Strings.Bounded; use Ada.Text_IO;
procedure PrintDirectory is
  maxLineLength : constant := 80;
  maxEntries    : constant := 1000;
  
  package InputLines is new Ada.Strings.Bounded.Generic_Bounded_Length
(MaxLineLength);
  
  use InputLines;
  
  type DirectoryEntryType is
    record
      NamePart, StreetPart, CityPart: Bounded_String;
    end record;
  
  type DirectoryEntryPointerType is access DirectoryEntryType;
  
  NewEntry        : DirectoryEntryPointerType;
  Buffer          : String (1..MaxLineLength);
  Length          : Integer range 0..MaxLineLength;
  OutOfPlaceEntry : DirectoryEntryPointerType;
  NumberOfEntries : Integer range 0..MaxEntries := 0;
  No_Exchanges    : Boolean;
  
  EntryList        : array (1..MaxEntries) of DirectoryEntryType;
  EntryPointerList : array (1..MaxEntries) of
DirectoryEntryPointerType;
  
begin
  -- read data into entry list
  WHILE NOT End_Of_File LOOP
    Get_Line(Buffer, length);
    NewEntry.NamePart := To_Bounded_String(Buffer(1..Length));
    Get_Line(Buffer, Length);
    NewEntry.StreetPart := To_Bounded_String(Buffer(1..Length));
    Get_Line(Buffer, Length);
    NewEntry.CityPart := To_Bounded_String(Buffer(1..Length));
    NumberOfEntries := NumberOfEntries + 1;
    EntryPointerList(NumberOfEntries) := new
DirectoryEntryType'(NewEntry);  -- MY QUESTION HERE
  END LOOP;
  
  -- sort entryList using bubble sort
  
  LOOP
    No_Exchanges := True;
    FOR I in 1 .. NumberOfEntries - 1 LOOP
      IF EntryPointerList(I).NamePart > EntryPointerList(I+1).NamePart
THEN
        OutOfPlaceEntry := EntryPointerList(I+1);
        EntryPointerList(I+1) := EntryPointerList(I);
        EntryPointerList(I) := OutOfPlaceEntry;
        No_Exchanges := False;
      END IF:
    END LOOP;
    EXIT WHEN No_Exchanges;
  END LOOP;
  
  -- write sorted data
  FOR I in 1 .. NumberOfEntries LOOP
    Put_Line(To_String(EntryPointerList(I).NamePart));
    Put_Line(To_String(EntryPointerList(I).StreetPart));
    Put_Line(To_String(EntryPointerList(I).CityPart));
  END LOOP;


My question is that I would expect to have an array that contains the
data, and a second array that is an array of pointers to the 1st
array.  The example does not define that.

How does the line:
EntryPointerList(NumberOfEntries) := new DirectoryEntryType'(NewEntry)
;

Do what's needed?

Thanks



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  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-24  2:00 ` (see below)
                   ` (4 subsequent siblings)
  5 siblings, 2 replies; 19+ messages in thread
From: Adam Beneschan @ 2009-09-24  1:34 UTC (permalink / raw)


On Sep 23, 5:47 pm, Rob Solomon <use...@drrob1-noreply.com> wrote:
> I am working my way thru Ada As A Second Language by Norman Cohen (c)
> 1996
>
> This confuses me.
>
> It is a simple sorting routine that swaps pointers rather than the
> data.  Note that the variables are more like Modula-2 syntax as I am
> very comfortable with that.  And it is easier to type.

ItMayBeEasierToTypeButItSureAsHellIsntEasierToReadAndIfYouCareAboutEaseOfTypingMoreThan
BeingNiceToTheReaderWhyNotJustNameAllYourVariablesWithSingleLetters???

This has always been a bugaboo of mine.  People whose native languages
use Latin or Cyrillic or other alphabets are used to seeing spaces
between words, and _ is unobtrusive enough look like a space to the
eye, but jamming everything together takes that space away from us and
makes things harder on the eyes.  I cannot for the life of me
understand why anyone would prefer that style.

OK, I'm done ranting.


> My question is that I would expect to have an array that contains the
> data, and a second array that is an array of pointers to the 1st
> array.  The example does not define that.
>
> How does the line:
> EntryPointerList(NumberOfEntries) := new DirectoryEntryType'(NewEntry)
> ;
>
> Do what's needed?

It finds some memory somewhere to store the DirectoryEntryType data.
You don't need to know where.  All you need to know is that
EntryPointerList(NumberOfEntries) will point to it, and you go through
that pointer to get to the data.

The example above does declare an array of DirectoryEntryType, i.e.
EntryList, but this array isn't used anywhere.  Did Cohen really
declare that variable, or did you add it yourself?

Whether to use an array of DirectoryEntryType or an array of pointers
is a design decision.  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.  Yeah, I know, memory
is cheap and nobody cares any more about memory usage.  But they
should, since I'm sick of applications that use virtual memory
wantonly and then use up all my computer's resources swapping like
crazy so that I can't run anything else.  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.

There may be other cases where you'd rather have an array of data than
an array of pointers.  But it would be very rare that you'd need
both.  If you have an array of pointers, you can index into that array
to get to the data you need, so there's no reason to set up an array
of data for you to index into.

                                 -- Adam



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  0:47 Table of pointers question Rob Solomon
  2009-09-24  1:34 ` Adam Beneschan
@ 2009-09-24  2:00 ` (see below)
  2009-09-24  3:46 ` Jeffrey R. Carter
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 19+ messages in thread
From: (see below) @ 2009-09-24  2:00 UTC (permalink / raw)


On 24/09/2009 01:47, in article utflb5p8871f6p2531cr81tkdteacm786a@4ax.com,
"Rob Solomon" <usenet@drrob1-noreply.com> wrote:

> Note that the variables are more like Modula-2 syntax as I am
> very comfortable with that.  And it is easier to type.

It's not easier to read, and that is all that matters.

-- 
Bill Findlay
<surname><forename> chez blueyonder.co.uk





^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  0:47 Table of pointers question Rob Solomon
  2009-09-24  1:34 ` Adam Beneschan
  2009-09-24  2:00 ` (see below)
@ 2009-09-24  3:46 ` Jeffrey R. Carter
  2009-09-24  6:59 ` Georg Bauhaus
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 19+ messages in thread
From: Jeffrey R. Carter @ 2009-09-24  3:46 UTC (permalink / raw)


Rob Solomon wrote:
> 
> It is a simple sorting routine that swaps pointers rather than the
> data.  Note that the variables are more like Modula-2 syntax as I am
> very comfortable with that.  And it is easier to type.

 From the ARM (Introduction/Design Goals): "The need for languages that promote 
reliability and simplify maintenance is well established. Hence emphasis was 
placed on program readability over ease of writing." This is one of the "three 
overriding concerns" in the design of Ada; if "easierToType" is more important 
to you than ease of reading, then Ada is probably not for you.

> My question is that I would expect to have an array that contains the
> data, and a second array that is an array of pointers to the 1st
> array.  The example does not define that.

Why would you expect that? When using pointers, the data is usually on the heap, 
as it is in your example.

Sometimes, in cases like this, one has an array of data and an array of indices 
into that array, and one sorts the array of indices rather than the array of 
data. An index is essentially a pointer into the array. Perhaps that is what you 
were thinking of.

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  1:34 ` Adam Beneschan
@ 2009-09-24  6:39   ` tmoran
  2009-09-25 16:52   ` björn lundin
  1 sibling, 0 replies; 19+ messages in thread
From: tmoran @ 2009-09-24  6:39 UTC (permalink / raw)


> The example above does declare an array of DirectoryEntryType, i.e.
> EntryList, but this array isn't used anywhere.  Did Cohen really
> declare that variable, or did you add it yourself?

  Page 341, in the chapter on uses of Access Types, shows a simple example
sorting the entries in array Entry_List.  On the next page, Cohen shows
"reducing the amount of data movement by using an array of pointers to
Directory_Entry_Type records in place of the array of Directory_Entry_Type
records:".  So the example on 341 has the Entry_List array and the
modified version drops that to use instead the Entry_Pointer_List array.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  0:47 Table of pointers question Rob Solomon
                   ` (2 preceding siblings ...)
  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-24  6:59 ` Stephen Leake
       [not found] ` <3cadnZif2YjGbyfXnZ2dnUVZ_tmdnZ2d@earthlink.com>
  5 siblings, 2 replies; 19+ messages in thread
From: Georg Bauhaus @ 2009-09-24  6:59 UTC (permalink / raw)


Rob Solomon wrote:
> I am working my way thru Ada As A Second Language by Norman Cohen (c)
> 1996
> 
> This confuses me.
> 
> It is a simple sorting routine that swaps pointers rather than the
> data.  Note that the variables are more like Modula-2 syntax as I am
> very comfortable with that.  And it is easier to type.

Has something gone wrong when (I'm guessing here) the
program was rewritten for Ada?  The compiler shows some
errors.  After correcting these formally, i.e. without thinking,
it confirms Adam's observation, and hints to others:

Compiling: printdirectory.adb (source file time stamp: 2009-09-24 06:42:50)

    25.   EntryList        : array (1..MaxEntries) of DirectoryEntryType;
          |
        >>> warning: variable "EntryList" is not referenced

    33.     NewEntry.NamePart := To_Bounded_String(Buffer(1..Length));
            |
        >>> warning: "NewEntry" may be null



Here, the "correction" was
1 -  END IF;  -- semicolon, not colon
...
2, 3 -    EntryPointerList(NumberOfEntries) := -- *new*
DirectoryEntry*Pointer*Type'(NewEntry);  -- MY QUESTION HERE

(NewEntry is of a pointer type already.)

Maybe the intent is that EntryList should privide storage
for new directory entries, as mentioned in other postings?
No "new" then, but putting pointers to the entries in EntryList
into EntryPointerList.  I guess that NewEntry should stand
for one of the components of EntryList during each loop.

Side note: A good source code editor will remove the burden
of having to press the shift key for capital letters:
whenever you press '_' or ' ' or ':' etc, the preceding
identifier will, by default, be adjusted to your casing
preferences.  They should be easy to type then.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  0:47 Table of pointers question Rob Solomon
                   ` (3 preceding siblings ...)
  2009-09-24  6:59 ` Georg Bauhaus
@ 2009-09-24  6:59 ` Stephen Leake
  2009-09-26 13:50   ` Rob Solomon
       [not found] ` <3cadnZif2YjGbyfXnZ2dnUVZ_tmdnZ2d@earthlink.com>
  5 siblings, 1 reply; 19+ messages in thread
From: Stephen Leake @ 2009-09-24  6:59 UTC (permalink / raw)


Rob Solomon <usenet@drrob1-noreply.com> writes:

>   EntryList        : array (1..MaxEntries) of DirectoryEntryType;
>   EntryPointerList : array (1..MaxEntries) of DirectoryEntryPointerType;

> My question is that I would expect to have an array that contains the
> data, 

That might appear to be EntryList. Except that EntryList is never used
in the code you quoted. I don't have the book handy, so I don't know
if it is used elsewhere.

> and a second array that is an array of pointers to the 1st array.

That's EntryPointerList. However, the pointers simply point to
allocated memory, not to elements of EntryList or any other data
structure. 

Why did you expect pointers into an array?

> The example does not define that.
>
> How does the line:
> EntryPointerList(NumberOfEntries) := new DirectoryEntryType'(NewEntry)
> ;
>
> Do what's needed?

This allocates memory for the data contained in NewEntry, and stores a
pointer to it in EntryPointerList.

The rest of the code then moves these pointers around in
EntryPointerList.

What other languages are you familiar with? Perhaps we could relate
this Ada code to one of them.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  6:59 ` Georg Bauhaus
@ 2009-09-24  7:06   ` Georg Bauhaus
  2009-09-24 14:55   ` Adam Beneschan
  1 sibling, 0 replies; 19+ messages in thread
From: Georg Bauhaus @ 2009-09-24  7:06 UTC (permalink / raw)


Georg Bauhaus wrote:
> Rob Solomon wrote:
>> I am working my way thru Ada As A Second Language by Norman Cohen (c)
>> 1996
>>
>> This confuses me.
>>
>> It is a simple sorting routine that swaps pointers rather than the
>> data.  Note that the variables are more like Modula-2 syntax as I am
>> very comfortable with that.  And it is easier to type.
> 
> Has something gone wrong when (I'm guessing here) the
> program was rewritten for Ada?  The compiler shows some
> errors. 

I'll take this one back after learning that the program
is a modified copy from section 8.5.3 of Cohen's book.

Anyway, to run a compiler can be helpful :-)



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
       [not found] ` <3cadnZif2YjGbyfXnZ2dnUVZ_tmdnZ2d@earthlink.com>
@ 2009-09-24 12:49   ` Robert A Duff
  2009-09-26 13:36   ` Rob Solomon
  1 sibling, 0 replies; 19+ messages in thread
From: Robert A Duff @ 2009-09-24 12:49 UTC (permalink / raw)


Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> 	This example appears (I haven't fully explored the code -- still
> recovering from a dilated eye exam so everything is fuzzy) ...

Maybe if the program had more underscores...  ;-)


...to be using
> dynamically allocated data records, and storing a true (memory) pointer
> to those records in the array. It then swaps the pointers. The data is
> somewhere in allocated memory.

- Bob



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  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
  1 sibling, 1 reply; 19+ messages in thread
From: Adam Beneschan @ 2009-09-24 14:55 UTC (permalink / raw)


On Sep 23, 11:59 pm, Georg Bauhaus <rm.tsoh.plus-
bug.bauh...@maps.futureapps.de> wrote:
> Rob Solomon wrote:
> > I am working my way thru Ada As A Second Language by Norman Cohen (c)
> > 1996
>
> > This confuses me.
>
> > It is a simple sorting routine that swaps pointers rather than the
> > data.  Note that the variables are more like Modula-2 syntax as I am
> > very comfortable with that.  And it is easier to type.
>
> Has something gone wrong when (I'm guessing here) the
> program was rewritten for Ada?  The compiler shows some
> errors.  After correcting these formally, i.e. without thinking,
> it confirms Adam's observation, and hints to others:
>
> Compiling: printdirectory.adb (source file time stamp: 2009-09-24 06:42:50)
>
>     25.   EntryList        : array (1..MaxEntries) of DirectoryEntryType;
>           |
>         >>> warning: variable "EntryList" is not referenced
>
>     33.     NewEntry.NamePart := To_Bounded_String(Buffer(1..Length));
>             |
>         >>> warning: "NewEntry" may be null
>
> Here, the "correction" was
> 1 -  END IF;  -- semicolon, not colon
> ...
> 2, 3 -    EntryPointerList(NumberOfEntries) := -- *new*
> DirectoryEntry*Pointer*Type'(NewEntry);  -- MY QUESTION HERE
>
> (NewEntry is of a pointer type already.)

That's the error.  NewEntry should not be a pointer type.  The intent
appears to be that a record of type DirectoryEntryType is built, and
then a copy of it allocated on the heap and the pointer stored in the
array.  (I'm assuming that's the case; I don't have Cohen's book
handy.)

So the declaration of NewEntry is wrong.  Perhaps that has to do with
a misunderstanding of the relationship between pointers and data; but
I think it's more likely just a typographical error, made harder to
spot by the lack of underscores.

                                     -- Adam



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  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
  1 sibling, 1 reply; 19+ messages in thread
From: björn lundin @ 2009-09-25 16:52 UTC (permalink / raw)


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

/Björn
Björn Lundin



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-25 16:52   ` björn lundin
@ 2009-09-25 17:12     ` Adam Beneschan
  0 siblings, 0 replies; 19+ messages in thread
From: Adam Beneschan @ 2009-09-25 17:12 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
       [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
                       ` (2 more replies)
  1 sibling, 3 replies; 19+ messages in thread
From: Rob Solomon @ 2009-09-26 13:36 UTC (permalink / raw)


>> My question is that I would expect to have an array that contains the
>> data, and a second array that is an array of pointers to the 1st
>> array.  The example does not define that.
>
>	That sounds like what I'd have used in BASIC or FORTRAN -- an array
>of INDICES (not [memory] pointers) used as subscripts to a static array
>of the data -- and the sort would be implemented by swapping the
>indices, not the physical data (IOWs, the indices start sorted 1..n, and
>end up out-of-order -- but when you then retrieve the data be using the
>indices from first to last, the data is in correct order).
>
You are describing how I expected the code to be written.  Many years
ago I wrote that very routine in Fortran, and I felt very satisfied
that it worked.

When  I did the same routine in Modula-2, I structured it the same
way, only used an array of pointer rather than indices.   AFAIK,
Modula-2 does not have the capability to place data in the heap and
return a pointer in one assignment.  

So when I came across this example that did not structure it as I
expected, I was confused.  It did not occur to me that Ada could do
what everyone here says it does.  

The syntax of the statement is also confusing to me.

EntryPointerList(NumberOfEntries) :=
            new DirectoryEntryType'(NewEntry);

It would help me to break this statement down more.  Why is the last
part written the way it is?  It uses a single quote and parens.  I
don't understand this syntax.  How does this store data on the heap
and then return a pointer to assign to the pointer list?



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24 14:55   ` Adam Beneschan
@ 2009-09-26 13:45     ` Rob Solomon
  0 siblings, 0 replies; 19+ messages in thread
From: Rob Solomon @ 2009-09-26 13:45 UTC (permalink / raw)


>> > I am working my way thru Ada As A Second Language by Norman Cohen (c)
>> > 1996
>>
>> > This confuses me.
>>
>> > It is a simple sorting routine that swaps pointers rather than the
>> > data. �Note that the variables are more like Modula-2 syntax as I am
>> > very comfortable with that. �And it is easier to type.
>>
>> Has something gone wrong when (I'm guessing here) the
>> program was rewritten for Ada? �The compiler shows some
>> errors. �After correcting these formally, i.e. without thinking,
>> it confirms Adam's observation, and hints to others:
>>
>> Compiling: printdirectory.adb (source file time stamp: 2009-09-24 06:42:50)
>>
>> � � 25. � EntryList � � � �: array (1..MaxEntries) of DirectoryEntryType;
>> � � � � � |
>> � � � � >>> warning: variable "EntryList" is not referenced
>>
>> � � 33. � � NewEntry.NamePart := To_Bounded_String(Buffer(1..Length));
>> � � � � � � |
>> � � � � >>> warning: "NewEntry" may be null
>>
>> Here, the "correction" was
>> 1 - �END IF; �-- semicolon, not colon
>> ...
>> 2, 3 - � �EntryPointerList(NumberOfEntries) := -- *new*
>> DirectoryEntry*Pointer*Type'(NewEntry); �-- MY QUESTION HERE
>>
>> (NewEntry is of a pointer type already.)
>
>That's the error.  NewEntry should not be a pointer type.  The intent
>appears to be that a record of type DirectoryEntryType is built, and
>then a copy of it allocated on the heap and the pointer stored in the
>array.  (I'm assuming that's the case; I don't have Cohen's book
>handy.)
>
>So the declaration of NewEntry is wrong.  Perhaps that has to do with
>a misunderstanding of the relationship between pointers and data; but
>I think it's more likely just a typographical error, made harder to
>spot by the lack of underscores.

Thanks Adam.  One of my problems here is that the example using
pointers is not declared in its entirety, but modified from a prev
example in the book.  I did not understand what had to be modified and
what remained.

And I haven't attempted to understand section 8.5.4 yet.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-24  6:59 ` Stephen Leake
@ 2009-09-26 13:50   ` Rob Solomon
  0 siblings, 0 replies; 19+ messages in thread
From: Rob Solomon @ 2009-09-26 13:50 UTC (permalink / raw)


>Rob Solomon <usenet@drrob1-noreply.com> writes:
>
>>   EntryList        : array (1..MaxEntries) of DirectoryEntryType;
>>   EntryPointerList : array (1..MaxEntries) of DirectoryEntryPointerType;
>
>> My question is that I would expect to have an array that contains the
>> data, 
>
>That might appear to be EntryList. Except that EntryList is never used
>in the code you quoted. I don't have the book handy, so I don't know
>if it is used elsewhere.
>
>> and a second array that is an array of pointers to the 1st array.
>
>That's EntryPointerList. However, the pointers simply point to
>allocated memory, not to elements of EntryList or any other data
>structure. 
>
>Why did you expect pointers into an array?
>
>> The example does not define that.
>>
>> How does the line:
>> EntryPointerList(NumberOfEntries) := new DirectoryEntryType'(NewEntry)
>> ;
>>
>> Do what's needed?
>
>This allocates memory for the data contained in NewEntry, and stores a
>pointer to it in EntryPointerList.
>
>The rest of the code then moves these pointers around in
>EntryPointerList.
>
>What other languages are you familiar with? Perhaps we could relate
>this Ada code to one of them.

I am very familiar with Wirth languages.  I have been using Modula-2
since the 80's.  

Some of my confusion can be due to the fact that I am merely a
recreational programmer that programs for his own use and amusement.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-26 13:36   ` Rob Solomon
@ 2009-09-26 14:51     ` John B. Matthews
       [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
  2009-09-27  5:53     ` Stephen Leake
  2 siblings, 0 replies; 19+ messages in thread
From: John B. Matthews @ 2009-09-26 14:51 UTC (permalink / raw)


In article <cj5sb5p5g3jvj6vvdgi56t9m25b6vb93da@4ax.com>,
 Rob Solomon <usenet@drrob1-noreply.com> wrote:
[...]
> The syntax of the statement is also confusing to me.
> 
> EntryPointerList(NumberOfEntries) :=
>             new DirectoryEntryType'(NewEntry);
> 
> It would help me to break this statement down more.  Why is the last 
> part written the way it is?  It uses a single quote and parens.  I 
> don't understand this syntax.  How does this store data on the heap 
> and then return a pointer to assign to the pointer list?

In this context, the allocator [1] includes a qualified expression [2]. 
When the expression is evaluated, "An object of the designated type is 
created and the value of the qualified_expression is converted to the 
designated subtype and assigned to the object." The resulting access 
value, which points to the newly created object, is stored in the 
EntryPointerList array at index NumberOfEntries.

Ada also uses "'" to indicate attributes [3].

[1]<http://www.adaic.com/standards/05rm/html/RM-4-8.html>
[2]<http://www.adaic.com/standards/05rm/html/RM-4-7.html>
[3]<http://www.adaic.com/standards/05rm/html/RM-K.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
       [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
@ 2009-09-26 18:58       ` Rob Solomon
  2009-09-26 21:00       ` Georg Bauhaus
  1 sibling, 0 replies; 19+ messages in thread
From: Rob Solomon @ 2009-09-26 18:58 UTC (permalink / raw)


On Sat, 26 Sep 2009 11:51:48 -0700, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:

>On Sat, 26 Sep 2009 09:36:26 -0400, Rob Solomon
><usenet@drrob1-noreply.com> declaimed the following in comp.lang.ada:
>
>
>> When  I did the same routine in Modula-2, I structured it the same
>> way, only used an array of pointer rather than indices.   AFAIK,
>> Modula-2 does not have the capability to place data in the heap and
>> return a pointer in one assignment.  
>>
>	Based on the example at http://www.modula2.org/tutor/chapter11.php,
>no... They use ALLOCATE() to create one structure on the heap,
>specifying the size (apparently Modula-2 either doesn't have the
>intelligence to infer the data size, of they also use it to create
>dynamic arrays of smaller components); followed by assigning values to
>the allocated memory.
>
>	In Ada, "new" is the keyword that allocates memory... There is no
>equivalent "free" as, in the realm Ada was originally spec'd for,
>garbage collection operations would have been detrimental. There is a
>generic "unchecked_deallocation" procedure -- as the name should
>indicate, usage is really up to the programmer to ensure they aren't
>leaving dangling references, etc.
>
>	Note that, as with Pascal, Modula-2 requires the user to explicitly
>code pointer dereference operations. Ada rarely needs such as one can
>not perform arithmetic on access types.

I could have been clearer as to my meaning.  ALLOCATE creates a
pointer to an empty object.  A separate assignment statement is
required to add the data.  That is, Modula-2 needs 2 statements to
achieve what Ada is allowing in that one statement.

This contraction confused me.  

If I come across any embedded assignment statements in this Ada book,
like is allowed in C, I'll give up.



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
       [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
  2009-09-26 18:58       ` Rob Solomon
@ 2009-09-26 21:00       ` Georg Bauhaus
  1 sibling, 0 replies; 19+ messages in thread
From: Georg Bauhaus @ 2009-09-26 21:00 UTC (permalink / raw)


Dennis Lee Bieber wrote:

> 	Note that, as with Pascal, Modula-2 requires the user to explicitly
> code pointer dereference operations. Ada rarely needs such as one can
> not perform arithmetic on access types.

Still, Ada programmers can do any pointer arithmetic they want,
using System.Storage_Elements (cf. 13.7.1).



^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Table of pointers question
  2009-09-26 13:36   ` Rob Solomon
  2009-09-26 14:51     ` John B. Matthews
       [not found]     ` <3YSdnY7SXPNd_yPXnZ2dnUVZ_sydnZ2d@earthlink.com>
@ 2009-09-27  5:53     ` Stephen Leake
  2 siblings, 0 replies; 19+ messages in thread
From: Stephen Leake @ 2009-09-27  5:53 UTC (permalink / raw)


Rob Solomon <usenet@drrob1-noreply.com> writes:

>>> My question is that I would expect to have an array that contains the
>>> data, and a second array that is an array of pointers to the 1st
>>> array.  The example does not define that.
>>
>>	That sounds like what I'd have used in BASIC or FORTRAN -- an array
>>of INDICES (not [memory] pointers) used as subscripts to a static array
>>of the data -- and the sort would be implemented by swapping the
>>indices, not the physical data (IOWs, the indices start sorted 1..n, and
>>end up out-of-order -- but when you then retrieve the data be using the
>>indices from first to last, the data is in correct order).
>>
> You are describing how I expected the code to be written.  Many years
> ago I wrote that very routine in Fortran, and I felt very satisfied
> that it worked.

Note that you can implement that same approach in Ada. 

You should do that, as an excersize in learning Ada.

The tradeoffs between that approach and the "pointer" approach are
subtle, and not very meaningful for this simple example.

-- 
-- Stephe



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2009-09-27  5:53 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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