1

To access a matrix we can use a double pointer, and then use 2d arrays with index i, j. for rows a column. For example the element (1,2) could be addressed as A[1][2].

Another method is to use single pointer, and then use 1D array, but do certain index calculation to correctly address a particular item in matrix.

I want to know the pros and cons of these two methods for accessing the matrix, especially in terms of performance. My understanding is that, the method involving single pointer may incur performance penalty because it has to do additional Calculation of index, which is not required in double pointer. Is my understanding correct ? What are the other pros and cons?

I am assuming that arrays are allocated dynamically.

  • In the "double pointer method" you don't actually have a 2D array. – user253751 Oct 06 '16 at 05:48
  • @immibis ok. but do you think 1D array will have significant overhead due to index calculation? – Afnan Abdullah Oct 06 '16 at 06:05
  • It's pretty much the same thing. As long as you make it equivalent. That is, allocating a single large block and treating it as if it was a 2D array. Whether you do that with a row index of pointers or just calculate indices is more a matter of preference. If you can't actually measure the difference in performance of these two methods, then you don't need to consider it so do whatever is easiest. If you really care about the difference, then try to measure it. – paddy Oct 06 '16 at 07:36

0 Answers0