comp.lang.ada
 help / color / mirror / Atom feed
* Re: Representation question (void *)
  1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
  1999-11-16  0:00 ` Keith Thompson
@ 1999-11-16  0:00 ` David C. Hoos, Sr.
  1999-11-17  0:00   ` Ehud Lamm
  1999-11-17  0:00 ` Matthew Heaney
  1999-11-17  0:00 ` Robert Dewar
  3 siblings, 1 reply; 7+ messages in thread
From: David C. Hoos, Sr. @ 1999-11-16  0:00 UTC (permalink / raw)



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?
>
No. A variable or function argument of type pointer to void means it can be
a pointer to an object of any type -- maybe the ultimate in type unsafeness.

As the return type of a function void (without the asterisk) means
a function that returns nothing -- the c equivalent of an Ada procedure.

void *some_function_name means that like the variable or function argument
case, the value returned by the function can point to any type of data.








^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Representation question (void *)
  1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
@ 1999-11-16  0:00 ` Keith Thompson
  1999-11-17  0:00   ` tmoran
  1999-11-16  0:00 ` David C. Hoos, Sr.
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Keith Thompson @ 1999-11-16  0:00 UTC (permalink / raw)


"Lipscomb, Kevin" <KLipscomb@C2CEN.USCG.mil> writes:
> 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?

The following is from an article I posted a few months ago.

My suggestion:

    type Void_Pointer is private;
    Null_Void_Pointer : constant Void_Pointer;
    ...
    type Void_Pointer is new Interfaces.C.Strings.chars_ptr;
    Null_Void_Pointer : constant Void_Pointer
        := Interfaces.C.Strings.Null_Ptr;

By making it a private type, you avoid making the operations in
Interfaces.C.Strings visible to clients.

The closest semantic equivalent to C's void* is Ada's System.Address.
This is *almost* certain to be compatible (I doubt that there's an
existing system on which they're different), but the language doesn't
guarantee it.

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
"Oh my gosh!  You are SO ahead of your time!" -- anon.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Representation question (void *)
@ 1999-11-16  0:00 Lipscomb, Kevin
  1999-11-16  0:00 ` Keith Thompson
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Lipscomb, Kevin @ 1999-11-16  0:00 UTC (permalink / raw)
  To: comp.lang.ada

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?

-- Kevin Andrew Lipscomb
-- CV, PGP keys, etc: http://users.exis.net/~klips








^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Representation question (void *)
  1999-11-16  0:00 ` Keith Thompson
@ 1999-11-17  0:00   ` tmoran
  0 siblings, 0 replies; 7+ messages in thread
From: tmoran @ 1999-11-17  0:00 UTC (permalink / raw)


>The closest semantic equivalent to C's void* is Ada's System.Address.
>This is *almost* certain to be compatible (I doubt that there's an
>existing system on which they're different), but the language doesn't
>guarantee it.
   I'm looking at an Ada 95 compiler manual that says its Ada
"Access a_type" maps to "*a_type".  It also says:
"System.Address for Windows NT compilers is 48 bits, including the
segment.  Most 32 bit C compilers cannot use the segment information.
If your compiler can, the declaration would likely be 'far *a_type'.
Otherwise ... pass the (offset component) as '*a_type'.  That will
work only if the address has the same segment as your program."
   We've found in CLAW, which of course is in the business of
talking to C (Windows), a rather modest need for "void".  A quick
scan shows 63 SLOC containing the words Void or LPVoid, out of
about 125 KSLOC.  YMMV




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Representation question (void *)
  1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
  1999-11-16  0:00 ` Keith Thompson
  1999-11-16  0:00 ` David C. Hoos, Sr.
@ 1999-11-17  0:00 ` Matthew Heaney
  1999-11-17  0:00 ` Robert Dewar
  3 siblings, 0 replies; 7+ messages in thread
From: Matthew Heaney @ 1999-11-17  0:00 UTC (permalink / raw)


In article <7E47A2C771ADD211AD3C00A0C960172D01269B57@ISCPORTSEX> , 
"Lipscomb, Kevin" <KLipscomb@C2CEN.USCG.mil> wrote:

> 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?

Type void_ptr needs to be in Interfaces.C, but it isn't right now.  In
the meantime, just use type System.Address.

  Data : Address;

If you know the type of the data you're pointing to, then you can
declare an actual access type, and use a convention pragma:

  type whatever is ...

  type Whatever_Ptr is access all Whatever;
  pragma Convention (C, Whatever_Ptr);


--
Evolution is "only" a theory?  "Creationism is not *even* a theory,
because its proponents have decided in advance that no amount of
evidence will change their beliefs."

Steve Walsh and Thomas Demere, "Facts, Faith, and Fairness"




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Representation question (void *)
  1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
                   ` (2 preceding siblings ...)
  1999-11-17  0:00 ` Matthew Heaney
@ 1999-11-17  0:00 ` Robert Dewar
  3 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1999-11-17  0:00 UTC (permalink / raw)


In article
<7E47A2C771ADD211AD3C00A0C960172D01269B57@ISCPORTSEX>,
  comp.lang.ada@ada.eu.org wrote:
> How would one represent the following C declaration in Ada 95?
>
>    void *data;


Usually the best representation is System.Address. Then to
actually use the value on the Ada side, you need to do an
(unchecked) conversion to the appropriate access type, using
Address_To_Access_Conversions. This conversion is of course
type unsafe, but void pointers are fundamentally unsafe!


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Representation question (void *)
  1999-11-16  0:00 ` David C. Hoos, Sr.
@ 1999-11-17  0:00   ` Ehud Lamm
  0 siblings, 0 replies; 7+ messages in thread
From: Ehud Lamm @ 1999-11-17  0:00 UTC (permalink / raw)


|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!








^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-11-17  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-16  0:00 Representation question (void *) Lipscomb, Kevin
1999-11-16  0:00 ` Keith Thompson
1999-11-17  0:00   ` tmoran
1999-11-16  0:00 ` David C. Hoos, Sr.
1999-11-17  0:00   ` Ehud Lamm
1999-11-17  0:00 ` Matthew Heaney
1999-11-17  0:00 ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox