Timeline for How do I declare a 2d array in C++ using new?
Current License: CC BY-SA 3.0
14 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 3, 2021 at 13:32 | comment | added | Caleth |
You can even keep the array[x][y] syntax if you are willing to write a row proxy for Matrix::operator[] to return
|
|
Feb 3, 2020 at 18:44 | comment | added | Dronz | @BorisDalstein Oh, that's interesting and useful, thanks very much for the explanation! | |
Feb 3, 2020 at 11:54 | comment | added | Boris Dalstein |
@Dronz With a[x][y] , what you are actually doing is *(*(a + x) + y) : two additions and two memory fetches. With a[index(x, y)] , what you are actually doing is *(a + x + w*y) : two additions, one multiplication, and one memory fetch. The latter is often preferable, for the reasons exposed in this answer (i.e., trading the extra memory fetch with a multiplication is worth it, especially because the data isn't fragmented and therefore you don't cache-miss).
|
|
Jun 17, 2019 at 19:14 | comment | added | user1741137 | Anyone who has done some Cuda programming will recognize this as a good solution; | |
May 26, 2018 at 4:22 | comment | added | Dronz | @AshKetchum Inlining (or maybe macro substitution) makes sense to optimize, but how is the compiled computation more complex than what needs to be done to resolve the address of a[x][y] ? | |
May 8, 2018 at 16:12 | comment | added | rampion |
You can now have lambdas return lvalue references, which can make this a little more user friendly: auto one_dim_array = new int[num_rows * num_cols]; auto two_dim_array = [num_cols, &one_dim_array](size_t row, size_t col) -> int& { return one_dim_array[row*num_cols + col]; } and use it like two_dim_array(i, j) = 5
|
|
May 23, 2017 at 11:33 | history | edited | URL Rewriter Bot |
replaced http://stackoverflow.com/ with https://stackoverflow.com/
|
|
Apr 27, 2017 at 16:49 | comment | added | Ash Ketchum |
The major problem with this solution is that there is extra computation for every index. It becomes worse if you put the index calculation in a function which adds extra overhead. At least, consider using macros or inline functions to reduce overhead. An example macro for C++: #define ROW_COL_TO_INDEX(row, col, num_cols) (row*num_cols + col) Then you can use it as int COLS = 4; A[ ROW_COL_TO_INDEX(r, c, COLS) ] = 75; The overhead really affects when we do matrix multiplications which are of complexity O(n^3) or O(n^2.81) for Strassen's algorithm.
|
|
Nov 26, 2016 at 20:57 | comment | added | Ulrar | I like this solution a lot, is it applicable to 3 dimensions array too ? I'm thinking something like this : (x + m_width * y) + (m_width * m_height * z) | |
May 26, 2015 at 19:51 | comment | added | Jeff Wofford |
@Noein, I sort of sketched a solution without prescribing a particular one. More detail might look like: class Matrix { int* array; int m_width; public: Matrix( int w, int h ) : m_width( w ), array( new int[ w * h ] ) {} ~Matrix() { delete[] array; } int at( int x, int y ) const { return array[ index( x, y ) ]; } protected: int index( int x, int y ) const { return x + m_width * y; } }; If you straighten out that code it might make sense, and might shed light on the answer above.
|
|
May 22, 2015 at 11:35 | comment | added | Noein |
I don't quite get this. So array is something raw, not of the Matrix type? Then, how does m_width work here? And shouldn't it be static ? Also, if we're wrapping arrays anyway, why not implement an .at(j, i) member function instead? Great answer otherwise (especially the 'don't ptr to ptr please' bit; been having trouble with that in assignments so I agree).
|
|
Mar 18, 2015 at 15:42 | history | edited | Jeff Wofford | CC BY-SA 3.0 |
edited body
|
Mar 3, 2015 at 20:48 | history | edited | Jeff Wofford | CC BY-SA 3.0 |
deleted 3 characters in body
|
Mar 3, 2015 at 20:43 | history | answered | Jeff Wofford | CC BY-SA 3.0 |