smart_ptr | Boost.org smart_ptr module | SDK library
kandi X-RAY | smart_ptr Summary
kandi X-RAY | smart_ptr Summary
Boost.org smart_ptr module
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 smart_ptr
smart_ptr Key Features
smart_ptr Examples and Code Snippets
Community Discussions
Trending Discussions on smart_ptr
QUESTION
I have a C++ library that I need to use which uses a build() function to return an object with new via a raw pointer. I want to use this object while using smart pointers but so far I have only found the solution to use smart_ptr.reset(build()) which works but is kind of ugly. Is there a better way of doing this or do I have to use manual memory management?
For some more context the function from the library calls return new
and I am sure you need to call delete on that pointer.
How to assign the address of an existing object to a smart pointer? this is the previous answers I based calling it "ugly" on and the reason why I was asking if there was a better way.
...ANSWER
Answered 2021-Jun-08 at 11:55It depends. Consider this example:
QUESTION
I am trying to code two classes FooFactory and Foo. FooFactory initializes an object ToolBox in the constructor and stores it in a unique_ptr as I want that to be cleaned after the factory is destructed. All the instances of Foo should be able to use ToolBox so I am passing ptr to ToolBox object in the constructor of Foo and storing it as bare ptr.
I am new to c++ development so, my questions in the light of general suggestion I heard :
avoid raw pointers when possible
- Is the usage of bare ptr to store the tool_box object that Foo doesn't own fine in this case? or Can I do better using smart_ptr?
- Is the pattern of passing the ptr to ToolBox from the FooFactory class to every new object the correct or is there something better I can do?
Pseudo-code for my classes:
...ANSWER
Answered 2021-Apr-20 at 13:11A factory
would normally never control the lifetime of an object. It should hand out an appropriate pointer, preferably a std::unique_ptr
and the caller determines it's lifetime.
QUESTION
I have MATLAB code that I want to call the GNU Scientific Library (GSL) C library to compute the singular value decomposition of a matrix. Basically, I need to implement the gateway MEX function. I see how to set the input variables, but how do I set the output variables?
Here is my code so far:
build.m ...ANSWER
Answered 2021-Mar-17 at 21:15There are multiple issues with your code:
- You don't check that the inputs are exactly as expected (real, double, non-sparse, etc.)
- You are attaching memory allocated without MATLAB API functions to an mxArray
- You are freeing the memory behind the pointers right after you attach them to the mxArray
- You don't create the plhs[ ] mxArray variables before using them
Normally, when working with mex routines one would do the following in order:
- Check that inputs are exactly as expected
- Create the output plhs[ ] mxArray variables
- Get the pointers to the data areas of the plhs[ ] mxArray variables
- Pass these pointers to the calculation routines
In your case, maybe there is a reason you want to use the gsl_matrix_alloc( ) routine for your memory allocation instead of the MATLAB API memory allocation functions. Fine, but if you do this then you will have to copy the result into the plhs[ ] mxArray data area. You can't attach the pointer directly because that will screw up the MATLAB Memory Manager and lead to a crash downstream. But in all cases you need to create the plhs[ ] mxArray variables first. E.g., if we just go with your current memory allocation scheme you would need to copy the data:
EDIT for gsl data copy
QUESTION
I had a piece of code that was including some Boost headers. Upon compilation I received errors like
...ANSWER
Answered 2020-Oct-03 at 12:52As it turned out I had a file called Assert.h
in my include-path (a custom file of mine). On case-insensitive filesystems as used by Windows and MacOS, this would shadow the original assert.h
header that actually defines the assert
macro.
The solution therefore was simply to rename my assert-header file.
(I found the solution thanks to https://github.com/luckybulldozer/VisualSFM_OS_X_Installer/issues/15)
QUESTION
I have a question about allocate_unique
from Boost. It looks like that resulting unique_ptr
s are quite limited - they cannot be default constructed to nullptr
without providing a deleter (even an invalid one), and also, move assignment does not work.
Luckily, move construction does work, so I was able to hack around not having move assignment by calling the destructor and move-constructing using placement new.
Is it a defect in Boost that alloc_deleter
is not moveable, and thus disables move assignment of these unique_ptr
s? Or am I misunderstanding something?
ANSWER
Answered 2020-Aug-01 at 20:39Polymorphic allocators are stateful, which means they cannot be default-constructed - because they wouldn't know about the memory resource they're supposed to work with.
This is not particular to PMR or unique pointers, it will also crop up when e.g. using Boost Interprocess allocators on a vector
- you will always have to pass an initializer for the allocator.
Ifff you want a global/singleton memory resource, you can obviously declare a custom deleter that encodes that constant:
QUESTION
As well as I understand, dereferencing - *smart_ptr
, and get()
+ dereferencing *smart_ptr.get()
doing the same thing with smart pointers, but may be there is something under the hood that I'm not aware of, cause I've seen a lot of cases there the second approach was used, so what is the point? Does it affect performance in any way?
ANSWER
Answered 2020-Jun-23 at 18:25There is no difference. The standard in [unique.ptr.single.observers] and [util.smartptr.shared.obs] both define operator*
as being *get()
.
get
should be used when you need a raw pointer to the managed object for passing to something that only excepts a raw pointer. Otherwise, *
and ->
are overloaded for the smart pointers to apply those operations to the managed pointer.
You could be tempeted to use &*smart_ptr
to get a raw pointer to the managed object, but &
is an operator that can be overloaded and could give you the wrong result. the proper way to get the address (a pointer) would be std::addressof(*smart_ptr)
, but using smart_ptr.get()
is shorter.
QUESTION
Here is a simple example of inheriting from a class that contains a smart pointer. We don't do anything with it, just declare it.
...ANSWER
Answered 2020-May-13 at 01:03As hinted by S.M., if we must use unique_ptr
, the trick appears to be to make sure to define a copy constructor, e.g., this example gives the expected results with no error messages,
QUESTION
I have a function that reads in a pointcloud successfully and stores it in pcl::PointCloud::Ptr pcd
I then run
...ANSWER
Answered 2020-Apr-17 at 21:26While I wasn't able to find a solution to this problem, I found a workaround. I switched to using pcl::PCLPointCloud2
intead of pcl::PointCloud
and the code works fine.
QUESTION
I am trying to install the python package pycuda on a Mac OS Mojave with python 3.8.1 using pip. I write:
python -m pip install pycuda
but after many trials I always end with the same error:
error: command 'clang' failed with exit status 1
I made: export CC=/usr/bin/clang export CXX=/usr/bin/clang++ in my .profile file but this did not helped.
The last part of the text written on the terminal says:
...ANSWER
Answered 2020-Apr-15 at 07:54This is the actual error:
ld: library not found for -lcuda
The compiler cannot find your CUDA C library. You will have to install it first, and / or make sure that it's in a location where the compiler can find it.
See also this question on how to install CUDA on OSX.
If you use conda, you can also do
QUESTION
Using boost log, I include the thread id in log messages.
...ANSWER
Answered 2020-Apr-01 at 19:17Starting with the example in the documentation makes for a simpler implementation.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install smart_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