Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #77157

Re: should c be a subset of c++?

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: should c be a subset of c++?
Date 2015-11-25 12:35 +0000
Organization Aioe.org NNTP Server
Message-ID <n349ui$4cg$1@speranza.aioe.org> (permalink)
References (5 earlier) <2c2e0fab-bb94-419c-b92c-2933340e4e16@googlegroups.com> <MPG.30bdd286eb6edd97ad@news.eternal-september.org> <4e2eb610-1853-451f-95c2-d30687ff3bd6@googlegroups.com> <lnvb8r1fz7.fsf@kst-u.example.com> <c91a9494-c6a5-4414-b16c-3ab375f28450@googlegroups.com>

Show all headers | View raw


Malcolm McLean <malcolm.mclean5@btinternet.com> wrote:
 
(snip)

> In the olden days, C programmers were used to working in assembly,
> and the instruction x[i] = 0 would compile to
 
> register 1 = x;
> register 2 = i;
> register a = register 1 + register 2
> write zero to register a

Some processors have indexed addressing mode, combining the last
two steps. That might take more time than a non-indexed load, but
usually less than two instructions.
 
> the instruction *ptr = 0 would compile to
 
> register a = ptr
> write zero to register a
 
> so you could get a worthwhile speed-up using pointer arithmetic,
> and it also made sense in the programmer's worldview.

Also, computers were slower then, so "worthwhile speed-up" was easier.
 
> But compilers and processors have moved on. There's seldom
> any speed advantage for be gained by using a travelling pointer
> rather than index i. So the occasions where pointer arithmetic
> is useful are rare. Integrating with the C++ "iterator" nonsense
> is one, implementing malloc() itself is another, and taking
> the difference between two pointers is maybe another. But
> shouldn't need to increment, decrement, or add a constant to a
> pointer. (Of course taking &x[i] is effectively adding a constant
> to a pointer, the need for the machine operation doesn't go
> away).

Yes.  At least for a simple loop, the compiler should be able
to figure this out. Compilers have been able to do this on larger
machines for longer than there has been C. It took a little longer
for smaller computers.
 
> Data will usually come in arrays, so access with x[i] and step through
> with a for loop. If you need to support fast insertions and deletions,
> use a linked list, and step through with 
> for(ptr = head; ptr; ptr = ptr->next)
 
> if it's a tree use the following pattern
 
> while(node)
> {
>    /* operation here */
>    if(node->child)
>      foo(node->child);
>    node = node->next;
> }
 
> those will support almost everything except special fast access containers,
> databases, and so on. If you use such a structure, usually stepping
> through the data is a special operation that is performed rarely
> or has problems associated with it. 

For the simplest function indexing through arrays, I might still 
use the pointer increment loop. That is, where it is obvious I don't
need the pointer variables for anything else.

Otherwise, the extra work of saving the pointer, the reduction in
readability keeping track of which value is in which variable, 
makes the indexed form more readable.

-- glen

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: should c be a subset of c++? "J. Clarke" <j.clarke.873638@gmail.com> - 2015-11-06 19:06 -0500
  Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 00:41 -0800
    Re: should c be a subset of c++? Richard Damon <Richard@Damon-Family.org> - 2015-11-07 10:01 -0500
      Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 09:12 -0800
        Re: should c be a subset of c++? Martin Shobe <martin.shobe@yahoo.com> - 2015-11-07 11:58 -0600
          Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 10:25 -0800
            Re: should c be a subset of c++? Keith Thompson <kst-u@mib.org> - 2015-11-07 11:54 -0800
              Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 14:22 -0800
                Re: should c be a subset of c++? Martin Shobe <martin.shobe@yahoo.com> - 2015-11-08 13:05 -0600
                Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-08 11:39 -0800
                Re: should c be a subset of c++? Geoff <geoff@invalid.invalid> - 2015-11-13 13:20 -0800
                Re: should c be a subset of c++? James Kuyper <jameskuyper@verizon.net> - 2015-11-13 16:36 -0500
                Re: should c be a subset of c++? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-14 00:03 +0000
                Re: should c be a subset of c++? Geoff <geoff@invalid.invalid> - 2015-11-13 17:06 -0800
                Re: should c be a subset of c++? Geoff <geoff@invalid.invalid> - 2015-11-13 13:29 -0800
            Re: should c be a subset of c++? Martin Shobe <martin.shobe@yahoo.com> - 2015-11-08 12:57 -0600
              Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-08 11:37 -0800
                Re: should c be a subset of c++? Keith Thompson <kst-u@mib.org> - 2015-11-08 17:16 -0800
                Re: should c be a subset of c++? supercat@casperkitty.com - 2015-11-08 18:19 -0800
                Re: should c be a subset of c++? Martin Shobe <martin.shobe@yahoo.com> - 2015-11-08 23:25 -0600
                Re: should c be a subset of c++? Gareth Owen <gwowen@gmail.com> - 2015-11-10 22:34 +0000
        Re: should c be a subset of c++? raltbos@xs4all.nl (Richard Bos) - 2015-11-13 22:18 +0000
          Re: should c be a subset of c++? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-14 00:11 +0000
            Re: should c be a subset of c++? raltbos@xs4all.nl (Richard Bos) - 2015-11-17 22:16 +0000
              Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-17 21:09 -0800
                Re: should c be a subset of c++? Richard Heathfield <rjh@cpax.org.uk> - 2015-11-18 05:22 +0000
                Re: should c be a subset of c++? Keith Thompson <kst-u@mib.org> - 2015-11-18 06:03 -0800
                Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-18 08:44 -0800
                Re: should c be a subset of c++? Keith Thompson <kst-u@mib.org> - 2015-11-18 09:11 -0800
                Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-18 09:54 -0800
                Re: should c be a subset of c++? Philip Lantz <prl@canterey.us> - 2015-11-24 01:49 -0800
                Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-24 02:58 -0800
                Re: should c be a subset of c++? raltbos@xs4all.nl (Richard Bos) - 2015-11-24 11:58 +0000
                Re: should c be a subset of c++? Keith Thompson <kst-u@mib.org> - 2015-11-24 09:09 -0800
                Re: should c be a subset of c++? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-25 00:12 -0800
                Re: should c be a subset of c++? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-25 12:35 +0000
                Re: should c be a subset of c++? raltbos@xs4all.nl (Richard Bos) - 2015-11-27 16:30 +0000
                Re: should c be a subset of c++? raltbos@xs4all.nl (Richard Bos) - 2015-11-19 10:37 +0000
                Re: should c be a subset of c++? Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 15:40 -0800
  Re: should c be a subset of c++? Gareth Owen <gwowen@gmail.com> - 2015-11-10 22:20 +0000

csiph-web