From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,7272aa7508a3d83f X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news1.optus.net.au!optus!newsfeeder.syd.optusnet.com.au!news.optusnet.com.au!not-for-mail Date: Fri, 28 Oct 2005 21:53:56 +1000 From: David User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: pointer questions References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <43621147$0$8220$afc38c87@news.optusnet.com.au> NNTP-Posting-Host: 211.28.141.75 X-Trace: 1130500423 8220 211.28.141.75 Xref: g2news1.google.com comp.lang.ada:6029 Date: 2005-10-28T21:53:56+10:00 List-Id: Robert A Duff wrote: > "Jeffrey R. Carter" 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 ". 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