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); ..
Category : unique-ptr
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 ..
I am currently trying to create a binary tree, each node of which contains a unique_ptr pointing to some other node in the tree. My question is one about design: I am not entirely sure how to write a recursive function that would build a tree like this without calling move on the pointers contained ..
Recently, I decided that it was time that I should dig into smart pointers. I read about the different kinds (unique, shared, weak), but I’m not sure about one thing. Let say that I have a class Player and a GameMap class. The content of those classes are irrelevant for the next part. I also ..
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 ..
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 ..
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 ..
I have a function foo(T value), where T is move-only. I want to (for some reasons) allocate value of type T and call foo. Can I do it like this? { std::unique_ptr<T> ptr = make_unique<T>(T()); foo(std::move(*ptr)); } If it’s not clear: I want to get rid of new and delete in this code: void ServeForever(uint16_t ..
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 = ..
Why can’t I return a unique_ptr from a clone function? I thought I could do this. I have a base class for different mathematical functions called transform. I have a container of pointers to this type because I am using polymorphism. For example, all these derived classes have a different implementation of log_jacobian that is ..
Recent Comments