Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-5121

Add documentation about sizes in QByteArray in relation to data()

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • 4.6.1
    • 4.5.3
    • Documentation
    • 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)

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            gbk Martin Smith (Qt)
            luuf Leif Ringstad
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes