From: David Thompson <dave.thompson2@verizon.net>
Subject: Re: Convert between C "void*" pointer and an access
Date: Sun, 29 Oct 2017 10:50:18 -0400
Date: 2017-10-29T10:50:18-04:00 [thread overview]
Message-ID: <1lqbvclr6rvv6a44o073qptdbo1hrli25k@4ax.com> (raw)
In-Reply-To: orm663$pce$1@franka.jacob-sparre.dk
On Wed, 11 Oct 2017 17:32:35 -0500, "Randy Brukardt"
<randy@rrsoftware.com> wrote:
> "Victor Porton" <porton@narod.ru> wrote in message
> news:orm3qe$1r0c$1@gioia.aioe.org...
> > What is the right way to convert between C "void*" pointer and an access
> > to
> > a tagged or class-wide type?
>
> I doubt that there is a *right* way, there just are several possibilities.
>
> > Ada.Unchecked_Conversion seems to be what I need, but the access type may
> > be
> > "fat" and thus have another format than void*.
>
> Right, but not very likely. I'd expect this to work in most cases.
>
> But our solution in the Claw libraries was simply to use the appropriate
> C-convention access type in the interface definitions and avoid making any
> conversions at all. I believe that on the C side, void* and <anything-else>*
> have to use the same representation, so C convention should work properly
> for any pointer type.
>
The C standard doesn't require that (now or ever). It requires that
- void* and [signed|unsigned] char* (each with or without qualifers =
const, void) must have the same representation, probably because in
K&R1 C, before C89 introduced void, char* was used for many things
that void* (or qualified void*) is now used for.
- pointers to all structs must do the same, as must all unions, mostly
because you can 'forward declare' a struct or union and point to it
before its contents (and thus size/alignment) are known. Notably this
is NOT required for enums, probably because enum types themselves are
not required to have the same representation, and _are_ required to be
fully defined before use -- although enum _literals_ in C are all type
'signed int' regardless of the enum type; this is different in C++.
Except for those, pointer types are allowed to differ.
Nowadays most hardware is byte-addressed, so in practice all data
pointers _are_ the same. Code (function) pointers are sometimes
different, but not often, and never in POSIX. It wouldn't surprise me
if every Ada implementation/target that _has_ C interop pairs to a C
implementation with at least all data pointer types the same.
However, last century I worked on one system with a pretty strong
preference for word addressing, and it did have void* char* different
from int* float* struct* etc, and when I looked at the code generated
it had shifts to convert back and forth all over the place.
next prev parent reply other threads:[~2017-10-29 14:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-11 21:52 Convert between C "void*" pointer and an access Victor Porton
2017-10-11 22:32 ` Randy Brukardt
2017-10-11 23:03 ` Victor Porton
2017-10-12 7:57 ` Dmitry A. Kazakov
2017-10-12 8:05 ` Simon Wright
2017-10-29 14:50 ` David Thompson [this message]
2017-10-11 22:58 ` Victor Porton
2017-10-11 23:12 ` Victor Porton
2017-10-12 1:01 ` Victor Porton
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox