The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: scj@yaccman.com (Steve Johnson)
Subject: [TUHS] Array index history
Date: Fri, 09 Jun 2017 14:21:57 -0700	[thread overview]
Message-ID: <e067d88d677a358404d7b561256e6e49c845c81f@webmail.yaccman.com> (raw)
In-Reply-To: <09918D02-9DA7-46B8-873E-A60489570400@kdbarto.org>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3875 bytes --]

I'd like to comment on the technical difficulty of array bounds
checking.   First of all, there are two kinds.   The simple kind
computes the index and simply checks that the address is within the
array.  This can be done fairly easily -- create a pointer to the end
of the array, and test against it (and with a bit more work,
"pre-test" loops so you don't need to test each element).  If you
ignore possible negative subscripts, it's even easier.  And it
catches a lot of the ugliest bugs.  The harder version checks the
range of each index in a multidimensional array separately.  This is
quite a bit more work, although it too can be optimized significantly
with enough work.  And if C didn't conflate arrays and pointers, I
suspect this would have been added to C long ago.

But C does conflate arrays and pointers.   When an array is passed
to a function, all that is passed is the location.  There is no clue
what the size is.  To even pass in the "end of array" pointer gets
expensive, since more registers need to be saved and loaded on each
call.  Correct negative subscripts are not unheard of when passing
part of an array into a functions.  And utilities like malloc get
more complicated as well, and for some of malloc's other uses (e.g.,
in class object creation) the checks would be inappropriate.  And
finally, because pointers are extensively used in data structures,
pointer conventions are often used when using arrays (e.g., passing in
NULL when an array argument is not present or needed).

There is yet another problem.  If array size or shape information
were somehow passed with arrays, it would be unkind for the language
to give the programmer no way to find out what the size(s) are.  This
desire ultimately leads to a rich calling convention like that of
MATLAB where you can have multiple function outputs, determine how
many inputs are present, and inputs will disclose their size and shape
on request.  Good to program in, but function calls are quite a bit
more expensive.

Realizing that most system code at the time of C's invention was still
written in assembler and PDP-11's were almost too small in memory to
do anything interesting (my laptop currently has roughly 243,000 times
the memory space of a PDP-11!), C's conventions provided a big
improvement in code correctness with a small cost when compared to
assembler... 

Steve

----- Original Message -----
From: david@kdbarto.org
To:<tuhs at minnie.tuhs.org>
Cc:
Sent:Thu, 8 Jun 2017 11:20:24 -0700
Subject:Re: [TUHS] Array index history

 Arnold gets it right on the Pascal indexing.

 In UCSD Pascal you could specify any array bounds you would like and
 the compiler would 0 base them for you by always doing a subtraction,
 or addition if your min was negative, of your min array index. So a
little
 run time cost for non-zero based arrays.

 I’m not sure how other Pascal compilers did this.

 I find it interesting that there are now a slew of testing programs
 (Valgrind, Address Sanitizer, Purify, etc) that will add the
‘missing’
 array bounds checking for C.

 David

 > On Jun 7, 2017, at 10:01 AM, tuhs-request at minnie.tuhs.org wrote:
 > 
 > Date: Wed, 07 Jun 2017 07:20:43 -0600
 > From: arnold at skeeve.com
 > To: tuhs at tuhs.org, ag4ve.us at gmail.com
 > Subject: Re: [TUHS] Array index history
 > Message-ID: <201706071320.v57DKhmJ026303 at freefriends.org>
 > Content-Type: text/plain; charset=us-ascii
 > 
 > Pascal (IIRC) allowed you to specify upper and lower bounds,
something
 > like
 > 
 > foo : array[5..10] of integer;
 > 
 > with runtime bounds checking on array accesses. (I could be wrong
---
 > it's been a LLLLOOONNNGGG time.)
 > 
 > HTH,
 > 
 > Arnold


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20170609/2cd98147/attachment.html>


  parent reply	other threads:[~2017-06-09 21:21 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.882.1496854900.3779.tuhs@minnie.tuhs.org>
2017-06-08 18:20 ` David
2017-06-08 18:55   ` Ron Natalie
2017-06-09 21:21   ` Steve Johnson [this message]
2017-06-09 21:52     ` Arthur Krewat
2017-06-08 22:29 ` Johnny Billquist
2017-06-08 23:16   ` Ron Natalie
2017-06-08 23:41     ` Steve Nickolas
2017-06-09 15:12       ` Random832
2017-06-09 16:30         ` Ron Natalie
2017-06-09  0:21     ` Johnny Billquist
2017-06-09  0:25     ` Pete Turnbull
2017-06-09  1:19       ` Toby Thain
2017-06-09  2:14         ` Pete Turnbull
     [not found] <mailman.890.1496953026.3779.tuhs@minnie.tuhs.org>
2017-06-08 22:28 ` Johnny Billquist
2017-06-08  9:16 Richard Tobin
  -- strict thread matches above, loose matches on Subject: below --
2017-06-08  2:27 Doug McIlroy
2017-06-08 12:49 ` William Cheswick
2017-06-08 12:56   ` Michael Kjörling
2017-06-08 13:45     ` Dan Cross
2017-06-08 13:57       ` Ron Natalie
2017-06-10 21:08     ` Nemo
2017-06-08 15:04   ` Dave Horsfall
2017-06-08 15:09     ` William Pechter
2017-06-08 16:53     ` Steve Nickolas
2017-06-08 20:17     ` Larry McVoy
2017-06-09  0:03     ` Greg 'groggy' Lehey
     [not found] <CAH_OBiewwwS6kbdWKUO-j3v=Do+Y9AzBn4ZkyVic7LOJN5WX7w@mail.gmail.com>
     [not found] ` <CAH_OBid5q-YeB6UYeOfjW68s3nb3TX_icC5dFMapfo=7_q5DUQ@mail.gmail.com>
     [not found]   ` <CAH_OBideJeNGiiys=VOEYcU1cffSg+6zDOD39HOU74vumQF0kA@mail.gmail.com>
     [not found]     ` <CAH_OBie_+dQXOC2OaZii5K=j1Ljmv-OvAVXvyypoLJ10jaysWA@mail.gmail.com>
     [not found]       ` <CAH_OBidXd1Lpgp8ttM3sWLfXDT0ms5EGAzEjPH9QkY+bXyev2w@mail.gmail.com>
     [not found]         ` <CAH_OBifge2RrwcDvSV46gwLAc0wHVtH++gdMBhXi-x8C0a_2jw@mail.gmail.com>
     [not found]           ` <CAH_OBic3MTy-20C_T2ciT2x+EjNZvXR2PJ0OzB0qRp1gd2OL-g@mail.gmail.com>
2017-06-07 12:56             ` shawn wilson
2017-06-07 13:16               ` Ron Natalie
2017-06-07 13:20               ` arnold
2017-06-07 13:50                 ` Michael Kjörling
2017-06-07 14:02                   ` arnold
2017-06-08 11:28                     ` Christian Neukirchen
2017-06-08 14:56                       ` shawn wilson
2017-06-07 14:58                   ` Tony Finch
2017-06-07 23:57                     ` Dave Horsfall
2017-06-07 16:31                 ` Lyndon Nerenberg
2017-06-08  0:47                 ` Bakul Shah
2017-06-07 17:59               ` Tim Bradshaw
2017-06-07 18:29               ` Clem Cole
2017-06-07 18:46                 ` Arthur Krewat
2017-06-07 19:03                   ` Ron Natalie
2017-06-07 19:15                     ` Arthur Krewat
2017-06-07 19:49                       ` Ron Natalie
2017-06-08  1:58                       ` ARJANEN Loïc Jean David
2017-06-08 13:49                       ` Random832
2017-06-08 13:55                         ` Arthur Krewat
2017-06-08 18:41                       ` Tim Bradshaw
2017-06-08  2:45                     ` Robert Swierczek
2017-06-08  4:05                       ` Dave Horsfall
2017-06-08  6:19                       ` Peter Jeremy
2017-06-08  7:02                         ` Dave Horsfall
2017-06-08 13:53                         ` Ron Natalie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e067d88d677a358404d7b561256e6e49c845c81f@webmail.yaccman.com \
    --to=scj@yaccman.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).