really big arrays?

Started by
16 comments, last by noVum 19 years, 9 months ago
For some reason this: int fractal_set[1024][768]; generates an immediate page fault at runtime using MS VCPP 2k3 and Win32 base code. But the same thing does not create any problems if I use it in a console application... whats going on? and how do I fix it?
Advertisement
Quite simply, the stack is a very limited resource. So you can't allocate large arrays on it. As was already said in the previous thread before you deleted it, use new/malloc or std::vector.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Stack overflow (1MB by default in VS, your array is precisely 3MB). Either make a bigger stack (not a good idea) or dynamically allocate your array: int *fractal_set = new int[1024*768]; or std::vector<int> fractal_set(1024*768);
No, you are not getting "2D indexing" - get over it (index = x+768*y)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You are blowing the stack. Try allocating it dynamically with new or free and see how it goes.

[edit] Wow, almost simultaneous answers.
Really big arrays (or any other large data type) should be allocated on the heap (using new, malloc or whatever your allocation method of choice is) instead of the stack. You can also cover up the symptoms by increasing the stack size in the linker options, but this tends to scale poorly.
big arrays you typically want to put on the heap as it has much more space:

int *fractal_set = new int[1024*768];


sadly you cannot nicely access multi-dimensional arrays that are declared on the heap. so you have to access it by:

//this is the equivalent of [x][y]fractal_set[y*768 + x];


if you really want to keepit on the stack, you can increase the default stack space size that you are given somehow... though i forget.

-me
Is the 3MB array on the heap or the stack?
Can you post some minimal amount of code which reproduces the problem?
It's not the declaration that causes the problem btw, it's the code that accesses it.

OMG lets just all post at once - LOL!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
It's not the declaration that causes the problem btw, it's the code that accesses it.


Actually, it is the declaration that causes the problem.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Assume an int is 4 bytes; 4*1024*768 is over 3000000 bytes. That's 3MB. The stack cannot take that much. [smile]

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Hmm, VC7, 7.1, and 8 don't even let that compile. *shrug*. Use the heap.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

This topic is closed to new replies.

Advertisement