| Giulio (5) | |
|
Hi everybody, I apologize if the question is really easy... but I cannot figure out why I get an error message by my operating system (not the compiler, the program works if the array is say [50][50]) double x[1000][1000]; for (int i=0; i<100; i++) { for (int j=0; i<100; j++) { x[i][j] = i+j; } } It is really weird since other programs like Matlab handle big 1000x1000 matrices without difficulties... I am writing some code where I should make use of some 2-dimensional arrays of that size (ie.[1000][1000]) but I cant make it work...(though it works perfectly if I use 50x50 arrays) Where is the problem?? Thanx giulio | |
|
|
|
| kempofighter (1120) | |
| Well your for loops are wrong since they only loop to 100 instead of 1000. What is the error that you are getting? What compiler? What OS? 1000*1000 = 1000000 which is 1 million. That is pretty big for a stack array but I don't know how big your stack is. | |
|
|
|
| helios (10126) | |
|
Yeah, that's probably an 8 MiB array. Way too large for just about any stack. http://www.cplusplus.com/doc/tutorial/dynamic/ | |
|
|
|
| Giulio (5) | |
|
I am sorry the loops were meant t go up to 1000 but that s not quite the point since even the program double x[1000][1000]; produces the same error....!! The compiler is Dev-C++ and the error I get is not from the compiler but from Windows itself... I get the usual message "an error as occurred with Untitled1.exe" and at the end I have the option to Notify the problem to Microsoft and I choose "Dont's send"... let me ask a question then...how come Matlab can easily handle a 1000x1000 matrix whereas c++, which is much more powerful, cannot do such a thing? | |
|
|
|
| helios (10126) | |
| Because it's allocating it in the heap. Read the link I posted. | |
|
|
|
| mspy2plus (28) | |
| All of the array things, a million doubles, are on the stack. The reason Matlab handles them is because of the way it is coded. The stack is way too much. | |
|
|
|
| wachtn (183) | |
|
why are you using doubles? Try short int unless your numbers are larger than 65535 or outside the range of -32768 to 32767. Would that cut the memory requirements down 75%? | |
|
|
|
| Giulio (5) | |
|
ok i am starting to understand... thank you i am using double bc in the code i am writing those are decimals... actually they are probabilities, ie reals bw 0 and 1... you think there is a better type i can use..? btw... i think that if i use the vector class in the std lib i can handle much bigger vectors in fact the command double x[1000000] //one million causes the program to fail...whereas the command vector<double> x(1000000]) //one million is ok... am i right? | |
|
|
|
| wachtn (183) | |
|
http://www.cplusplus.com/doc/tutorial/variables/ scroll down to "Fundamental data types" | |
|
|
|