I have code that looks like this:
#include <array>
#include <cstdint>
#include <exception>
#include <iostream>
#include <utility>
template<typename T, size_t size>
auto randomly_populate_array(std::mt19937& gen, T lower, T upper) {
std::array<T, size> arr;
std::uniform_int_distribution<T> dist{ lower, upper };
for (auto& e : arr) { e = dist(gen); }
return std::move(arr);
}
int main() {
try {
std::random_device rd;
std::mt19937 gen{ rd() };
constexpr std::size_t arrSize{ 10000 };
constexpr uint32_t lower = 1, upper = 100;
std::array<uint32_t, arrSize> A = randomly_populate_array<uint32_t, arrSize>(gen, lower, upper);
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
And this seems to run fine, I have other code to print samples from this array, except in my full program I'm generating 4 of them of the same size...
Now when I crank up the value from 10,000
to say 20,000
, 50,000
or 100,000
as I'm trying to make a profiler class for time performance... Visual Studio wants to throw an unhandled exception... The code compiles and builds, but it is throwing a runtime exception...
Here's the message when I change arrSize
to 20,000
:
Exception thrown at 0x000000013FB40698 in Simulator.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x00000000001B3000).
Unhandled exception at 0x000000013FB40698 in Simulator.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x00000000001B3000).
For the life of me, I can not figure out why... I should be able to create an array with 1,000,000
entries even 100,000,000
on my machine... I have a 64-bit CPU and 8GB of Ram.
And I have already read this Q/A and it doesn't answer or resolve my question... I'm not using dynamic
allocation here. And creating 100,000
32-bit values should be fine for my system... that should be 400,000 bytes which is what about 50Kb... so any help into resolving this issue is already appreciated.
User contributions licensed under CC BY-SA 3.0