[comp.lang.c++] Some coding questions

conliffe@caen.engin.umich.edu (Darryl C. Conliffe) (03/06/91)

(1) I use the following routine to build a string from an integer.
    Is it correct to say that what is returned is the pointer
    to a memory area that is out of scope, and thus is unavailable?  If
    so, why does it work?  If not, when, if ever, does the ostrstream
    buffer get reclaimed?
    
    char *itoa (int i) {  // converts integer to char * that will
      ostrstream it;      // work in & string ops
      it << i << ends;
      char *bp = it.str();
      return bp;
    }
    
(2) Another question: I have seen constructs like the above using
    <stdio.h> and a sprintf call.  Any opinions on that?
    
(3) I also use the tactic on a NIHCL Time object to get the print image
    into a char* for ultimate inclusion in a String.  Any comments here?
    
    char *ttoa (const Time& time) {
      ostrstream time_str;
      time_str << time << ends;
      char *bp = time_str.str();
      return bp;
    }
    
(4) These fragments work, but do they represent at least one of the
    better ways of getting there?  Thanks.
    
(5) As a final note (bait?), while NIHCL's String yields the
    internal char* when passed as a const char* argument, many
    of the prototypes I use are char*, not const char*.  I use this
    function to get the convenience of an expression such as
      String command("/bin/date");
      system ( cptr(command) );
    Is there a better way?
    
    char *cptr(const char* s) {
      char *bp = (char *)s;
      return bp;
    }

___________________

 Darryl C. Conliffe  conliffe@caen.engin.umich.edu  (313) 721-6069
-------------------
--
___________________

 Darryl C. Conliffe  conliffe@caen.engin.umich.edu  (313) 721-6069
-------------------