comp.lang.ada
 help / color / mirror / Atom feed
From: duggar@mit.edu (Keith H Duggar)
Subject: Ada access vs C/C++ pointers and references
Date: 18 Aug 2004 15:27:47 -0700
Date: 2004-08-18T15:27:47-07:00	[thread overview]
Message-ID: <b47de02.0408181427.25d82fe1@posting.google.com> (raw)

In the "ADA Popularity Discussion Request" thread, the topic
of C/C++ pointers and Ada access types came up. Here are the
related quotes

Keith H Duggar writes :
> Or are there simply missing features that preclude some
> efficient coding idioms (does Ada have pointers?).

Ludovic Brenta writes :
> Yes, Ada does have pointers.  They're called
> "access types" in Ada.

Dmitry A. Kazakov writes :
> Yes, Ada has pointers, and interestingly, because that
> wasn't design intent, richer than C++ has.
> ...
>
> 5. Ada pointers are transparent to member extraction and
> array indexing. I.e. in Ada "->" and "." are same.

Before I raise my examples and questions please keep in mind
that I do not know Ada. Therefore, I'm purely seeking
knowledge and all my statements about Ada should be regarded
as questions rather than statements.

It seems to me that Ada access types are more akin to C++
references than there are to C/C++ pointers. For by pointer
I literally mean an integral type supporting integer
arithmetic which can be interpreted as a memory address.

In reference to point 5. above, pointer arithmetic is partly
why C++ has both "->" and "."

In C/C++ such pointer arithmetic allows for a number of
idioms intended to produce faster or more efficient code.
For example, consider array subscript access:

    C++ :  x[i]
    Ada :  x(i)

Since both languages allow subscripting by integers that
may not be available until runtime, both C++ and Ada
compilers must produce machine code that effectively:

1) computes the location in memory of x[i] from the location
   of x and the value of i
2) "dereferences" that location for either read or write (by
   loading into a register, etc)

Pointer arithmetic in C/C++ sometimes allows you to skip
step 1 by keeping and updating the address directly. For
example, consider the following two examples which both
process all the characters of a null terminated array.

    char buffer[80]

    // read in some contents into buffer

    // method one
    for ( int i = 0 ; buffer[i] != '\0' ; ++i ) { ... }

    // method two
    for ( char * cp = buffer ; *cp != '\0' ; ++cp ) { ... }

Here are the operations performed during each pass of the
loop

method one :  increment i
              compute address of buffer[i]
              dereference address of buffer[i]
              compare to null '\0'

method two :  increment address (pointer) cp
              dereference address cp
              compare to null '\0'

As you can see, method two saves one instruction by storing
and manipulating a memory address directly.

Here is an example of saving space using pointer arithmetic
in a doubly linked list

http://c2.com/cgi/wiki?TwoPointersInOneWord

Now please don't misunderstand me. I'm not advocating these
techniques. Nor do I mean to imply that they are practically
important. Furthermore, allowing pointer arithmetic creates
a host of problems of which I'm sure you are aware.

I'm simply asking four things.

First, is it correct to say that Ada access types ore more
similar to C++ references (T&) than they are to C/C++
pointers (T*) ?

Second, does Ada provide any facilities for direct and raw
memory access by memory address and pointer arithmetic?

Third, if Ada does not provide arithmetic pointers do you
know if this has played any part in acceptance of Ada for
systems programming or efficiency critical applications?

Fourth, have you experienced or can you think of any cases
were pointer arithmetic has helped to solve a problem that
would have been more difficult with Ada access types alone?

Keith



             reply	other threads:[~2004-08-18 22:27 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-18 22:27 Keith H Duggar [this message]
2004-08-19  0:21 ` Ada access vs C/C++ pointers and references Nick Roberts
2004-08-19 11:42   ` Marin David Condic
2004-08-19  0:29 ` Jim Rogers
2004-08-19  5:19 ` Ludovic Brenta
2004-08-19  6:21   ` Martin Dowie
2004-08-19 11:55     ` Marin David Condic
2004-08-19 18:01     ` Ludovic Brenta
2004-08-19  7:19   ` j
2004-08-19  7:42     ` Martin Dowie
2004-08-19 12:03     ` Marin David Condic
2004-08-19  7:59 ` Ole-Hjalmar Kristensen
2004-08-19 15:33   ` Keith H Duggar
2004-08-19 21:32     ` Ludovic Brenta
2004-08-19 22:18       ` Hyman Rosen
2004-08-20  7:52     ` Ole-Hjalmar Kristensen
2004-08-19  8:11 ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox