comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison <dennison@telepath.com>
Subject: Re: Single statment STRING to chars_ptr... how to?
Date: 2000/03/15
Date: 2000-03-15T00:00:00+00:00	[thread overview]
Message-ID: <8ao96a$nk0$1@nnrp1.deja.com> (raw)
In-Reply-To: 38CE7BB6.C045C710@mindspring.com

In article <38CE7BB6.C045C710@mindspring.com>,
Al Johnston <sofeise@mindspring.com> wrote:
> I am playing around with moving data between ada's "String" and
> interfaces.c.strings's "chars_ptr".
>
> Is it possible to do this operation as a single statement and without
> allocating memory (i.e. no calls to things like new_string, new,
etc)..
> I thought
>
> my_chars_ptr := interfaces.c.strings.to_chars_ptr(
> interfaces.c.to_c(my_string)'access);
>
> would work... but it will not compile.
>
> I know how to accomplishes this using multi-statments along with
> a call to i.c.s.new_string and i.c.s.free; that solution is not
> what I am looking for.

The problem is that you are trying to create a pointer to a stack-based
copy of my_string (specificly, the copy passed by by interfaces.c.to_c).
That copy in all likelyhood ceases to exist as a valid object after the
assignment. So if this did compile, you'd have a pointer to unallocated
stack space stored in my_chars_ptr. I think we can agree that its a
*good* thing the compiler won't let you do this.

Instead, what I think you'd like to do is make my_chars_ptr a c pointer
that points to my_string as if it were a c string rather than an Ada
string. Technically this wouldn't be portable, as some systems might use
a different representation for C strings and Ada strings.

But if your system is not one of those and you'd like to do this, the
way most likely to work is probably to do a my_string'address. Then
instantiate System.Address_To_Access_Conversions with char_array. That
will allow you to do a To_Pointer on the address to get a pointer to
char_array, which ought to work for most purposes.

(warning: Uncompiled code)

   package Char_Ptr_Conversions is new
System.Address_To_Access_Conversions (Interfaces.C.Char_Array);

   My_Chars_Ptr : constant Char_Ptr_Conversions.Object_Pointer :=
Char_Ptr_Conversions.To_Pointer (My_String'address);

   -- (I *think* this will work too)
   My_Chars_Ptr : constant Interfaces.C.Strings.Chars_Ptr :=
      Interfaces.C.Strings.To_Chars_Ptr(Char_Ptr_Conversions.To_Pointer
(My_String'address).all'access);

--
T.E.D.
http://www.telepath.com/~dennison/Ted/TED.html


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




  reply	other threads:[~2000-03-15  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-14  0:00 Single statment STRING to chars_ptr... how to? Al Johnston
2000-03-15  0:00 ` Ted Dennison [this message]
2000-03-15  0:00   ` Al Johnston
replies disabled

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