Sign in to follow this  
Dovyman

really big arrays?

Recommended Posts

Dovyman    277
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?

Share this post


Link to post
Share on other sites
Washu    7829
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.

Share this post


Link to post
Share on other sites
Fruny    1658
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)

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
Palidine    1315
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

Share this post


Link to post
Share on other sites
iMalc    2466
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!

Share this post


Link to post
Share on other sites
Fruny    1658
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.

Share this post


Link to post
Share on other sites
Dovyman    277
Quote:
Original post by Washu
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.


Wow you respond fast.. thanks guys. It should work fine now. (I originally had a "oh wait a second I'm a dumbass" moment, and then realized it still didn't work :-/ )

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Fruny
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.
You may well be correct, but at the time you posted that post we still had not been told that it was on the stack. Though many seem to have jumped to that conclusion. For all we knew it could have been a global, and it could have been other code causing the problem right?

I guess I did kind of automatically jump to the opposite conclusion though, my bad.

Share this post


Link to post
Share on other sites
Samith    2460
Quote:
Original post by Fruny
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)


You can do this though:

int main(void)
{
float** array;
array = new float*[20];

for(int i = 0; i < 20; i++)
array[i] = new float[20];

for(int y = 0; y < 20; y++)
{
for(int x = 0; x < 20; x++)
{
array[x][y] = x + y*20;
std::cout << array[x][y] << std::endl;
}
}

for(i = 0; i < 20; i++)
delete[] array[i];

delete[] array;

return 0;
}




and get 2d indexing :D (I don't know if I deleted everything right, though).

EDIT: Thanks guys, now I've got the delete right :)

Share this post


Link to post
Share on other sites
id recommend using a nested vector.


vector< vector<int> > fractal_set;

fractal_set.resize(1024);

for(int i = 0; i < 1024; i++)
fract_set[i].resize(768);


now you can access the array as fractal_set[y][x] if you want. (you can set it up for [x][y] too). i might have screwed up the way i set it up, but this is the gist of it. its late and i dont feel like checking my code =).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this