Advertisement
Guest User

QString copy-on-write example

a guest
Mar 28th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. QString str_a("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
  2. qDebug() << str_a.constData(); // print out address of data buffer
  3. // With copy-on-write strings, str_b does not yet allocate a new buffer and copy str_a's content into it. An associated reference counter for the data is increased instead.
  4. QString str_b=str_a;
  5. qDebug() << str_b.constData(); // print out address of data buffer, it is the same as for str_a
  6. // str_a notices that the reference counter to its current shared data is >1, therefore it can't copy assign "Quisque..." directly into the buffer. Rather a new memory buffer is allocated, the new text copied there and the reference count to the "Lorem ipsum..."-string is decreased by one.
  7.  
  8. str_a="Quisque mollis elit sed vehicula luctus.";
  9. qDebug() << str_a.constData(); // now a new address
  10. qDebug() << str_b.constData(); // same as for the initial printout
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement