How is a 2D array stored in memory?
I thought about the following approach, where rows are stored as contigous blocks of memory.
|_________||_________|________|________|...|_________|
The elements are accesed like (i,j) -> n*i+j, where n is the dimension of the matrix (supposing it is nxn).
But what if i want to add a new column to it? I'd have to update each (n+1)th element in each row and also shift them to right, but that's too computationally expensive.
Another option would be to copy the matrix in a new location and update the rows with the new column's elements on the fly. But that's not too efficient too if the array is big.
And finally the third option i thought of is to allocate a fixed amount of memory for each row and when i add a new column i don't have to shift the rows to right.
I can't have gaps in the memory, so all blocks must be contigous.
I'm not asking for a C implementation using pointers and the actual RAM memory, i'm just curious about a theoretical approach of storing a dynamic 2d array in memory so as it is easy to append new rows or columns to it.
Are there other more efficient approaches?
A two-dimensional array is a tabular representation of data in which the elements are kept in rows and columns. M X N elements are arranged in M rows and N columns to form a two-dimensional array. To access any element in a two-dimensional array, provide the element's row and column location.
We will have to define at least the second dimension of the array. The syntax to declare and initialize the 2D array is given as follows. The number of elements that can be present in a 2D array will always be equal to (number of rows * number of columns). Example : Storing User's data into a 2D array and printing it.
Create an array to which you want to store the existing array with the same length. A 2d array is an array of one dimensional arrays therefore, to copy (or, to perform any operation on) the elements of the 2d array you need two loops one nested within the other.
Memory is allocated contiguously when a two-dimensional array is declared as follows: int matrix [ 2 ][ 5 ] = {{ 1 , 2 , 3 , 4 , 5 },{ 6 , 7 , 8 , 9 , 10 }}; However, when we use a function such as malloc to create a two-dimensional array, there are variations in how memory can be allocated.
If you know you're creating a 2D array that you will expand, one approach would be to allocate more size than you need in each dimension. Keep track of the actual size and the allocated size, and when the actual size exceeds what is allocated, do the following:
This would be a 2D extension of a common technique for allocating dynamic 1D arrays.
If you need the arrays to expand and be contiguous in memory, one way to achieve this is to simply use a 1d array and 'fake' the 2nd dimension.
If your initial 1d array has more space than all your 2d arrays requires, it wouldnt require moving in memory (potentially avoiding gaps). However depending on how you implement it, an insert that makes one of the sub arrays grow may requires shuffling later elements down (you could also have gaps in your array, but I believe this violates your no gaps requirement).
If you actually do need 2 dimensions, then greg's answer is the way to go. If you know the size of your data to begin with, it makes it much easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With