[英]How does a double pointer reconcile with a shared_ptr?
預先披露:我認為整個事情都是胡說八道並且偶然“起作用”,但我發現這段代碼並且它似乎對足夠低的工作價值“有效”(因為它在運行時不會崩潰,這不會意思很多),我不明白為什么。 手頭的問題是extern "C" API 作為 DLL/so 公開,然后通過 FFI 調用(在本例中為 Python),但ext ...
[英]How does a double pointer reconcile with a shared_ptr?
預先披露:我認為整個事情都是胡說八道並且偶然“起作用”,但我發現這段代碼並且它似乎對足夠低的工作價值“有效”(因為它在運行時不會崩潰,這不會意思很多),我不明白為什么。 手頭的問題是extern "C" API 作為 DLL/so 公開,然后通過 FFI 調用(在本例中為 Python),但ext ...
[英]'std::bad_weak_ptr' error while using shared_from_this
注意:在發布問題之前,我在使用shared_from_this將現有 shared_ptr 實例的 shared_ptr 傳遞給另一個方法時,已經完成了關於 std::bad_weak_error 的現有問題。 它們都不像這樣: 在嘗試調用 shared_from_this shared_from_ ...
[英]Why make_shared call the destructor even if there are still weak pointer?
從理論上講, make_shared()和shared_ptr之間的一個區別是 memory 分配技術。 make_shared()應該使用一個塊,而shared_ptr應該使用兩個塊。 make_shared()的一個缺點應該是即使只有一個weak_ptr還活着,memory 也不能被釋放。 ...
[英]What is the difference between shared_ptr<int> and shared_ptr<int[]>?
我有一個 class 變量ptr ,我希望它指向我的 1D int數組的第一個元素。 這兩個語句有什么區別,我應該使用哪一個? ...
[英]std::bad_weak_ptr after inheriting from class that inherits from std::enable_shared_from_this
我有以下代碼:#include <memory> class A: public std::enable_shared_from_this<A> {}; struct B: public A, public std::enable_shared_from_this< ...
[英]GoogleMock - Leaked mock objects due to cross-referencing mock classes
我有兩個類, Server和Client ,每個類都有自己的模擬 class 並且分別有一個getClients和getServer方法(實際的類要大得多,但這是最小的情況)。 我想在getClients方法返回的MockServer中添加一個變量,允許我在適當的時候在我的測試中修改這個變量(注意c ...
[英]Using memcpy with smart pointers in C++
我有一個這樣定義的智能指針 和另一個數組 我想將 arr 中的數據復制到 ptr,我嘗試使用這個 但是當我嘗試像這樣打印時 我明白了 ptr初始化有問題嗎? ...
[英]shared_ptr reference count changed after passed to a unique_ptr object
我對智能指針還很陌生,無法理解下面附加的代碼的結果。 基本上, shared_ptr object 的引用計數和原始指針在傳遞給另一個unique_ptr object 后會發生變化。 所以代碼是這樣的。typedef shared_ptr<int> sp; ...
[英]What's the purpose of using pointer to std::shared_ptr in C++ library Gandiva
我正在 Apache Arrow 中學習 Gandiva 模塊。 我發現許多 API 需要std::shared_ptr<T>*形式的參數,例如這里是一個典型的 API: 我不明白為什么它使用指向shared_ptr的指針而不是簡單的shared_ptr 。 以我的理解,在C++中應該盡 ...
[英]Are all members of a Smart Pointer allocated on the Heap?
給定以下代碼: Container shared_ptr實例的所有成員是否都存儲在堆上,包括m_elements unordered_map中Element class 的所有實例? 我的問題歸結為: shared_ptr的所有成員都存儲在堆上嗎? ...
[英]_CrtIsValidHeapPointer(block) when using std::shared_ptr
我正在嘗試在一個類函數中創建和使用一個std::shared_ptr ,該類函數的構造函數接受一個std::shared_ptr私有數據成員(屬於同一類),另一個std::shared_ptr使用指向類對象的原始指針創建( this )。 每當我調用創建std::shared_ptr ( foo ...
[英]Heap use after free when using shared pointers
當我嘗試將共享指針傳遞給 class object 時,出現免費錯誤后,我一直在使用堆。 首先,我的樹構建器將根作為私有字段: ASTNode 供參考: 構建 function(使用調車場算法)將節點存儲在出隊中: 例如,對於二元運算符(從 ASTNode 派生的ASTNode ): 在調車場算法 ...
[英]Returning object with shared_ptr
嘿,所以我正在編寫一個 function,如果找到該名稱,它將返回正確的 class。 如果我找不到 class 我應該返回什么以及如何返回。std::vector<std::shared_ptr<myClass>> myClasses; class myClass{ ...
[英]Unhandled exception thrown: read access violation. "this" was 0x8 - Using Shared_ptr
Node.h doubleLinkedList.h Node.cpp doubleLinkedList.cpp main.cpp 我得到的錯誤是:unhandled exception thrown: read access violation. "this" was 0x8 To elabora ...
[英]Get a vector of map keys without copying?
我有一個 map 的對象,其中鍵是std::string 。 如何在不復制數據的情況下生成鍵向量? 我是否需要更改我的 map 以改為使用std::shared_ptr<std::string>作為鍵? 或者你會推薦其他東西嗎? 我當前的代碼是這樣的: MyClass.hclass M ...
[英]Storing and using smart_ptr address
我正在嘗試將shared_ptr傳遞給周圍的 object,它可能是也可能不是 null:#include <iostream> #include <memory> struct MyObject { int i = 0; MyObject(const int i_ ...
[英]std::bad_weak_ptr from shared_from_this()
我正在構建一個異步程序,其中boost::asio::io_context有一個指向我的 object 的共享指針。當我的 object 停止發布帶有回調的異步工作時,我的 object 應該死掉。 我正在使用std::enable_shared_from_this<T>::share ...
[英]C++ Safe Recursive Dereference
我想要一種優雅的方式來安全地讀取包含在“可空類型”(例如 std::optional 和 std::shared_ptr)中的字段中的數據。 舉個例子: 要從給定容器的條目中讀取“名稱”字段,我可以這樣寫: 但我不覺得這特別容易讀或寫。 而且由於可能未設置可選項和共享指針,所以我無論如何也不能。 我 ...
[英]Uniqueness of objects in shared pointers
讓我們考慮下面的代碼: 結果令我吃驚,因為條件 1 為假,但條件 2 為真。 為什么? 你能解釋一下這是怎么回事嗎? 我正在研究 inte.net。 我想了解它是如何工作的 ...
[英]A container of objects with shared_ptr members; another container with copies of those pointers in a different order; How to remove without destroying
編輯:我已經將其更改為在兩個容器中都使用 std::shared_ptr 並且還為基礎 class 指定了一個虛擬析構函數。它仍然在它試圖取消引用std::list<std::shared_ptr<MidiMessage>>::iterator的瞬間崩潰std::list&l ...