Category : unique-ptr

After a small modification in my code I encountered a segfault while running it and I can’t figure out why. Here is the code working : std::shared_ptr<UCHAR> myClass::myMethod(const char *str) { size_t strLen = strlen(str); ULONG paddedLen = (strLen % this->bufferLen) ? ((strLen / blockSize) + 1) * this->bufferLen : strLen; std::unique_ptr<UCHAR> padded = std::make_unique<UCHAR>(paddedLen); ..

Read more

I’m wrapping a MySQL C++ Connector’s sql::PreparedStatement using a std::unique_ptr. For smaller test runs, this appeared to be fine, but when testing on a larger dataset, I get an exception like: Can’t create more than max_prepared_stmt_count statements (current value: 16382) Presumably this is because the PreparedStatements are not being correctly deleted? Where did I go ..

Read more

My benchmark code is below. Basically I run through 10 million iterations. For each iteration, there are 10 level deep function call passing share_ptr or unique_ptr. The total time calculated with stead_clock is divided by iterations * deep level. It is not meant to be complete accurate on just passing itself since I do have ..

Read more

Shouldn’t unique_ptr prevent the possibility of such an error? #include <iostream> #include <vector> #include <memory> struct B { int b; }; int main() { std::vector<std::unique_ptr<B>> v; // unique_ptr can be stored in a container B* p = new B; v.emplace_back(p); std::cout << "p:" <<p <<"n"; std::cout << "v[0]:"<<v[0].get() << "n"; v.emplace_back(p); std::cout << "p:" <<p ..

Read more

I’m trying to encrypt and decrypt a file, basically exercising std::span and std::unique_ptr. The issues are commented in the code. 2nd parameter of rc4 is std::span but my parameters are vector<uint8_t> for the encryption and unique_ptr<uint8_t> for the decryption, which I’m unable to convert to. *output++ = elem ^ s[(s[i] + s[j]) % 256]; cannot ..

Read more

How do you use algorithms like std::find_if with a vector of unique pointers? For instance: #include <iostream> #include <vector> #include <memory> #include <algorithm> class Integer { public: explicit Integer(int i): i_(i){}; int get() const{ return i_; } private: int i_; }; using IntegerPtr = std::unique_ptr<Integer>; int main() { IntegerPtr p1 = std::make_unique<Integer>(4); IntegerPtr p2 = ..

Read more