From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,404527a85668e8ac X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: Single statment STRING to chars_ptr... how to? Date: 2000/03/15 Message-ID: <8ao96a$nk0$1@nnrp1.deja.com>#1/1 X-Deja-AN: 597818037 References: <38CE7BB6.C045C710@mindspring.com> X-Http-Proxy: 1.0 x24.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Wed Mar 15 15:11:12 2000 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.7 [en] (WinNT; I) Date: 2000-03-15T00:00:00+00:00 List-Id: In article <38CE7BB6.C045C710@mindspring.com>, Al Johnston 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.