****New to C!****
I am running Dev-C++ 4.9.9.2 on Windows 7 (64 bit build)
My computer has 39GB of Physical Memory.
I am trying to create a large two dimensional array. I have already created code that tells me how many dimensions it has, and how many items are in each dimension.
As an example, let’s say the array is two dimensional: One million items long, and 6 wide:
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
...and on to one million items.
I have tried:
float MyArray[1000000][6];
but this crashes Dev-C. It seems to fail when I try to initialize an array larger than:
float Myarray[86486][6];
I imagine I am experiencing a “stack overflow” which amuses me since that is the name of this site.
I have been digging around and it seems I need to use malloc to help C understand how much memory to reserve. I have seen good examples of how to use this to set up a 1 dimensional array, but I would very much appreciate example code of how to set this up with a 2 dimensional array.
I have seen the example here: Initializing a large two dimensional array in C
But I’m afraid I am too much of a beginner in c to understand the brief explanation.
As background: I am coming from python where you can make an array of (almost) any size or dimension by just declaring MyArray=[] and then filling it with whatever you want.
Thank you!
Yeah, you’re running up against the limit of the size of an individual stack frame.
Here’s one approach:
Why this works:
The subscript operation
a[i]is interpreted as*(a + i); that is, we compute the address of thei‘th element (not byte) aftera(the base address of the array) and dereference it. SincemyArrayis a pointer to a 6-element array offloat,myArray[i]gives us the address of thei‘th 6-element array offloataftermyArray.The advantage of this approach is that the memory is allocated in a contiguous chunk, and you can subscript
myArraylike any 2-d array.