O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unnecessary allocation

The next question is what actions on the object trigger the actual allocation of a new buffer? Obviously, x[0] = 42 will trigger an allocation because the vector needs a buffer to write the new data to. However, int i = x[0] will also trigger an allocation if x is not declared as a const value or reference. That happens because in C++ this code triggers the non-const overload of operator[] if it's available, even though it's not necessary in this case. The vector doesn't know whether the requested item will or will not be changed, so it has to assume that it will be, and it triggers an allocation before returning a reference to the item in the new buffer.

The same issue takes effect when using other methods that have ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required