0

When a 2 D array is declared statically, we get a huge contiguous chunk of virtual addresses. These addresses need not be mapped to contiguous memory addresses in physical addresses. Does the same happen for 1 D array? Are the addresses of individual elements in a 1D array contiguous in the physical address space or can they be mapped to addresses on different physical pages?

Sahana ys
  • 351
  • 1
  • 3
  • 8

1 Answers1

1

Virtual address space has nothing to do with arrays in C programming (I assume you use C).

When you have a 2D array, the last dimension is just virtual. Behind the scenes, the compiler has one long 1D array, and when you want to reach one specific node, it will do a quick multiplication of the first index and the run length of the column before adding the second index.

Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19
  • Yeah. But those are virtual addresses right? The actual location where it will be stored i.e., the physical addresses depends on the vitual to physical address mapping by the TLB. So, in such a situation, will a 1D array elements be mapped to contiguous memory locations or can they be mapped to physical addresses on different pages? – Sahana ys May 07 '16 at 17:25
  • No. Virtual adresses and TLB are not needed for 2D arrays. From the memory view, a 2D array and 1D array looks the same. – Stian Skjelstad May 07 '16 at 19:38
  • Virtual adresses and TLB are not visible for the user process. That all happens in the operating system, allowing each program to have uniqe memory, and not needing to know about swap, etc. – Stian Skjelstad May 07 '16 at 19:40