From: jb@rti.rti.org (Jeff Bartlett)
Subject: Re: C Strings in Ada?
Date: 15 Jun 90 07:53:17 GMT [thread overview]
Message-ID: <3903@rtifs1.UUCP> (raw)
In-Reply-To: 920025@hpclapd.HP.COM
In article <920025@hpclapd.HP.COM>, defaria@hpclapd.HP.COM (Andy DeFaria) writes:
> >/ 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?
> >
> > 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;
> ... 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. ...
A good compiler should grow the stack by (str'length + 1 + string_descriptor)
then copy 'str' and the NUL into the area. It would also fill in the
new string_descriptor for 'c_string' from the information in 'str's descriptor
and the additional byte. (A smart compiler may not even allocate space for
the new descriptor, keeping the information in registers).
Constant strings are a good 'trick'. Such as:
...
str : string(1..10);
N : natural;
begin
...
-- a subroutineless call like above
declare
s : constant string := str & ascii.nul;
begin
c_routine( s(s'first)'address );
end;
-- remove the leading space supplied by 'image.
declare
s : constant string := integer'image(N);
begin
text_io.put_line("Activity on PE" & s(s'first+1..s'last) );
end;
-- figuring out how much of a buffer to overwrite
declare
s : constant string := function_that_returns_a_string(j);
begin
line(line'first..line'first+s'length-1) := s; -- result used twice
end;
> 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 *)?
It depends on what operations need to be performed on it. I'll leave this for
someone else. Don't forget that you can:
type s_ptr is access string;
p : s_ptr;
begin
...
-- this will call the function, allocate space for the result, and
-- initialize the area with the result.
p := new string'( ada_function_that_returns_a_string );
...
text_io.put_line("answer was '" & p.all & "'");
If you are paranoid about the Ada run-time heap manager forgetting to free
the space pointed to by 'p', you can instantate a new UNCHECKED_DEALLOCATION.
Hope this helps,
Jeff Bartlett
Research Engineer, Center for Digital Systems Research
Research Triangle Institute, RTP NC.
jb@rti.rti.org mcnc!rti!jb rti!jb@mcnc.org (919)-541-6945
PS: The code above was only compiled with a chemical computer. ;-)
next prev parent reply other threads:[~1990-06-15 7:53 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
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 [this message]
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