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=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mcnc!rti!jb From: jb@rti.rti.org (Jeff Bartlett) Newsgroups: comp.lang.ada Subject: Re: C Strings in Ada? Summary: on the stack not in the heap Message-ID: <3903@rtifs1.UUCP> Date: 15 Jun 90 07:53:17 GMT References: <920024@hpclapd.HP.COM> <920025@hpclapd.HP.COM> Organization: Research Triangle Institute, RTP, NC List-Id: 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. ;-)