Timeline for C++ wrapper around raw array - memory management
Current License: CC BY-SA 4.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 8, 2023 at 18:25 | vote | accept | Martin Perry | ||
| Apr 8, 2023 at 16:18 | comment | added | Ben Cottrell |
@MartinPerry Just to clarify, I am certainly not suggesting using templates at all (there's simply no need for that - you already know the concrete types), I'm only suggesting using shared_ptr<vector<double>> inside your class.
|
|
| Apr 8, 2023 at 13:55 | answer | added | amon | timeline score: 3 | |
| Apr 8, 2023 at 12:48 | comment | added | Martin Perry | @BenCottrell Yes, the array size etc. is managed by my wrapper. The posted code is just a basic sample. I have some AVX-related methods and most of all, I don't want to have template in class signature, I have variant logic inside (similar to OpenCV cv::Mat), but that is out-of-scope of this question :) | |
| Apr 8, 2023 at 12:45 | comment | added | Ben Cottrell | Also, if your idea is to add the extra methods to RawDataArray then i don't really see that being any different to adding the methods to a vector wrapper except that there'd be less code to write and test when using vector? | |
| Apr 8, 2023 at 12:43 | comment | added | Ben Cottrell |
@MartinPerry Correct, but that's not what I meant - using a shared_ptr doesn't add any kind of safety to the array; for example the shared_ptr doesn't know the array's size, so that would need to be tracked separately; there is also no bounds-checking for element access which you would get from using vector::get, and no resizing either. But mostly that what you seem to be doing with RawDataArray looks pretty much like a naive vector implementation anyway. In other words, implementing it yourself only has disadvantages over using vector
|
|
| Apr 8, 2023 at 12:37 | comment | added | Martin Perry | @BenCottrell According to this, stackoverflow.com/questions/13061979/…, I understand that raw array can be managed by shared_ptr. Pre C++17 I can use it as well with custom deleter. | |
| Apr 8, 2023 at 12:31 | comment | added | Ben Cottrell | @MartinPerry a raw array won't be managed by shared_ptr - you do not have any of the safety provided by the vector API when using raw arrays. If you have some other methods which are needed then a wrapper class seems like a sensible option - you can encapsulate whatever functionality is needed for working on the vector in the wrapper | |
| Apr 8, 2023 at 12:30 | comment | added | Martin Perry | @BenCottrell I was thinking of this as well, but it seems rather "weird" when I can create raw array directly and it is managed by shared_ptr as well. As for the wrapper object, I have there some other methods, it is not just a simple vector extension. | |
| Apr 8, 2023 at 12:29 | comment | added | Martin Perry |
@PhilipKendall To minimize memory allocations. If I use std::vector, it is copied with each copy of the wrapper object. So I was thinking of C++17 shared_ptr for the raw array. However, I am not sure if there is some "better" way
|
|
| Apr 8, 2023 at 12:28 | comment | added | Ben Cottrell |
Why not shared_ptr<vector<double>> ? You could also wrap the vector in some other class and use a shared_ptr<MyWrapper>.
|
|
| Apr 8, 2023 at 11:51 | comment | added | Philip Kendall | What actual problem are you trying to solve here? | |
| Apr 8, 2023 at 11:11 | history | asked | Martin Perry | CC BY-SA 4.0 |