Board index » delphi » Array subscript declarations.

Array subscript declarations.

Hello Folks,

What precisely is the meaning of this type declaration:

TSomething: array [0..0] of TThing;

I think I saw it somewhere in Delphi 3 help but now I can't find it. I
think I know, but I would like to see the proper deffinition in the
help file.

Help would be appreciated,
Bill Boulton

 

Re:Array subscript declarations.


On Sat, 04 Mar 2000 01:26:20 GMT, billboul...@one.nospam.net.au (Bill

Quote
Boulton) wrote:
>Hello Folks,

>What precisely is the meaning of this type declaration:

>TSomething: array [0..0] of TThing;

>I think I saw it somewhere in Delphi 3 help but now I can't find it. I
>think I know, but I would like to see the proper deffinition in the
>help file.

That means that TSomething is defined to be an array with only one
element in it, element 0.  This is commonly used when the array is
dynamically allocated:  you don't know how many elements there will be
at run time (you used GetMem to get some memory depending on run-time
conditions), so you just declare one element.

In my opinion this usage is bad style.  It forces you to turn run-time
array bounds checking off, because you'll often refer to elements
other than element 0.  Run-time bounds checking is a very useful
debugging aid, and you should use it when you can. As far as I know
there isn't any way to do proper run-time error checks on dynamically
allocated memory, but at least you shouldn't throw away a tool that's
useful in other situations.

A better declaration for a dynamically allocated array is

 TSomething: array [0..SomeBigNumber] of TThing;

SomeBigNumber can be really huge, e.g. Maxint div sizeof (TThing).
You'll never actually allocate one of these things at its full size,
so it's not a waste of memory.

Duncan Murdoch

Re:Array subscript declarations.


JRS:  In article <38c05f6a.5146...@news.one.net.au> of Sat, 4 Mar 2000
01:26:20 in news:comp.lang.pascal.delphi.misc, Bill Boulton <billboulton

Quote
@one.nospam.net.au> wrote:

>What precisely is the meaning of this type declaration:

>TSomething: array [0..0] of TThing;

>I think I saw it somewhere in Delphi 3 help but now I can't find it. I
>think I know, but I would like to see the proper deffinition in the
>help file.

You don't, really <g>!  

It's a typedef of an array of a single element, with ":" in place of
"=".

It's generally used for working with an array of indeterminate size,
with the relevant runtime check OFF.

Instead, one should use TSomething= array [0..Lots] of TThing; where
Lots is either more than the application can need or the size of the
largest possible structure DIV the size of TThing.  Then range-checking
can be ON.

It should not be there, IMHO.

My pas-extn.htm#VLarrs refers, though originally written for TP/BP.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Array subscript declarations.


JRS:  In article <38c13df8.623...@newshost.uwo.ca> of Sat, 4 Mar 2000
16:52:34 in news:comp.lang.pascal.delphi.misc, Duncan Murdoch

Quote
<dmurd...@pair.com> wrote:
>On Sat, 04 Mar 2000 01:26:20 GMT, billboul...@one.nospam.net.au (Bill
>Boulton) wrote:
>>TSomething: array [0..0] of TThing;
>In my opinion this usage is bad style.  It forces you to turn run-time
>array bounds checking off, because you'll often refer to elements
>other than element 0.  Run-time bounds checking is a very useful
>debugging aid, and you should use it when you can. As far as I know
>there isn't any way to do proper run-time error checks on dynamically
>allocated memory, but at least you shouldn't throw away a tool that's
>useful in other situations.

Fully agreed, of course - but it can be useful to think about doing non-
proper run-time error checks.

In longcalc.pas, I have a type Arr (imax is defined to be suitably
large) :
        Figures = array [1..imax] of byte ;
        Arr = packed record Magic : word ;
          Bytes : Tbytes ; Sg : SgT ; Sz : idx ; Ar : Figures end ;

All Arr are on the Heap, constructed to size with GetMem.

Now (rapidly changing mind about exactly what to say) I do check Magic
to see whether an PArr is valid (it's always set to a const $4762 on
creation, and checked and flipped before destruction, and can be tested;
so double-destruction, at least, should generally be detected).

If I had had the wit, when I needed it, to add a similar marker byte
after the intended length of Arr, that byte could have been checked when
appropriate for still having the right value.  This would not catch wild
mis-access, but it would catch the (more likely here) case of running
off the end.  Other examples might be invented.

In this case, access to the arrays Arr is time-significant; the program,
on non-trivial data, spends all of its time working through the elements
of Arrs.  If that were not the case, if Arrs contained data which were
rarely accessed, then one might use a procedure or function to access
them, and include the checks.  Perhaps it was something like that which
led to the concept of properties.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
 PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
 Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:Array subscript declarations.


On Sat, 4 Mar 2000 20:40:23 +0000, Dr John Stockton

Quote
<j...@merlyn.demon.co.uk> wrote:
>JRS:  In article <38c13df8.623...@newshost.uwo.ca> of Sat, 4 Mar 2000
>16:52:34 in news:comp.lang.pascal.delphi.misc, Duncan Murdoch
><dmurd...@pair.com> wrote:
>>On Sat, 04 Mar 2000 01:26:20 GMT, billboul...@one.nospam.net.au (Bill
>>Boulton) wrote:

[snip]

Thanks Duncan & John. That's what I thought it was supposed to
represent.

Bill Boulton.

Other Threads