Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:16:16+00:00 2026-05-27T21:16:16+00:00

How is a 2D array stored in memory? I thought about the following approach,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:16:17+00:00Added an answer on May 27, 2026 at 9:16 pm

    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:

    • double the size of the allocation
    • copy all the data from the old array to the new one
    • free the old array

    This would be a 2D extension of a common technique for allocating dynamic 1D arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you access elements of an array using array[i], I thought that C++ would
I have a very large graph stored in a single dimensional array (about 1.1
how do I retrieve the byte array stored in an integer column in my
An array of ints in java is stored as a block of 32-bit values
There is an array of user states stored in the session. This works: <?php
A cancer CT picture is stored inside a unsigned short array (1-dimensional). I have
I have CSV text thats stored in the session array as csv. The lines
Overview: I have an array of 20 byte strings that needs to be stored
here's the stored procedure i wrote.In this proc p_subjectid is an array of numbers
How can i display a list of images stored in an array as list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.