[vdr] Re: cString operator= with same buffer

Holger Brunn holger.brunn at stud.uni-karlsruhe.de
Sun Nov 20 12:35:03 CET 2005


Sascha Volkenandt wrote:

> Why would you want this, or better why should two cStrings point to the
> same buffer?

Thanks for your reply, after putting together an example, I found that my
problem is rather a symptom.
Look at this code:

#include "tools.h"

cString str=cString("hello world");

void func(cString string)
{
        str=string;
}

int main(int argc, char* argv[])
{
        printf("%s\n", *str);
        func(str);
        printf("%s\n", *str);
}

The problem is that str and string in func point to the same buffer. And
even without assinging string to str, the second printf receives a freed
buffer, for cString's destructor will be called for string when func
returns.

Then apart from dealing with the same-buffer thing, shouldn't cString have a
copy constructor to take care of duplicating the buffer for this case? Or
is cString intended to be passed by reference only?

Thanks for comments
Holger



More information about the vdr mailing list