Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function exceeds stack size, consider moving some data to heap (C6262)

Tags:

c++

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.

like image 738
LunarHunter Avatar asked Oct 20 '19 21:10

LunarHunter


People also ask

How do I move a variable from stack to heap?

Moving something from stack to heap is basically allocating new object explicitly with new instead of declaring a variable. In your case you can replace four Randomer xxx variables with Randomer* xxx = new Randomer {...}, and deleting it at the end of the loop.

Is it safe to move data to the heap?

Consider moving some data to heap. Sorry, something went wrong. This is not an issue. Just something MSVC (and only MSVC) considers unsafe. There's no need to do anything with it. Sorry, something went wrong. Yes, it's indicated via MSVS Code Analysis... Sorry, something went wrong.

How many bytes does the function use on the stack?

Warning C6262 Function uses '40076' bytes of stack: exceeds /analyze:stacksize '16384'. Consider moving some data to heap.

How many bytes does the function use in c6262?

warning C6262: Function uses '37648' bytes of stack: exceeds /analyze:stacksize '16384'. Consider moving some data to heap.


2 Answers

Moving something from stack to heap is basically allocating new object explicitly with new instead of declaring a variable.

In your case you can replace four Randomer xxx variables with Randomer* xxx = new Randomer{...}, and deleting it at the end of the loop. Although there's not much sense in recreating PRNG within loop, and maybe you should just move Randomer objects outside of main().

like image 85
Vlad Avatar answered Oct 21 '22 17:10

Vlad


A std::mt19937 pseudo random number generator is actually a huge object and you are creating lots of them in your loop. What you should do is create one of them in the beginning and share it between you Randomer objects, e.g. by passing them a pointer in the constructor. (You could move them or your Randomer objects to the heap, but creating lots of them is just a waste of performance and there is no good reason to have more than one.)

like image 44
Knoep Avatar answered Oct 21 '22 19:10

Knoep