0

I know a similar question is already asked here for example:

Malloc a 2D array in C

However, my question is not how to create one but rather if I should prefer to use for a mathematical 2D matrix a "real" 2D array (with pointers of pointers) or rather a flattened 1-dimensional array with proper indexing.

bilalj
  • 123
  • 3
  • 8
  • From a pure mathematical perspective, it really doesn't matter. – Some programmer dude May 08 '22 at 07:40
  • Yes, that's true, but I meant rather from a programming perspective. – bilalj May 08 '22 at 07:41
  • In most cases that doesn't really matter either. For a good answer you need to give us much more details, like what is the assignment or exercise you're tasked with solving? What are its requirements? Its limitations? Please [edit] your question to elaborate. – Some programmer dude May 08 '22 at 07:49

1 Answers1

0

I think the only case it can be important is when you are doing operations that depends on the neighbors of the matrix. In this case, using a 2D matrix is a bit better because it avoids cache misses.

This is specially important for problem solutions that use dynamic programming optimization .

I believe it can also be important for image processing, where many operations are applied in a rectangle of pixels.

John Doe
  • 547
  • 5
  • 15