[vdr] Re: cString operator= with same buffer

Sascha Volkenandt lists at magoa.net
Sun Nov 20 21:49:54 CET 2005

On Sunday 20 November 2005 20:53, Jon Burgess wrote:
> What happens if someone was trying to remove the initial part of the
> string, e.g. something like:
> 	cString hw("Hello World");
> 	hw = strstr(*hw, " ")+1;

In that case the following would happen (with the original cString plus a 
simple copy ctor):
1. strstr gets the "second word" of "Hello World" (which is part of hw's 
2. a temporary cString is constructed with that result (because cString has no 
operator=(const char*), only an operator=(const cString&) ) making the 
"second word" a copy of the second half of hw's buffer
3. operator=(const cString&) frees hw's old buffer (which is not referenced by 
the temporary) and replaces it with a copy of the temporary cString's buffer
4. the temporary cString is destructed freeing it's buffer

So, no unexpected behaviour here.

Like I mentioned before, I don't think all of this is necessary since the 
normal ctor and the copy ctor cover all cases. Besides, I've never 
encountered string classes doing such overly complicated things to handle 
their buffers, including robust frameworks such as QT or MFC.


More information about the vdr mailing list