954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

large 2d array

Hey, i just got alil problem. Im trying to create a 2d array with a size of [1000][1000]...

double variable[1000][1000] = { 0 };

its compiles fine but when i run the program, it just crashes...is the array too big?? the maximum i can get is [300][300]...any ideas??

geez85
Newbie Poster
4 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

> double variable[1000][1000] = { 0 };
In other words, about 8MB in size.
You're basically blowing away the stack, which depending on your machine could be as little as say 16K in size.

Depending on your compiler, you can specify the initial stack size as being something else.

Since 300*300*8 is about .75 Meg, I'd guess your stack size defaults to 1MB.

1. Make it global - move the array outside of the function it is in.

2. Make it static, as in
static double variable[1000][1000] = { 0 };

3. Allocate it dynamically.

double (*variable)[1000] = new double[1000][1000]; // c++ way
double (*variable)[1000] = malloc ( 1000 * sizeof(*variable) );  // C way
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

sweet thanks it wrked...really appricate it

geez85
Newbie Poster
4 posts since Apr 2006
Reputation Points: 10
Solved Threads: 0
 

Hey, i just got alil problem. Im trying to create a 2d array with a size of [1000][1000]...

double variable[1000][1000] = { 0 };

its compiles fine but when i run the program, it just crashes...is the array too big?? the maximum i can get is [300][300]...any ideas??


there is a limit of array allocation, the size u are trying causes stack overflow of the function, beacuse ur array can't fit into the memory allocated to th function stack.
there is a limit of 8MB on the maximum size of objects, due to internal compiler implementation limits.
You should use malloc() to create large arrays instead.

dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You