From: David <kdgreen@fastmail.fm.au>
Subject: Re: pointer questions
Date: Fri, 28 Oct 2005 21:53:56 +1000
Date: 2005-10-28T21:53:56+10:00 [thread overview]
Message-ID: <43621147$0$8220$afc38c87@news.optusnet.com.au> (raw)
In-Reply-To: <wccvezjrqvv.fsf@shell01.TheWorld.com>
Robert A Duff wrote:
> "Jeffrey R. Carter" <spam@spam.com> writes:
>
>
>>Szymon Guz wrote:
>>
>>
>>>I've got some maybe stupid questions but I don't understand some things:
>>>1. Is there a pointer like (void *) from C that points to anything ?
>>>2. Is there a universal (like above) pointer for procedure|function
>>>that can point to any kind of procedure|funcion ?
>>
>>The short answer, as you've already heard, is No.
>
>
> Well, type System.Address can be used in that fashion.
> Or you can say "type Void_Star is access all Void;"
> and use unchecked conversions. So I'd say the answer is
> "Yes, but you don't usually want to do things this way."
>
>
>>The long answer is that you'll never need these to use Ada.
>
>
> Never? Well, these kinds of low-level hacks are _rarely_ a good idea.
> But you might need them when interfacing to some other language
> that uses such trickery.
>
>
>>...In fact,
>>you'll rarely need pointers at all.
>
>
> I've heard that claim many times in this newsgroup. It doesn't match my
> experience at all. I use pointers all the time.
>
> I just checked one project. About 500 named access types in about a
> quarter million lines of code. That's about one named access type for
> every 500 lines of code. Do you call that "rare"? (I didn't count
> anonymous access types -- there are probably hundreds of those
> in this project, too.)
>
> There are also about 500 packages in this project.
>
> You need pointers in Ada whenever you have complex (linked) data
> structures. Also, whenever you don't know the size of an object
> at the point it is created. Also, whenever the size of an object
> changes wildly over time.
>
> And whenever you have recursive types. (E.g. in a compiler, an
> expression contains subexpressions; in Ada you will need pointers
> to accomplish that, since you can't do it directly.)
>
> And whenever you want to pass a composite type as a discriminant -- you
> have to create a bogus pointer:
>
> type T(Some_Task: access Some_Task_Type) is ...
>
> (because "Some_Task: Some_Task_Type" is annoyngly illegal.
>
> Maybe the kinds of programs I write are different. Question for those
> who rarely use access types: what sort of application areas are you
> talking about? Mine are usually software development tools -- such as
> compilers.
>
> It is true that C requires or encourages pointers in cases where Ada
> does not. Two examples: (1) a common idiom in C is to loop through an
> array using a pointer. In Ada, you would use an index instead
> (which you can also do in C). (2) In C, you need to use heap
> allocation if a struct contains a field whose size is not known
> at compiler time. In Ada, you could use a discriminated record
> instead.
>
> If I designed a language, I would go even further in that direction.
> I would allow mutually recursive types without intervening pointers.
> The only need for pointers in my language would be where
> reference semantics is desired -- e.g. when you want two pointers
> to the same object, such that updates to that object can be seen
> through both.
>
>
>>... If you think you do, especially for
>>beginning Ada, then you're probably misunderstanding something.
>
>
> I don't really agree. A beginner to Ada who knows how to program
> (in some other language, such as C) might well want to make a
> linked list, for example, and that requires pointers of some
> sort -- usually access types.
>
>
>>About the only time you'd need anything like these is when interfacing
>>to C. Judging from your questions, I suspect your understanding of Ada
>>is not yet at a level where you should be doing that.
>
>
> Well, a programmer coming from C might well have some C code lying
> around, and might well want to call it from Ada!
>
> - Bob
I develop larg'ish mission-critical systems in Ada and cannot use
pointers, dynamic memory allocation, tasks, variants, recursion, etc.
etc. You can always find a way to not use pointers as long as you stick
to Ada (not interfacing C/C++). Memory mapped hardware can be accessed
using an expression like "for x'address use at <hardware address>". You
can declare one variable on top of another the same way.
Trying to get code safety-certified with pointers is very difficult to
impossible. Code that doesn't use pointers is invariably simpler and
more reliable but it may require more thought depending upon mind-set.
regard ... David
next prev parent reply other threads:[~2005-10-28 11:53 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-10-26 23:01 pointer questions Szymon Guz
2005-10-26 23:51 ` Gene
2005-10-26 23:58 ` tmoran
2005-10-27 1:12 ` Stephen Leake
2005-10-27 2:30 ` Steve
2005-10-27 5:56 ` Jeffrey R. Carter
2005-10-27 10:19 ` Szymon Guz
2005-10-27 14:14 ` Robert A Duff
2005-11-07 4:21 ` Dave Thompson
2005-10-27 14:11 ` Robert A Duff
2005-10-27 15:13 ` Marc A. Criley
2005-10-27 18:53 ` Jeffrey R. Carter
2005-10-27 17:29 ` Martin Dowie
2005-10-27 18:28 ` Marc A. Criley
2005-10-27 19:28 ` Martin Dowie
2005-10-28 0:12 ` Robert A Duff
2005-10-28 11:57 ` Dr. Adrian Wrigley
2005-10-28 21:26 ` Jeffrey R. Carter
2005-10-30 22:26 ` Robert A Duff
2005-10-31 6:21 ` Jeffrey R. Carter
2005-11-02 0:52 ` Dr. Adrian Wrigley
2005-11-02 3:46 ` Jeffrey R. Carter
2005-11-02 11:16 ` Dr. Adrian Wrigley
2005-11-02 13:39 ` Robert A Duff
2005-11-02 15:34 ` Bob Spooner
2005-11-02 18:59 ` Björn Persson
2005-10-27 18:43 ` Jeffrey R. Carter
2005-10-28 0:42 ` Robert A Duff
2005-10-28 5:58 ` Martin Dowie
2005-10-28 21:24 ` Jeffrey R. Carter
2005-10-28 11:53 ` David [this message]
2005-10-29 12:25 ` Simon Wright
2005-10-27 17:19 ` Martin Krischik
2005-11-07 4:21 ` Dave Thompson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox