I'm trying to learn C++ by making a password generator, and when I was almost done, I had a warning (Warning C6262 Function uses '20044' bytes of stack: exceeds /analyze:stacksize '16384'. Consider moving some data to heap.) And since I'm a beginner at C++ and only now the basics, I don't know what to do.
Since I had 2 arrays in the function, I've tried to move them out of the main function, It did lower the number of bytes it was using (20100), it was still too high. I've tried to read Microsoft's errors guide, but I don't know about heaps or moving said data to the heaps in C++
Here's the code:
#include <iostream>
#include <random>
using namespace std;
class Randomer {
// random seed by default
std::mt19937 gen_;
std::uniform_int_distribution<size_t> dist_;
public:
/* ... some convenient ctors ... */
Randomer(size_t min, size_t max, unsigned int seed = std::random_device{}())
: gen_{ seed }, dist_{ min, max } {
}
// if you want predictable numbers
void SetSeed(unsigned int seed) {
gen_.seed(seed);
}
size_t operator()() {
return dist_(gen_);
}
};
string alphabet{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n','o','p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z' };
string specialchars{ ' ','!','#','$', '%', '&','(',')','*','+','-','.','/',':',';','<','=','>','?','@' };
Randomer rand1{ 0,2 };
Randomer randint{ 0,9 };
Randomer randalpha{ 0,26 };
Randomer randspecial{ 0,20 };
int main()
{
while (true) { //This loop is to restart the program if an invaild response is detected.
int i;
int lengthofpassword = 8;
cout << "Do you want to generate a password or to type in a existing password to make it stronger?\n\n(1 for generation, 2 for existing password)\n";
cin >> i;
int g = 0;
if (i == 1) {
cout << "\nWhat is the length of the password?\n";
cin >> lengthofpassword;
while (g <= lengthofpassword -1) {
//if (rand() == 0) {//numb
// cout << randint();
//}
//else if (rand() == 1) {//letter
// cout << alphabet[randalpha()];
//}
switch (rand1())
{
case 0:
cout << alphabet[randalpha()];
break;
case 1:
cout << randint();
break;
case 2:
cout << specialchars[randspecial()];
break;
default:
break;
}
g = g + 1;
//cout << "\n\n" << g << "\n\n";
}
cout << "\n";
system("pause");
return 0;
}
else if (i == 2) {
cout << "\n";
system("pause");
return 0;
}
else {
cout << "\n";
cout << "Invaild Response!\n";
}
}
// system("pause");
return 0;
}
Trying to condense it for stackoverflow didn't work. Although it hasn't broken yet and works as expected, I feel like I should learn this type of stuff now rather than later.