From: Hyman Rosen <hymie@prolifics.com>
Subject: Re: Ada Queue
Date: 2000/04/07
Date: 2000-04-07T17:04:22+00:00 [thread overview]
Message-ID: <t7n1n5c05v.fsf@calumny.jyacc.com> (raw)
In-Reply-To: 8ckqbi$llg$1@nnrp1.deja.com
Ted Dennison <dennison@telepath.com> 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;
}
next prev parent reply other threads:[~2000-04-07 0:00 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-04-06 0:00 Ada Queue Joseph T.
2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` Joseph T
2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` Joseph T.
2000-04-07 0:00 ` Ole-Hjalmar Kristensen
2000-04-07 0:00 ` Joseph T
2000-04-07 0:00 ` Ted Dennison
2000-04-07 0:00 ` Ted Dennison
2000-04-07 0:00 ` Hyman Rosen [this message]
2000-04-06 0:00 ` Robert Dewar
2000-04-06 0:00 ` Ted Dennison
2000-04-06 0:00 ` Joseph T
2000-04-07 0:00 ` Ole-Hjalmar Kristensen
2000-04-06 0:00 ` tmoran
2000-04-08 0:00 ` tmoran
2000-04-07 0:00 ` MaggieJohn
2000-04-07 0:00 ` Ted Dennison
2000-04-07 0:00 ` Simon Wright
2000-04-09 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox