Relative Content

Tag Archive for c++shared-ptr

Create a new operator for an existing class

I’m working in C++ with a C library that uses a lot of pointers in its API.
To make things work without being annoyed by memory management, I’m using extensively std::shared_ptr and std::unique_ptr, but when it comes to calling the internal C function, I have a bunch of get() calls, which I find annoying.

The destructor of shared_ptr

class A { public: ~A() { cout << “A” << endl; } }; class B :public A { public: ~B() { cout << “B” << endl; } }; int main() { shared_ptr<A> a; shared_ptr<B> b(new B); a = b; cout << “main” << endl; } I run this code in VS2022. the output is ” […]

Why does my simple C++ program with shared_ptr segfault?

#include <memory> #include <iostream> #define BLOCKSIZE 100000 struct MyClass { std::shared_ptr<char[]> block; MyClass() : block(std::make_shared<char[]>(BLOCKSIZE)) {} void fill_block() { for(size_t i=0; i<BLOCKSIZE; ++i) { block[i] = static_cast<char>(i % 99); } } void print_one(size_t i) { if (i < BLOCKSIZE) { std::cout << static_cast<int>(block[i]); } else { std::cout << “Index out of range!”; } } }; […]

Shared pointer pass by value makes count as 0 on mac and 1 on win

I saw that Shared pointer pass by value makes count as 0 on mac but remains as 1 on window for my code.
So I have been trying to understand shared pointer pass by value and pass by reference by listening to the talk reference talk . This was referenced in some other stack overflow link here

Shared pointer pass by reference makes count as 0 on mac and 1 on win

I saw that Shared pointer pass by value makes count as 0 on mac but remains as 1 on window for my code.
So I have been trying to understand shared pointer pass by value and pass by reference by listening to the talk reference talk . This was referenced in some other stack overflow link here