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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f32236e7e55b02e0 X-Google-Attributes: gid103376,public From: Hyman Rosen Subject: Re: Ada Queue Date: 2000/04/07 Message-ID: #1/1 X-Deja-AN: 608059815 Sender: hymie@calumny.jyacc.com References: <38eca724@news.hamilton.edu> <8ciesn$4mt$1@nnrp1.deja.com> <38ecc752@news.hamilton.edu> <8cisft$jtg$1@nnrp1.deja.com> <38ed0123@news.hamilton.edu> <8ckqbi$llg$1@nnrp1.deja.com> X-Complaints-To: abuse@panix.com X-Trace: news.panix.com 955127062 8962 209.49.126.226 (7 Apr 2000 17:04:22 GMT) Organization: PANIX Public Access Internet and UNIX, NYC NNTP-Posting-Date: 7 Apr 2000 17:04:22 GMT Newsgroups: comp.lang.ada Date: 2000-04-07T17:04:22+00:00 List-Id: Ted Dennison writes: > In article <38ed0123@news.hamilton.edu>, > For those who don't want to bother surfing there, the relevent source is: > > listelement * AddItem (listelement * listpointer, int data) { > listelement * lp = listpointer; > if (listpointer != NULL) { > while (listpointer -> link != NULL) > listpointer = listpointer -> link; > listpointer -> link = (struct listelement *) malloc (sizeof > (listelement)); > listpointer = listpointer -> link; > listpointer -> link = NULL; > listpointer -> dataitem = data; > return lp; > } > else { > listpointer = (struct listelement *) malloc (sizeof > (listelement)); > listpointer -> link = NULL; > listpointer -> dataitem = data; > return listpointer; > } > } > > The reasons I think he probably did it this way are: > 1, C has no "in out" parameters like Ada. In order to emulate them, he'd > have to have used something like "listelement ** listpointer", a > construction that is both ugly and error-prone. > 2. C automaticly gives you a function return, which subtly encourages > developers to find some use for it. > In other words, I think his interface has more to do with the failings > of C than anything else. This is a very good example of why its not > generally best to do wrote translations of C code into Ada. If I were writing this (and I have, many times), I would certainly use the "error-prone" number 1. Using the function return value is fine - we should return the newly allocated node. My C code would be listelement *AddItem(listelement **linkpointer, int data) { listelement *node; while ((node = *linkpointer) != 0) linkpointer = &node->link; if ((node = *linkpointer = malloc(sizeof(listelement))) != 0) { node->link = 0; node->dataitem = data; } return node; }