Hi, I am having an odd problem with my arrays. At present, I am creating the arrays in main and then passing them to other functions to do work on them and then returning their pointers so I can read them. In effect, this test works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <array>
using namespace std;
//constants
const int _100k = 100000;
const int _200k = 200000;
const int _300k = 300000;
const int _400k = 400000;
const int _500k = 500000;
int* sequentialFillArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = i + 1;
}
return arr;
}
int main()
{
// Initialize the 10 arrays:
int arr100k_random[_100k];
int arr100k_sequential[_100k];
int arr200k_random[_200k];
int arr200k_sequential[_200k];
int arr300k_random[_300k];
int arr300k_sequential[_300k];
int arr400k_random[_400k];
int arr400k_sequential[_400k];
int arr500k_random[_500k];
int arr500k_sequential[_500k];
// sequentially fill the 5 sequential arrays:
sequentialFillArray(arr100k_sequential, _100k);
//sequentialFillArray(arr200k_sequential, _200k);
//sequentialFillArray(arr300k_sequential, _300k);
//sequentialFillArray(arr400k_sequential, _400k);
//sequentialFillArray(arr500k_sequential, _500k);
// testing:
for (int i = 0; i < _100k; i++)
{
cout << arr100k_sequential[i] << endl;
}
cout << "I get here!" << endl;
system("pause");
return 0;
}
| |
The output of the test prints out 1 - 100,000 exactly as I need it to. If I uncomment the 200k array it still works, but once I uncomment the 300k the program crashes on load. No errors, it just doesn't work. I thought it might be a limitation of CODE::BLOCKS so I tried running the built application, but I run into the same problem.
Now, if I reduce the array elements by a factor of 10 in the constants, everything works so I know there isn't otherwise a problem with my code. Even here I run into a problem though.
The following code runs just fine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
[code]
#include <iostream>
#include <array>
using namespace std;
//constants
const int _100k = 10;
const int _200k = 20;
const int _300k = 30;
const int _400k = 40;
const int _500k = 50;
int* sequentialFillArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] = i + 1;
}
return arr;
}
int* randomFillArray(int arr[], int size)
{
// Need to fill the array sequentially before it can be shuffled
sequentialFillArray(arr, size);
// Now we shuffle the array elements at random
for (int i = 0; i < size; i++)
{
//int j = rand()%i;
//int temp = arr[j];
//arr[i] = arr[j];
//arr[j] = temp;
}
return arr;
}
int main()
{
// Initialize the 10 arrays:
int arr100k_random[_100k];
int arr100k_sequential[_100k];
int arr200k_random[_200k];
int arr200k_sequential[_200k];
int arr300k_random[_300k];
int arr300k_sequential[_300k];
int arr400k_random[_400k];
int arr400k_sequential[_400k];
int arr500k_random[_500k];
int arr500k_sequential[_500k];
// sequentially fill the 5 sequential arrays:
sequentialFillArray(arr100k_sequential, _100k);
sequentialFillArray(arr200k_sequential, _200k);
sequentialFillArray(arr300k_sequential, _300k);
sequentialFillArray(arr400k_sequential, _400k);
sequentialFillArray(arr500k_sequential, _500k);
// randomly fill the 5 random arrays:
randomFillArray(arr100k_random, _100k);
randomFillArray(arr200k_random, _200k);
randomFillArray(arr300k_random, _300k);
randomFillArray(arr400k_random, _400k);
randomFillArray(arr500k_random, _500k);
// testing:
for (int i = 0; i < _500k; i++)
{
cout << arr500k_sequential[i] << endl;
}
cout << "I get here!" << endl;
system("pause");
return 0;
}
| |
But as soon as I uncomment the for loop within:
int* randomFillArray(int arr[], int size)
it crashes again. Even with those few elements!
My computer is fairly powerful, so it should be able to handle what I am doing. I just don't understand why it can't seem to. Looking at the task manager, it's not like my CPU goes above 14% and my RAM remains at 40% with most of the system resources going to web browsers so I don't see how the program is breaing anything. I am running in Windows 10 if it helps.
What can I do to have all the array elements I need?