comp.lang.ada
 help / color / mirror / Atom feed
From: Ole-Hjalmar Kristensen <ole-hjalmar.kristensen@substitute_employer_here.com>
Subject: Re: Ada access vs C/C++ pointers and references
Date: 19 Aug 2004 09:59:15 +0200
Date: 2004-08-19T07:59:15+00:00	[thread overview]
Message-ID: <wvbrekm3h830.fsf@sun.com> (raw)
In-Reply-To: b47de02.0408181427.25d82fe1@posting.google.com

>>>>> "KHD" == Keith H Duggar <duggar@mit.edu> writes:

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

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

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

    KHD> 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.

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

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

Not really. C++ references are constant, Ada access types can be
changed.
Ada access types are *very* similar to C++ pointers.

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

No. This is just syntax. "." would have worked just fine for
dereferencing C/C++ pointers. I suspect the reason for using "->" was
just to make it more visible that you are using a pointer.

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

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

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

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

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

    KHD>     char buffer[80]

    KHD>     // read in some contents into buffer

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

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

    KHD> Here are the operations performed during each pass of the
    KHD> loop

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

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

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

Except that the Ada construct "for i in buffer'range loop ... end
loop" will typically be optimized to yield the most efficient code.
The use of explicit pointer arithmetic may even prevent compilers from
generating the most efficient code, because you give them no choice in
strategy. I have even seen C++ compilers generate faster code for
your method one than your method two. But as usual, it all depends on
your compiler and hardware.

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

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

Yes, an old trick. You save some space, but I doubt you save time, and
it is utterly unreadable to someone who does not know the trick.

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

    KHD> I'm simply asking four things.

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

See above.

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

Yes. If you really want it, it's in System.Storage_Elements

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

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

    KHD> Keith

As Ada does provide pointer arithmetic your two last questions are
somewhat hypothetical, but in my experince, the answer is no.

-- 
   C++: The power, elegance and simplicity of a hand grenade.


P.S. I do most of my programming in C++. It's fun, it's powerful, but
not my first choice to build large, reliable systems.



  parent reply	other threads:[~2004-08-19  7:59 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-18 22:27 Ada access vs C/C++ pointers and references Keith H Duggar
2004-08-19  0:21 ` 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 [this message]
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