[comp.lang.modula2] Cardinal numbers

intbook@sask.UUCP (Dave Daoust) (05/19/88)

Is the following program legal?  What is displayed?


MODULE N;
FROM InOut IMPORT WriteCard, WriteLn;
PROCEDURE Out(S : ARRAY OF CHAR);
BEGIN
   WriteCard(HIGH(S), 0);
   WriteLn;
END Out;
BEGIN
   Out("");
END N.

tmb@davinci.acc.Virginia.EDU (Thomas M. Breeden) (05/21/88)

In article <1101@sask.UUCP> intbook@sask.UUCP (Dave Daoust) writes:
   >
   >Is the following program legal?  What is displayed?
   >
   >
   >MODULE N;
   >FROM InOut IMPORT WriteCard, WriteLn;
   >PROCEDURE Out(S : ARRAY OF CHAR);
   >BEGIN
   >   WriteCard(HIGH(S), 0);
   >   WriteLn;
   >END Out;
   >BEGIN
   >   Out("");
   >END N.

The original PDP11 Modula-2 compiler (which I am still using) always
places a 0C at the end of any literal string, including '""'. Thus the
above HIGH(S) value is printed out as (and is) 0. For the above, SIZE(S)
is 1.

I believe most or all other M2 compilers do the same thing, but don't
think that it is specified in PIM2, 3rd edition. Is this standard or
becoming standard?

Another related thing that I have wondered about is the situation with
respect to dynamic arrays and LONGCARD vs. CARDINAL indices. ie, how is
the TYPE of HIGH() maintained correctly when the index could be a LONG...?

The TDI compiler for the Amiga will react to the following :

          MODULE N;
          FROM InOut IMPORT WriteCard, WriteLn;
          VAR  c :CARDINAL;
               a :ARRAY[0..65536] OF CHAR;
          BEGIN
          WriteCard(HIGH(a), 0); WriteLn;
          END N;

with a diagnostic on compiling that HIGH(a) is the wrong type. It does
seem to type it as LONGCARD.

Unfortunately, the situation is not so good for the following :

          MODULE N;
          FROM InOut IMPORT WriteCard, WriteLn;
          VAR  c :CARDINAL;
               a :ARRAY[0..65536] OF CHAR;

             PROCEDURE P(s:ARRAY OF CHAR);
             BEGIN
             WriteCard(HIGH(s), 0); WriteLn;
             END;

          BEGIN
          P(a);
          END N;

This compiles without error but prints out out HIGH(s) as a CARDINAL "1"
(ie, the first word, 68000 style, of the 32 bit value).
(Their release notes do warn about this bug, however).

Do other compilers that support both CARDINAL and LONGCARD TYPES and that
allow either to be array indices handle this better?

Is this a settled issue in the standardization efforts?

            - Tom Breeden
              tmb@virginia.EDU      -> Internet
              tmb@virginia          -> BITNET


            - Tom Breeden
              tmb@virginia.EDU      -> Internet
              tmb@virginia          -> BITNET

alan@pdn.UUCP (Alan Lovejoy) (05/22/88)

In article <1101@sask.UUCP> intbook@sask.UUCP (Dave Daoust) writes:
/
/Is the following program legal?  What is displayed?
/
/
/MODULE N;
/FROM InOut IMPORT WriteCard, WriteLn;
/PROCEDURE Out(S : ARRAY OF CHAR);
/BEGIN
/   WriteCard(HIGH(S), 0);
/   WriteLn;
/END Out;
/BEGIN
/   Out("");
/END N.

This legal program outputs the digit 0 followed by a newLine.

It is impossible to have an array whose length is zero, since that
would require its ending index to be less than its starting index.
So the type of "" is ARRAY [0..0] OF CHAR, and its length is therefore
one character.  Normally, the compiler stores 0C in such a string.

Therefore, HIGH never needs to return -1 as the maximum index of an
array.  But open array parameters which are strings should always be
checked for the presence of 0C, starting with index 0.

Marking the end of strings with a special character is an irredeemably
brain-damaged idea.  The fact that C does it is no excuse.  

-- 
Alan Lovejoy; alan@pdn; 813-530-8241; Paradyne Corporation: Largo, Florida.
Disclaimer: Do not confuse my views with the official views of Paradyne
            Corporation (regardless of how confusing those views may be).
Motto: Never put off to run-time what you can do at compile-time!