comp.lang.ada
 help / color / mirror / Atom feed
From: Keith Thompson <kst@cts.com>
Subject: Re: Interfacing to void *
Date: 1999/07/18
Date: 1999-07-19T06:57:52+00:00	[thread overview]
Message-ID: <yec908dazyn.fsf@king.cts.com> (raw)
In-Reply-To: 37929901.649C0955@worldnet.att.net

William Starner <billeug@worldnet.att.net> writes:
> If I'm trying to interface to a C library that uses this struct
> 
> struct BigNum {
> 	void *ptr;
> 	unsigned int size;
> 	unsigned int allocated;
> };
> 
> how should I interface to void*?
> 
> I have
> private
> 	type voidpointer is access character;
> 	type BigNum is record 
> 		ptr: voidpointer;
> 		size: Interfaces.C.unsigned;
> 		allocated: Interfaces.C.unsigned;
> 	end record;
> 	type RealBigNum is access BigNum;
> 	type UnboundedInt is new Linear with record
> 		Value: RealBigNum;
> 	end record;
> end nla23.linear.bnlib;
> but char* = void* is a C rule, not an Ada rule. It probably works,
> but it's not right. (I don't need to access anything through that
> pointer.)

You're not 100% guaranteed that an Ada type declared as "access
Character" is compatible with a C "char*", or even that Ada's type
Character is the same as C's char.

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>
One of the great tragedies of ancient history is that Helen of Troy
lived before the invention of the champagne bottle.




  reply	other threads:[~1999-07-18  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-07-18  0:00 Interfacing to void * William Starner
1999-07-18  0:00 ` Keith Thompson [this message]
1999-07-19  0:00 ` jerry
1999-07-19  0:00   ` William Starner
1999-07-20  0:00     ` jerry
replies disabled

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