2 dimensional array-crash?

Hello, I'm very new to C++. I hope you guys can help me out.

I'm trying to declare the variable 'charmap':

1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()
{
    char charmap [1000][1000];
    system("PAUSE");
    return EXIT_SUCCESS;
}


This works fine. But when I try to declare

 
    char charmap [2000][2000];


the thing crashes: 'app.exe has stopped working'. I figured out it can't be a memory-related problem, it should take just about 3 megabytes.

What am I doing wrong? Is there an other way to make a data-map of 2000x2000 (or more?)

Many thanks!
well my devC++ compiler doesnt allow me to declare an array in array[][][] format. thats why i use the typedef command to creat matrices like
1
2
typedef float point[100];
point matrix[100];


this gives a 100x100 matrix
Last edited on
Thanks, but using that also makes it crash.

1
2
    typedef int point[1000];
    point matrix[1000];

'app.exe has stopped working'

Any ideas? I want an array bigger than 2000x2000.
It may be a memory error actually.
Even though it may be smaller than your memory size, arrays are contiguous in memory (meaning for your definition it needs 4000000 char sized blocks right next to each other).

I'm a beginner myself, but it seems plausible that this may be your problem.
If I'm not wrong, static arrays (such as the one above), and all static data, actually, are created on the stack. By creating one as large as that you caused a stack overflow and the OS complained.
If you need to allocate big arrays (I think the definition of big in this case is >=64K. Of course, it's system-dependent) allocate them dynamically.
 
char *charmap=new char[2000][2000];


EDIT: Okay, a friend just explained it to me. Those aren't technically static arrays, since they go out of scope when the function returns, but they are created on the stack.
Last edited on
helios, so is that a memory issue? I'm just wondering because I've personally allocated 10k by 10k arrays before with no problems on my own computer.
Like I said, it's system-dependent. I'm currently doing some testing on Win32, and the stack seems to be somewhere between 1 MB and 2 MB.

EDIT: The largest array that doesn't cause a run-time error and doesn't make main() return an error code in Win32 is 2,084,688 bytes (~1.988 MB) long.
Last edited on
Thanks all. So it isn't possible this way. Then what do you do if you want to make a game with a large map consisting of tiles? Eatch tile is occupied by something, a value. How is this done?
Thanks again, I hope you can help me further.
Well it's like we were saying, the problem isnt that your array is too big, it's just possible that your system's stack isn't big enough to handle it. What system are you using? Also, what compiler do you use?
Well, since he's creating the array in the stack, it is that his array is too large.

SanderLeander: When creating large arrays, use dynamic allocation.
Dynamic allocation will allocate memory in the heap right ? So how is that going to help ? Is the heap bigger than the stack ? I am sorry but i am new to memory management of c++ programs.
Not just "bigger". Much bigger. In fact, most of the memory installed in a modern computer (including virtual memory, which alone is already huge) counts as part of the heap.
Topic archived. No new replies allowed.