internal_ptr | CppCon 2016 keynote , Herb Sutter | Dataset library
kandi X-RAY | internal_ptr Summary
kandi X-RAY | internal_ptr Summary
In his CppCon 2016 keynote, Herb Sutter described the ideal of code being "leak free by construction", and then went on to detail the uses of std::unique_ptr and std::shared_ptr to ensure this. One of the key shortcomings of the standard smart pointers is that they don't work for data structures with cycles: if A holds a std::shared_ptr to B and B holds a std::shared_ptr to A then they will never be freed, even if there are no other references to A or B. Herb proposed his gcpp library as an experimental solution to this problem. By allocating your objects from the deferred_heap, and using deferred_ptr to point to them, then you are guaranteed that they will be properly destroyed by calling their destructors, even if that destruction is "deferred" until later. This library is an alternative solution to the same problem. Rather than deferring collection of unreachable objects, collection is done immediately, just as with std::shared_ptr. The key difference here is that if the only outstanding references to objects are those within a cycle then the whole set of objects is destroyed, rather than the internal references keeping the whole data structure alive.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of internal_ptr
internal_ptr Key Features
internal_ptr Examples and Code Snippets
Community Discussions
Trending Discussions on internal_ptr
QUESTION
I was implementing classes my_unique_ptr
and my_shared_ptr
that mimic the standard library smart pointers std::unique_ptr
and std::shared_ptr
to get to know it better.
When implementing the destructors I'm in this trouble to decide whether to use delete
or delete[]
to free the memory. As much as I read on SO and other sites, there is no portable way to know how many bytes were allotted by new[]
(or whether new
was used or new[]
) (How many bytes were allocated by new?)
While implementing class my_unique_ptr
I have no way of knowing how many bytes the user will request from the constructor , i.e.
Whether he will do my_unique_ptr ptr1(new int)
or will he do
my_unique_ptr ptr1(new int[5])
If there is a way please let me know!
Here is my class (simplified and without cpy/move constructors):
...ANSWER
Answered 2020-Jun-24 at 04:48You are correct that you can't know if a memory was allocated by new
or new[]
. But you can't even know that a memory stores an automatic duration object. E.g. your user can do:
QUESTION
I'm trying to call a native Windows API from managed C++/CLI. One of the arguments is a void**. The idea is that the function will allocate a memory structure and return a void pointer to the caller, which should be passed back to the API on the next call. So I need to allocate storage for a pointer on the managed side and pass a reference to the C API. I can't figure out how to do this.
I've tried declaring a void * in the caller and passing a reference via various operators: &, internal_ptr<>, pin_ptr<>. I did the same with an IntPtr. I get errors saying the compiler can't convert this to a void**.
Here's one attempt using IntPtr and pin_ptr. I get the following compile error on line 28 (the line that declares the pin_ptr):
E0144 a value of type "interior_ptr
" cannot be used to initialize an entity of type "cli::pin_ptr
"
ANSWER
Answered 2019-Apr-03 at 00:10IntPtr
can be converted to and from void*
, but it isn't the same type.
Since the parameter is out-only, the simple solution is just to use a temporary:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install internal_ptr
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page