Re: char* to std::string
pepone wrote (comments adjusted):
> can any body sayme how this code works in c++
>
>
> std::string
> MyClass::doSameThing()
> {
> char* data = getSameData(); /*this return same data reserved
> with malloc*/
> std::string retval = data; //Whats happen here i get a new
> // copy of data?
Yes, std::string manages its own storage. It copies the C-style string
you pass to its constructor and does not just hijack the pointer.
> delete(data); // is correct free data now?
It's safe to free it now, but it's wrong to use delete when the memory
was allocated via malloc. Always pair new/delete and malloc/free.
> return retval;
> }
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]