Topic: why are pointers necessary?


Author: Daniel Barker <sokal@holyrood.ed.ac.uk>
Date: 1997/08/04
Raw View
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
]





Author: Tom Payne <thp@cs.ucr.edu>
Date: 1997/08/04
Raw View
In comp.std.c++ Walt Howard <walth@netcom.com> wrote:
: On 23 Jul 97 09:27:16 GMT, James Kanze
: <james-albert.kanze@vx.cit.alcatel.fr> wrote:

: >captarm@azstarnet.COM (Howard Salmon) writes:
: >
: > |>  I'm teaching myself C++ and I'm struggling with understanding why creating
: > |>  pointers are necessary.  It's possible to create programs without
: > |>  pointers...so what's the advantage of pointers?  The best I could come up
: > |>  with was that data is "two-dimensional", i.e., there's the data itself,
: > |>  and then the location of the data.  Each of these things needs to be
: > |>  stored in separate places in memory.  Does the OS move the location of
: > |>  data around, hence the need for pointers?  Or do we ever have a need for
: > |>  moving the data location around?  I feel like I'm just on the verge of
: > |>  understanding why we NEED pointers.
: >
: >Get a book about algorithms, and look up linked lists, trees, graphs,
: >etc.

:  Here's a good analogy. The world wide web. An URL is a
: pointer. The actual web page is the real data. Do you save
: locally, every web page you access? Or do you just save its URL?
: Think about the reasons URLs are good. Pointers have just about
: all the same good reasons.

  Good analogy!

  Another way to look at it: pointers are really (abstractions of)
addresses, which are indices into the program's address space, which
is a very large array.  In languages, like Fortran and Basic, that
lack pointers, one uses explicit arrays and indices.  In Perl:

  Associative arrays can be used to map any set of strings to any
  other set of strings(or numbers, or Boolean values).  Thus, they
  tend to fill the role that pointers fill in other languages.
                                          [Programming Perl, p.30]

  The other way to get by without "pointers" is to automagically
dereference them and refer to them as "references," as did Algol68.

Tom Payne
---
[ 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
]





Author: Tim Ottinger <tottinge@oma.com>
Date: 1997/07/29
Raw View
> |> Get a book about algorithms, and look up linked lists, trees,
> graphs,
> |> etc.
>
> All of which are possible without literal pointers.  Java gets by just
>
> fine without pointers.  Of course in most cases the construct you use
> instead of pointers (i.e., references) really are pointers "under
> the covers" but the compiler worries about the details instead of you.

Well, that's not really true. Everything in Java is a pointer. What java
gets by just fine without is pointer *arithmetic*.

But the real reason pointers are necessary is that they're the most
efficient built-in iterators for the built-in array types, they are used
as handles to dynamically allocated memory, and they're the only
nullable references we have. Oh, and because millions of C programs
depend on them.

Solve the first three (well) and you still have the fourth.
Compatibility is a two-edged sword. It really promotes the language, and
provides a migration path. But it also restricts the kinds of solutions
which may be applied.

Hey, it's not a perfect language. It is a useful language, though.

It also doesn't mean that there aren't some Java features which are more
attractive than the C++ equivalents. Java is a useful language, and
becoming moreso all the time. By saying good things about C++ I am
definitely not putting down java. I like both, so far (not doing work in
Java yet).

Tim
---
[ 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
]





Author: Tim Ottinger <tottinge@oma.com>
Date: 1997/07/30
Raw View
Paul D. DeRocco wrote:

> Howard Salmon wrote:
> >
> > I'm teaching myself C++ and I'm struggling with understanding why
> creating
> > pointers are necessary.  It's possible to create programs without
> > pointers...so what's the advantage of pointers?
>
> Many uses of pointers can be avoided using references. However, C
> didn't
> have references, so to an extent C++ has pointers merely because C
> did.
> However, pointers allow you to do something that references don't,
> which
> is to modify the pointer.
>
> Internally, a reference is a kind of pointer, but the difference lies
> in
> what the name refers to. With pointers, the name refers to the
> address,
> and hence the address can be changed to point to something else (or to
>
> nothing, using zero as a pointer). Therefore, to get to what is
> pointed
> to requires an operator (unary *) on the pointer. With references, the
>
> name refers to the thing pointed to, so the pointer is implicit, and
> therefore cannot be changed.

Right. A reference cannot be 'null' (well, not safely -- there are ways
to spoof this which are dangerous and nonportable) and cannot be
reassigned.  I teach that references are to be used in preference to
pointers (and const references preferred over non-const references).
Pointers are used in a fallback position. :-)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/07/23
Raw View
captarm@azstarnet.COM (Howard Salmon) writes:

 |>  I'm teaching myself C++ and I'm struggling with understanding why creating
 |>  pointers are necessary.  It's possible to create programs without
 |>  pointers...so what's the advantage of pointers?  The best I could come up
 |>  with was that data is "two-dimensional", i.e., there's the data itself,
 |>  and then the location of the data.  Each of these things needs to be
 |>  stored in separate places in memory.  Does the OS move the location of
 |>  data around, hence the need for pointers?  Or do we ever have a need for
 |>  moving the data location around?  I feel like I'm just on the verge of
 |>  understanding why we NEED pointers.

Get a book about algorithms, and look up linked lists, trees, graphs,
etc.

--
James Kanze   home:   kanze@gabi-soft.fr       +33 (0)1 39 55 85 62
              office: kanze@vx.cit.alcatel.fr  +33 (0)1 69 63 14 54
GABI Software, 22 rue Jacques-Lemercier, F-78000 Versailles, France
           -- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: walth@netcom.com (Walt Howard)
Date: 1997/07/24
Raw View
On 23 Jul 97 15:28:27 GMT, seurer@rchland.ibm.com (Bill Seurer)
wrote:

>In article <rf5iuy2m9p1.fsf@vx.cit.alcatel.fr>, James Kanze <james-albert.kanze@vx.cit.alcatel.fr> writes:
>|> Get a book about algorithms, and look up linked lists, trees, graphs,
>|> etc.
>
>All of which are possible without literal pointers.  Java gets by just
>fine without pointers.  Of course in most cases the construct you use
>instead of pointers (i.e., references) really are pointers "under
>the covers" but the compiler worries about the details instead of you.

 Internally, once compiled into machine code, every language
is almost ALL pointers. Referencing parameters on the stack is
pointers. How much of code is functions?

 I've been trying to get some time to learn Java. The
discipline of having to use references instead of pointers is
good to learn up front. Except where references won't do, I never
use pointers in C++.

 Walt Howard
---
[ 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
]





Author: walth@netcom.com (Walt Howard)
Date: 1997/07/24
Raw View
On 23 Jul 97 09:27:16 GMT, James Kanze
<james-albert.kanze@vx.cit.alcatel.fr> wrote:

>captarm@azstarnet.COM (Howard Salmon) writes:
>
> |>  I'm teaching myself C++ and I'm struggling with understanding why creating
> |>  pointers are necessary.  It's possible to create programs without
> |>  pointers...so what's the advantage of pointers?  The best I could come up
> |>  with was that data is "two-dimensional", i.e., there's the data itself,
> |>  and then the location of the data.  Each of these things needs to be
> |>  stored in separate places in memory.  Does the OS move the location of
> |>  data around, hence the need for pointers?  Or do we ever have a need for
> |>  moving the data location around?  I feel like I'm just on the verge of
> |>  understanding why we NEED pointers.
>
>Get a book about algorithms, and look up linked lists, trees, graphs,
>etc.

 Here's a good analogy. The world wide web. An URL is a
pointer. The actual web page is the real data. Do you save
locally, every web page you access? Or do you just save its URL?
Think about the reasons URLs are good. Pointers have just about
all the same good reasons.

 Walt Howard
---
[ 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
]





Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/07/24
Raw View
In article <5r57bp$15j2$3@news.rchland.ibm.com>,
Bill Seurer <billseurer@VNET.IBM.COM> wrote:
>In article <rf5iuy2m9p1.fsf@vx.cit.alcatel.fr>, James Kanze <james-albert.kanze@vx.cit.alcatel.fr> writes:
>|> Get a book about algorithms, and look up linked lists, trees, graphs,
>|> etc.
>
>All of which are possible without literal pointers.  Java gets by just
>fine without pointers.  Of course in most cases the construct you use

So does Lisp, one of the oldest languages around.

>instead of pointers (i.e., references) really are pointers "under
>the covers" but the compiler worries about the details instead of you.

Correct.  The difference is that some language designers prefer to make the
programmer deal with the details, while others choose to let the language
handle the complexities.

Also, the languages that automatically handle the pointers sometimes have
little inconsistencies: variables of some types are references, while
variables of other types (usually atomic types like integers) are
immediate.  I've seen many beginning Lisp programmers confused by the
difference between:

(setq x 1)
(defun setter-1 (a)
  (setq a 2))
(setter-1 x)
x => 1

and:

(setq y (list 1 2))
(defun setter-2 (b)
  (setf (car b) 2))
(setter-2 y)
y => (2 2)

They wonder why the first one's assignment doesn't work, while the second
one's does.  If you're forced to deal with the pointers, this confusion is
much less likely -- you know precisely when sharing is taking place.  On
the other hand, dealing with pointers opens up plenty of other areas for
confusion.

Note that C still has areas where this confusion occurs, when dealing with
arrays and strings; there are many contexts where they morph into pointers,
and beginners are often confused about when this does or doesn't occur.

--
Barry Margolin, barmar@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
---
[ 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
]





Author: george@dircon.co.uk (George Curry)
Date: 1997/07/25
Raw View
In article <captarm-2007971209450001@169.197.21.243>, captarm@azstarnet.COM (Howard Salmon) wrote:
>I'm teaching myself C++ and I'm struggling with understanding why creating
>pointers are necessary.  It's possible to create programs without
>pointers...so what's the advantage of pointers?  The best I could come up
>with was that data is "two-dimensional", i.e., there's the data itself,
>and then the location of the data.  Each of these things needs to be
>stored in separate places in memory.  Does the OS move the location of
>data around, hence the need for pointers?  Or do we ever have a need for
>moving the data location around?  I feel like I'm just on the verge of
>understanding why we NEED pointers.
>

Pointers are extremely useful and vital when writing large C and C++ programs.
There are many ways that pointers are used in C and C++.  Here are the main
uses I can think of off the cuff.

1.      To perform calls by reference.
2.      Dynamically allocated structures.
3.      To perform calls by value for large structures (although this is
         really a special case of 1).
4.      For traversing structures (although this could be regarded as a
         special case of 2).
5.      To optimise performance.
6.      Polymorphism in C++

Perform calls by reference.

        This is equivalent to the use of 'var' arguments in Pascal and 'ByRef'
        arguments in Visual Basic.  In C the variable's address is passed as
        an argument.  The receiving function must then dereference the
        argument to use it.  This kind of usage can also be performed in C++
        using a reference, this has a slicker syntax which alleviates the need
        to take the address of a variable when making the call, and
        dereferencing during.  It is however not considered good practice to
        use reference notation for situations where the original variable can
        be modified, since by convention it is assumed that function calls do
        not have side effects on their arguments.  Using pointer notation
        during the call makes the use of so called side effects more apparent.

        By default arrays and structures are passed as pointers.  The most
        obvious case of this being char arrays (e.g. string constants).  One
        of the powerful features of C/C++ is that this is often transparent.

Dynamically allocated structures.

        Any dynamically allocated structure or array (whether created by new
        or malloc) is accessed by a pointer.

Handling Large Structures.

        Passing the contents of large structures on the stack can be very time
        consuming.  Passing the address of a structure is much quicker and the
        use of const in the argument definition can avoid potential side
        effects.  This can be done using a pointer or a reference.

Traversing structures.

        Traversing structures (esp. dynamically allocated structures) of
        requires the use of pointers.  STL makes extensive use of pointers.

Optimising performance.

        A pointer dereference is often faster than using a subscript.

Polymorphism in C++.

        Polymorphism in C++ relies on using pointers to a common base class to
        access derived classes in a polymorphis manner.  This is often
        accompanied by the use of pure virtual functions.

This by no means covers every possible use of structures in C++, but should
give an idea of the most common usages.

George Curry
1997/07/25

George Curry
george@dircon.co.uk
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: captarm@azstarnet.COM (Howard Salmon)
Date: 1997/07/21
Raw View
I'm teaching myself C++ and I'm struggling with understanding why creating
pointers are necessary.  It's possible to create programs without
pointers...so what's the advantage of pointers?  The best I could come up
with was that data is "two-dimensional", i.e., there's the data itself,
and then the location of the data.  Each of these things needs to be
stored in separate places in memory.  Does the OS move the location of
data around, hence the need for pointers?  Or do we ever have a need for
moving the data location around?  I feel like I'm just on the verge of
understanding why we NEED pointers.

--
Howard Salmon
Tucson, AZ
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.crud.com>
Date: 1997/07/22
Raw View
Howard Salmon wrote:
>
> I'm teaching myself C++ and I'm struggling with understanding why creating
> pointers are necessary.  It's possible to create programs without
> pointers...so what's the advantage of pointers?

Many uses of pointers can be avoided using references. However, C didn't
have references, so to an extent C++ has pointers merely because C did.
However, pointers allow you to do something that references don't, which
is to modify the pointer.

Internally, a reference is a kind of pointer, but the difference lies in
what the name refers to. With pointers, the name refers to the address,
and hence the address can be changed to point to something else (or to
nothing, using zero as a pointer). Therefore, to get to what is pointed
to requires an operator (unary *) on the pointer. With references, the
name refers to the thing pointed to, so the pointer is implicit, and
therefore cannot be changed.

A common C idiom, held over into C++, is to increment a pointer in order
to scan across an array (most commonly a string of characters). OOP
folks raise an eyebrow at this because the ability to manipulate
pointers explicitly leads to the ability to fabricate wild pointers,
which are a persistent source of bugs. However, for better or for worse,
C habits have carried over into C++, giving us "neat" constructs like:

 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.

--

Ciao,
Paul

(Please remove the extra "crud" from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/07/23
Raw View
Howard Salmon <captarm@azstarnet.COM> wrote in article
<captarm-2007971209450001@169.197.21.243>...
> I'm teaching myself C++ and I'm struggling with understanding why
creating
> pointers are necessary.  It's possible to create programs without
> pointers...so what's the advantage of pointers?  The best I could come up
> with was that data is "two-dimensional", i.e., there's the data itself,
> and then the location of the data.  Each of these things needs to be
> stored in separate places in memory.  Does the OS move the location of
> data around, hence the need for pointers?  Or do we ever have a need for
> moving the data location around?  I feel like I'm just on the verge of
> understanding why we NEED pointers.

Pointers provide a flexible way of refering to objects in memory without
having to know much (or any) context.  Suppose we have a class that lists
the name of a person, and the name of their father.

struct person
{
  string name;
  string dads_name; // If we don't know, we'll leave this blank.
};

Now suppose we want to write a function to return the name of a persons
grandfather.

string Grandpa(person& p)
{
  if(p.dads_name == "") return ""; // If we don't know dad assume we don't
know grandfather.
  else ???
};

At this point you can't write the body of this function.  Given a person
object you can't find the corresponding object for that persons dad.  We
could fix this by requiring all person objects to live in a big array

person population[BIG_NUMBER];

Now we can write the Grandpa function as

string Grandpa(person& p)
{
  if(p.dads_name == "") return "";
  else for(int i = 0; i < BIG_NUMBER; ++i)
    if(population[i].name == p.dads_name) return population[i].dads_name;
// Found it.
  return "";  // Dad isn't in population.
}

This might be expensive if we have to do this very often and BIG_NUMBER is
big, so we might change the person structure:

struct person
{
  string name;
  int dads_index; // Index of dad in population or -1
};

Now our function becomes

string Grandpa(person& p)
{
  if(p.dads_index >= 0 && population[p.dads_index].dads_index >= 0)
    return population[population[p.dads_index].dads_index].name;
  else return "";
}

which looks ugly but is almost certainly faster than the earlier version.
Now the restriction that all persons live in the big population array is
often a nuisance.  Also for all practical purposes dads_index was a kind of
pointer.  Its only use was to help find the hunk of memory where dad lives.
 Lets change it to a pointer and see what we get.

struct person
{
  string name;
  person* dad; // NULL if we don't know dad
}

string Grandpa(person& p)
{
  // Note that if(p.dad) means the same thing as if(p.dad != NULL)
  if(p.dad && p.dad->dad) return p.dad->dad->name;
  else return "";
}

This is considerably easier to read, and we have much more flexibility
about where to put dad.  You can think of a pointer as an index into the
big array we call "memory."  In addition to being a memory index a pointer
carries some type information which can prevent certain mistakes.  In this
case dad can be in a big array, be a local variable, or be allocated on the
heap with 'new'.  The Grandpa function doesn't need to know or care.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: davids@shell.UCAR.EDU (Dave Schreiber)
Date: 1997/07/23
Raw View
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
workstation compiles the first construct into a six-instruction loop, but
compiles the second into a five-instruction loop (since the first does
two increments while the second does one;  the second uses a register for
the index variable and thus gets it for "free").  In the past, the first
construct would indeed have been handled more efficiently by the
compilers of the day, but compilers and instruction sets have changed
enough such that old assumptions about what makes for efficient C (and
hence, C++) no longer may be true.  Unfortunately, this is yet another
area in which the legacy from C may adversely affect C++.

>Paul


--
Dave Schreiber     "Can money pay for all the days
 davids             I lived awake but half asleep?"
     wco.com             -Primitive Radio Gods (SOaBPBwMiMH)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]