* Single statment STRING to chars_ptr... how to? @ 2000-03-14 0:00 Al Johnston 2000-03-15 0:00 ` Ted Dennison 0 siblings, 1 reply; 3+ messages in thread From: Al Johnston @ 2000-03-14 0:00 UTC (permalink / raw) 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. anyone know how to do this? thanks, ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Single statment STRING to chars_ptr... how to? 2000-03-14 0:00 Single statment STRING to chars_ptr... how to? Al Johnston @ 2000-03-15 0:00 ` Ted Dennison 2000-03-15 0:00 ` Al Johnston 0 siblings, 1 reply; 3+ messages in thread From: Ted Dennison @ 2000-03-15 0:00 UTC (permalink / raw) 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. ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Single statment STRING to chars_ptr... how to? 2000-03-15 0:00 ` Ted Dennison @ 2000-03-15 0:00 ` Al Johnston 0 siblings, 0 replies; 3+ messages in thread From: Al Johnston @ 2000-03-15 0:00 UTC (permalink / raw) To: dennison THANK YOU... I have been pounding my head against this for a day now... > 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 well, when you put it that way... :{ two quick points. 1) I am new to 95 and am simply trying to learn what to do and what not to do. 2) Our real code (being ported from 83 to 95 by me) uses system.address for all of the c interfaces. Some of these probably will work under gnat, some of them will not work (fat pointers), and some I just dont know about. In the case you responded to I am trying to find a replacement for a a single statement in our 83 code, preferably without having to write any subroutines of my own. Here is what the code looks like under 83: return mypkg.my_c_sub(mypkg.cstring_to_adastring(the_string)); where my_c_sub is written in c and imported, and the_string is just a normal ada string. a second case is mypkg.his_c_sub(mypkg.cstring_to_adastring(the_string)); For both of these cases I want the c routines (my_c_sub and his_c_sub) to have a "in" parameter of type interfaces.c.strings.chars_ptr; I know I am trying to have a pointer to memory on the stack, but I was expecting the compiler to be able to carry this construct back up the function returns. In the particular case of if.c.s.to_chars_ptr[if.c.to-c(S)'access] my intent was to have if.c.to-c(S) to return its value by VALUE, via the stack and have the outer function if.c.s.to_chars_ptr operate on that value, making a new thingie on the stack that it could then return by VALUE to its caller, which would then be still "living" in the scope of the caller. This type of thing is done all the time with arrays and structures... so I thought it could be done with some type of anonymous pointer type that would be created/distroyed as needed to propagate the data up the call list. the think on the stack that is pointed to is only there for the interface between two routines, so it lifetime should be okay... I thought I had seen code that did this type of thing. Well, that's what I was trying to do, and why I thought it would be okay... so, where am I going awry? By the way, the reason I was trying to use chars_ptr was I was familiar with it. As best as I can tell, using if.c.char_array is actually a better choice for the two "real code" cases I gave above. Still, I am try to learn the can's and cant's of chars_ptr, so I am still very interested in this. thanks for the help! -al ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-03-15 0:00 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2000-03-14 0:00 Single statment STRING to chars_ptr... how to? Al Johnston 2000-03-15 0:00 ` Ted Dennison 2000-03-15 0:00 ` Al Johnston
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox