Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very Large Arrays and Stack Overflow

Status
Not open for further replies.

Foj

Programmer
Mar 21, 2003
2
US
I need to create a very large array of about 10000x10000 ints. Of course I get a stack overflow. I get a stack overflow starting as low as 1000x1000. I am wondering if it is possible to overcome this problem. Thank you for any help you can offer.

 
My programing teacher and all [good] programers told me that if you have large arrays in your program something is wrong with the structure.

Maybe try breaking into smaller arrays size 500 each and try to go from there.
 
I ended up creating an array of pointers to more arrays... best of all it works just as if it was a 2d array. Thanks for your suggestion.
 
The stack size is limited to a relatively small amount in comparison to the heap. Most compilers will allow you to set the stack size but still within limits. The heap size is based on your OS. For example in Windows NT/2000 etc I think it is like 4 gig.

In general you don't want to put any sizeable data structures on the stack. Use the heap for it.

-pete
 
Use static or global arrays or allocate them on the heap instead of local arrays.

// allocated statically during compilation
int global_array[1000000];

YourClass::YourMethod(void)
{
// allocated dynamically on the stack
int local_array[1000000];

// allocated statically during compilation
static int static_array[1000000];

int *pointer_to_array;

// alocated dynamically on the heap
pointer_to_array=new int[1000000];
if(pointer_to_array!=NULL)
{
...
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top