comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Interfacing Generics with C Pointers
Date: 17 Mar 2003 15:09:34 -0500
Date: 2003-03-17T15:09:34-05:00	[thread overview]
Message-ID: <wccwuixdabl.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: v7c9f6sa793403@corp.supernews.com

Brian Palmer <bpalmer@elmco.com> writes:

> I've got a problem where I get in a pointer to a block of memory that was 
> allocated in C++.  In addition to the pointer, I get the number of bytes 
> that were allocated.  The Ada type that the memory comes in as is a pointer 
> to an unconstrained byte array.  Whenever I try to dereference the pointer, 
> I get a CONSTRAINT ERROR.  I think the problem is due to the compiler 
> trying to access a storage location within the type that tells how many 
> bytes are there.

Probably true.  In general, unconstrained arrays don't work well when
interfacing to C, because C doesn't know about the dope.

>...  I feel like I could constrain the base type and get 
> around the problem, however, this mechanism is used for several different 
> size blocks of memory, several hundred times per second, and I don't want 
> to have to allocate more memory than necessary.  Any ideas how to access 
> unconstrained memory without the compiler looking for that built in 
> element?

One way is to lie to the Ada compiler about the length of the array.
E.g., "type T is array(Natural) of ...;" then you get back a pointer to
T, and you can refer to X.all(I).  Just make sure I doesn't go out of
the "real" ounds.  Presumably the real bounds are available via some
other means.

As soon as possible, start dealing with the data in a safer way -- e.g.,
pass the appropriate slice of X.all off the to rest of the code.

- Bob



      reply	other threads:[~2003-03-17 20:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-17 13:56 Interfacing Generics with C Pointers Brian Palmer
2003-03-17 20:09 ` Robert A Duff [this message]
replies disabled

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