Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Huge Multidimensional Arrays in C++
    text
    copied!<p>I'm designing a game in C++ similar to Minecraft that holds an enormous amount of terrain data in memory. In general, I want to store an array in memory that is [5][4][5][50][50][50]. This isn't bad since it amounts to about 100mb of virtual memory since my structure will only be about 8 bytes. </p> <p>However, I'm having trouble figuring out the best way to handle this. I do want this to be in virtual memory, but obviously not on the stack. And I keep making the mistake some how of creating this array on the stack an causing a stack overflow. What I would like to do is below. This is just code that I threw together to give you an example of what I'm doing, I have code with correct syntax on my machine, I just didn't want to clutter the post.</p> <pre><code>typedef struct modelBlock { // Information about the blocks } BLOCK; typedef struct modelGrid { bool empty; BLOCK blocksArray[50][50][50]; } GRID; class Parent { Child* child; Parent(void); } Parent::Parent() { Child c; child = &amp;c; } class Child { GRID grids[5][4][5]; } </code></pre> <p>However, every time I do this, I cause a stack overflow (appropriate web site choice right?). I played with using pointer based arrays, but I had a lot of trouble with data being lost outside of its scope. </p> <p>If anyone could give me some insight on how to get my data to store on the heap instead of the stack, or if I should use some other way of creating my array, I'd really appreciate the help. I'd like to avoid using vectors because of overhead, though I'm not sure how substantial it is.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload