Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!umriscc!mcs213f.cs.umr.edu!jamesh From: jamesh@cs.umr.edu (James Hartley) Newsgroups: comp.lang.c++ Subject: How are strings efficiently concatenated? Message-ID: <2825@umriscc.isc.umr.edu> Date: 16 Jun 91 06:55:59 GMT Sender: news@umriscc.isc.umr.edu Organization: University of Missouri - Rolla Lines: 34 Originator: jamesh@mcs213f.cs.umr.edu Most implementations of strings that I have seen are centered around re- tention of a character point in the class; specifically, class string { char *str; ... }; Because of the possibility of differing extents, memberwise initialization of two strings is unwise. For example, string s0, s1("one"); s0 = s1; s0 should dynamically allocate space for a copy of "one" rather than receive a copy of the pointer to "one". How is one to reclaim heapspace when concatenating strings together as in: string s2("two"); s0 = s1 + s2; The expression s1 + s2 will allocate space for "onetwo", but s0 will create a new copy "onetwo". How is the intermediate string deallocated? What I would really like to do is write efficient code for the following -- s0 = s1 + s2 + s3; No reasonable request will be turned down; inquiring minds want to know... -- James J. Hartley _ /| Internet: jamesh@cs.umr.edu Department of Computer Science \'o.O' Bitnet: jamesh@cs.umr.edu@umrvmb.bitnet University of Missouri - Rolla =(___)= UUCP: ...!uunet!cs.umr.edu!jamesh "Life is like an analogy..." U ACK! PHFFT!