1,105,402 Community Members

Limit on Integer 2-dimensional arrays?

Member Avatar
halluc1nati0n
Newbie Poster
24 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was trying out a 2-dimensional integer array on a UNIX box. I hit up a 'Segmentation Fault' on trying to run it.

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
	int matrix[2000][2000];
	int i, j;
	srand ( 1 );
	for (i=0; i<2000; i++) {
		for (j=0; j<2000; j++) {
		matrix[i][j] = rand() %1000;
		}
	}
        return 0;
}

But If I use an array of 10x10, everything seems to work properly. I was thinking of this : 'Did I hit up on the limit of the integer array?'

Do I need to use some other datatype? Uh, double/long/something of this sort ? I need to replicate a 2000x2000 matrix...

Member Avatar
halluc1nati0n
Newbie Poster
24 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

EDIT:

It looks like the problems' not with the data-type, rather, it's with the way the random numbers are getting generated :)

NEW EDIT:

Turns out I was wrong. I'm thinking the datatype has overrun somehow when trying to simulate a 2000x2000 matrix.

Member Avatar
thomas_naveen
Junior Poster
168 posts since Jan 2010
Reputation Points: 105 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
1
 

I have seen this problem before of large arrays on stack.

Just declare it as a global.

Member Avatar
mattjbond
Junior Poster
149 posts since Mar 2010
Reputation Points: 54 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
1
 

Looks like you may have blown your stack. Your compiler probably sets an initial stack size which your array size exceeds. You should check you compilers linker settings to look for stack size settings and experiment with these until you find a size which works.

Member Avatar
halluc1nati0n
Newbie Poster
24 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have seen this problem before of large arrays on stack.

Just declare it as a global.

Looks like you may have blown your stack. Your compiler probably sets an initial stack size which your array size exceeds. You should check you compilers linker settings to look for stack size settings and experiment with these until you find a size which works.

Thanks for the view guys. I'll let you know my progress ;)

Member Avatar
mattjbond
Junior Poster
149 posts since Mar 2010
Reputation Points: 54 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the view guys. I'll let you know my progress ;)

nae woz. vote up useful info.

Question Answered as of 4 Years Ago by mattjbond and thomas_naveen
Member Avatar
halluc1nati0n
Newbie Poster
24 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Defined my array globally and it worked! :)

Kudos to ya guys!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: