This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
(1) C-style multi-dimension array pointer: "int (*p)[16][32];"Advantage: continuous memory address for low-level conversion and operation.
Weakness: only 1 dimension can be dynamically assigned. The size '16' and '32' must be constant.
(2) STL container vector: vector< vector< vector<int> > > p; Advantage: fully dynamic size assignment for each dimension. Weakness: discontinuous memory address.(3) Sparse matrix using std::unordered_map: unordered_map<int, unordered_map<int, int> > p
Advantage: memory-saving for sparse matrix .
Weakness: discontinuous memory address, low performance.
(4) STL array: array< array< int, MAX1> , MAX2> Darray = {0};
Advantage: Seems to be continuous memory address? Mordern STL
features enabled.
Weakness: Dimension size is static. We must predict the max size for each dimension.
We choose carefully, and consider that C-style multi-dimension array pointer with shared_ptr is the best way for dynamic m-D array. If continuous memory is not required, STL container vector should be the best.
2017-10-06 19:52, Jonathan Wakely:
You're welcome. Instead of reading the code to understand what changed it might be simpler to see the changes made by the proposal to change the C++ standard: https://wg21.link/p0414 On 6 October 2017 at 12:49, goldenhawking@163.com <goldenhawking@163.com> wrote:Oh, amazing ! I read about the new code of template class in <memory>, really complex , indeed a great job. There seems to be a lot of new knowledge for me to learn, tks! ---Original--- From: "Jonathan Wakely"<jwakely.gcc@gmail.com> Date: 2017/10/6 19:25:02 To: "goldenhawking"<goldenhawking@163.com>; Cc: "libstdc++"<libstdc++@gcc.gnu.org>; Subject: Re: std::shared_ptr compile error for 2D array type (G++7.2) On 6 October 2017 at 12:02, Jonathan Wakely wrote: > On 6 October 2017 at 11:48, Jonathan Wakely wrote: >> On 6 October 2017 at 09:47, goldenhawking wrote: >>> We use std::shared_ptr to hold 2D array like this: >>> >>> std::shared_ptr< int[24] > bar(new int[N][24], [=](int(*p)[24])->void { >>> delete[] p; } ); >>> >>> GNU C++ 4/5/6 is ok . GNU C++ 7.2 gives an error message: >>> >>> error: no matching function for call to 'std::shared_ptr>> [24]>::shared_ptr(int (*)[24])' shared_ptr< int [24] > pt (new int >>> [4096][24]); >>> >>> This may be a problem caused by default construct mechanism of a 2D array. >> >> No, it's because in C++11 and C++14 shared_ptr was not designed to be >> used with arrays. In C++17 arrays are fully supported, but in a way >> that is incompatible with your code. >> >> It will work if you use shared_ptr because that's actually a >> shared_ptr that owns a 2D array. > > Oops, sorry for the type. I meant shared_ptr not "int[]24]" > which isn't a valid type :-) Also you don't need to use the custom deleter, because shared_ptr will do the right thing, and use delete[] to release the memory. So simply: std::shared_ptr< int[][24] > bar(new int[N][24]);
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |