简体   繁体   中英

QSharedPointer::create()

In Qt 4 i have I have the following array:

QSharedPointer<unsigned char> encrypted(new unsigned char[RSA_size(publickey)]);

How can I combine the two allocations in one with the new Qt 5 create function?

QSharedPointer<T> QSharedPointer::create()

Your first example is wrong and will not only leak memory, but cause UB. When you define a QSharedPointer<unsigned char> you are defining a smart pointer for a single element, not an array of elements, therefore delete will be called, not delete[] .

Change it to:

QSharedPointer<unsigned char> encrypted(new unsigned char[RSA_size(publickey)], [](unsigned char* x){ delete[] x; });

That is: you have to provide a custom deleter for the pointer.

Finally, the QSharedPointer::create function is supposed to be used only for one element, not an array of elements and it can be used with:

auto x = QSharedPointer<unsigned char>::create();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM