From: defaria@hpclapd.HP.COM (Andy DeFaria)
Subject: Re: C Strings in Ada?
Date: 8 Jun 90 16:00:22 GMT [thread overview]
Message-ID: <920025@hpclapd.HP.COM> (raw)
In-Reply-To: 920024@hpclapd.HP.COM
>/ hpclapd:comp.lang.ada / emery@D74SUN.MITRE.ORG (David Emery) / 10:21 am Jun 6, 1990 /
>>What is the best way to represent C_Strings in Ada?
>
>The right solution is to keep all strings in the Ada program as Ada
>strings, and change them to/from C strings at the point of interface
>to C. (i.e. within the body of Ada_routine_calling_c). This
>preserves the Ada abstractions, and makes all the Ada things work just
>fine (e.g. 'IMAGE, which returns an Ada string). The only other
>alternative (that makes sense to me) is to make C_STRING a private
>type derived from System.address, but you won't be able to do anything
>useful in the debugger with this.
>
>How you do this conversion is very machine dependent, but here's my
>tricks that work on Verdix (Sun 3) and Meridian (IBM PC):
>
> procedure to_c (str : string)
> is
> procedure c_routine (addr : system.address);
> pragma interface (C, c_routine); -- (void) c_routine(s)
> -- char * s;
> c_string : constant string := str & Ascii.NUL;
> begin
> c_routine(c_string(c_string'first)'address);
> end to_c;
>
> function from_c
> return string
> is
> function c_function
> return System.address;
> pragma interface (C, c_function); -- char * c_function
> function strlen (addr : System.address)
> return integer;
> pragma interface (C, strlen); -- the C function of
> -- the same name
> addr_from_c : System.address;
> begin
> addr_from_c := c_function;
> declare
> answer : string(1..strlen(addr_from_c);
> for answer'address use at addr_from_c;
> begin
> return answer;
> end;
> end from_c;
>
>Hopefully what's going here is obvious, if not let me know and I'll
>send more information.
>
> dave emery
> emery@aries.mitre.org
>----------
I kinda agree with you that the "right" way to do this would be to keep Ada
strings as Ada strings and do the conversion in package but you have not
addressed one of my critical concerns: What about the overhead incurred by
having to call the heap manager to allocate a new string just to append
ASCII.NUL so that the C routine I'm calling will be able to handle it
properly. I anticipate that this overhead will have an severe impact on
performance and Ada already has a bad rap for being a dog. I don't want to
add to that reputation.
Another, problem that you haven't addressed is that of the representation
of a string in a record. Should it be and Ada access to string or should
it be the string itself. Or should it be a C string (char *)?
next prev parent reply other threads:[~1990-06-08 16:00 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
1990-06-04 22:45 C Strings in Ada? Andy DeFaria
1990-06-07 17:41 ` stt
1990-06-08 16:00 ` Andy DeFaria [this message]
1990-06-10 20:38 ` Alex Blakemore
1990-06-11 12:57 ` Allan Doyle
1990-06-11 14:59 ` David Kassover
1990-06-11 19:48 ` Allan Doyle
1990-06-11 21:01 ` David Kassover
1990-06-11 22:30 ` Mike Murphy
1990-06-13 21:20 ` Edward Falis
1990-06-11 17:53 ` David Emery
1990-06-11 19:59 ` Allan Doyle
1990-06-15 7:53 ` Jeff Bartlett
1990-06-11 22:39 ` Andy DeFaria
1990-06-12 14:04 ` David Emery
1990-06-12 18:11 ` Mike Murphy
1990-06-13 13:43 ` stt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox