On Sep 12, 8:37 am, "Lew...@gmail.com" <Lew...@gmail.comwrote:
Today, i read the source code of "cppunitlite", a obscure
problem come from the funciton: (the function reside in the
simplestring.cpp)
SimpleString SimpleString::operator= (const SimpleString&
other)
{
delete buffer; //???
buffer = new char [other.size() + 1];
strcpy(buffer, other.buffer);
return *this;
}
"buffer" is a instance of " char * ", so i want to know,the
statment "delete buffer", is it a ill statment? if not, why?
It depends on how the memory for buffer was allocated, but a
priori, it's undefined behavior here, since the memory seems to
have been allocated by a new[].
As assignment operators go, this one is broken anyway. I don't
know where the code comes from, but the author obviously isn't
competent to write C++.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34