Details
-
Suggestion
-
Resolution: Done
-
Not Evaluated
-
4.5.3
-
None
Description
under section:
—
char * QByteArray::data ()
—
Please add something similar to this to the documentation:
—
"To get the number of bytes returned by char * QByteArray::data (), call size() and add 1 for the \0 termination."
—
Examples:
Doesn't work:
QString tmp = "test";
QByteArray qb_array = tmp.toLocal8Bit();
char *todata;
todata = new char[qb_array.size()]
strcpy(todata, qb_array.data());
delete [] todata; // will cause heap crash, because of the \0 that is copied, which is at size() + 1
while this works:
QString tmp = "test";
QByteArray qb_array = tmp.toLocal8Bit();
char *todata;
todata = new char[qb_array.size() + 1]
strcpy(todata, qb_array.data());
delete [] todata; // works, size is now including the 0 termination, and will not crash on the heap.
(tested on Microsoft Visual C++ with Qt 4.5.1)