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!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: pointer questions Date: 27 Oct 2005 10:11:48 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1130422308 28516 192.74.137.71 (27 Oct 2005 14:11:48 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 27 Oct 2005 14:11:48 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:5989 Date: 2005-10-27T10:11:48-04:00 List-Id: "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