is it a bug of "cppunitlite"?

L

LewGun

Hi all:
Today, i read the source code of "cppunitlite", a obscure
problem come from the funciton: (the function reside in the
simplestring.cpp)

SimpleString SimpleString::eek:perator= (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?
 
J

James Kanze

Today, i read the source code of "cppunitlite", a obscure
problem come from the funciton: (the function reside in the
simplestring.cpp)
SimpleString SimpleString::eek:perator= (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++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top