From 2446057992158347986
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,be4e234b6733532
X-Google-Attributes: gidf78e5,public
From: Daniel Barker <sokal@holyrood.ed.ac.uk>
Subject: Re: why are pointers necessary?
Date: 1997/08/04
Message-ID: <Pine.GSO.3.95.970804051733.6152B-100000@holyrood.ed.ac.uk>#1/1
X-Deja-AN: 262433210
References: <captarm-2007971209450001@169.197.21.243> <33D43DA8.73D7@ix.netcom.crud.com> <5r3g4e$t5t$1@news.wco.com>
X-Original-Date: Mon, 4 Aug 1997 05:44:47 +0100
Organization: Edinburgh University
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBM+Y310y4NqrwXLNJAQFjdAIAr/qpAC7MILopKQZaGaUkQl87y3kSXxg+ voFYX3IaWqgqCVWXea8GUtQ4kFzyWURDlRMaaBDUMOGU6nFgkzU8qw== =5iSw
Newsgroups: comp.std.c++
Originator: austern@isolde.mti.sgi.com


On 23 Jul 1997, Dave Schreiber wrote:

> In article <33D43DA8.73D7@ix.netcom.crud.com>,
> Paul D. DeRocco <pderocco@ix.netcom.crud.com> wrote:
> [...]
> >	while (*dest++ = *src++);
> >
> >which copies a null-terminated string. To do this without pointers would
> >involve array indexing, and a separate local variable:
> >
> >	int i;
> >	while (dest[i] = src[i]) i++;
> >
> >The former method spares the local variable, but provides no protection
> >against scribbling in memory given bad arguments. The latter provides a
> >place where bounds checking could be done, assuming the arrays are smart
> >enough, but is less efficient.
> 
> The efficiency depends on the implementation. For example, gcc on my Sun

The best way to achieve efficient loops with arrays on a 'typical' modern
RISC computer is to use for loops (with no conditional statements in the
loop) where reasonably possible, and leave the rest to the compiler. The
opportunities for efficient pipelining and, on multiprocessor
architectures, automatic parallelization are greater. In this example,
keep a record of the length of strings, then do

for (i = 0; i < len; ++i)
	dest[i] = src[i];

Since there is no data dependency within the loop, all the assignments
(len of them) can be done at once. The while versions gnarl things up,
because the upper limit of i is unknown beforehand.

On the data structures side, I prefer to implement trees using array
indices rather than pointers to show connections. The fact that the array
(tree) as we know it is really a pointer to an array, since it has been
obtained dynamically, can be ignored for most of the program.  Each
element of the array might be

struct node
{
	int left;	/* array index of left child */
	int right;	/* array index of right child */
}

The biggest advantage is the temptation to allocate for each branch
separately is not there, so the whole tree is in contiguous memory for
efficient caching. 

I've written an entirely non-commercial C program which makes almost
minimal use of pointers and other cryptic features of C. Basically I
wanted to emulate Fortran 90, without having to learn all the new syntax.
If anyone is interest the program source can be got from

http://www.icmb.ed.ac.uk/sokal.html

If there were dynamic true arrays and a few changes to the standard
library, we could all forget about explicit pointer operations forever.


Daniel Barker,
Institute of Cell and Molecular Biology,
University of Edinburgh,
Daniel Rutherford Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



