comp.lang.ada
 help / color / mirror / Atom feed
From: Ehud Lamm <mslamm@mscc.huji.ac.il>
Subject: Re: Representation question (void *)
Date: 1999/11/17
Date: 1999-11-17T07:30:45+00:00	[thread overview]
Message-ID: <Pine.A41.3.96-heb-2.07.991117092152.84066B-100000@pluto.mscc.huji.ac.il> (raw)
In-Reply-To: 80t8er$23g$1@birch.prod.itd.earthlink.net

|Lipscomb, Kevin <KLipscomb@C2CEN.USCG.mil> wrote in message
|news:7E47A2C771ADD211AD3C00A0C960172D01269B57@ISCPORTSEX...
|> How would one represent the following C declaration in Ada 95?
|>
|>    void *data;
|>
|> Is this just a placeholder for a pointer that should never be referenced?
|>

In discussing the creating of a general sort routine for any type, I
recently wrote this:

"Let's examine the solution used in the C standard library. The C
standard library offers two interesting functions: \textbf{qsort} which is
a
sorting function and \textbf{bsearch} which is used for
searching. They are defined as follows:
\begin{verbatim}
void *bsearch(const void *key, const void *base, 
              size_t n, size_t size,
              int (*cmp)(const void *keyval, const void *datum))
  bsearch searches base[0]..base[n-1] for an item that matches
  *key. The function cmp must return negaitve if its first
  argument (the search key) is less than its second (a
  table entry), zero if equal, and positive if
  greater. Items in the array base must be in ascending
  order. bsearch returns a pointer to a matching item, or
  NULL if none exists.

void qsort(void *base, size_t n, size_t size,
           int (*cmp)(const void *, const void *))
  qsort sorts into ascending order an array base[0]..base[n-1] or
  objects of size size. The comparison function cmp is as in bsearch.
\end{verbatim}

\par
The C routines make use of a common programing trick in C. They
receive ``void *'' arguments, which are taken to mean any memory
address, and do the low-level pointer manipulation using the sizes
supplied as parameters. 
\par
Notice that this approach ignores type safety, since ``void *'' is
simply an idiom of ``any type.''  This has disadvantages for the user
of the routine, but also for the routines programmer, who must do low
level pointer arithmetic, instead of using the higher level of
abstraction provided by types.
\par
Ada provides a type safe solution, by providing \textit{generic
  programming} facilites,...."[1]

Of course you can mimic the C coid* trick by using the unchecked
programming facilies in Ada, with all their charm :-)

[1] Again from "The Little Abstractionsist"

Ehud Lamm mslamm@mscc.huji.ac.il
http://purl.oclc.org/NET/ehudlamm <== My home on the web 
Check it out and subscribe to the E-List- for interesting essays and more!








  reply	other threads:[~1999-11-17  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
1999-11-16  0:00 ` David C. Hoos, Sr.
1999-11-17  0:00   ` Ehud Lamm [this message]
1999-11-16  0:00 ` Keith Thompson
1999-11-17  0:00   ` tmoran
1999-11-17  0:00 ` Matthew Heaney
1999-11-17  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