I have the code listed below and it reports a stack overflow when I run it.
I use pass by value to showTest()
.
What I expect is that it will make a copy of the Test
struct to the stack (pushed to the stack), then at the end of the function call the Test
struct will be released (popped off of the stack).
So I make a call for three times. It is supposed to push onto the stack and pop off at the end of each function call.
I don't see any stack issue if it pushes on and pops off of the stack each time the function is called.
However, when I run this code, it reports a stack overflow exception on the first line of main
. (I use Visual Studio 2017.)
If I remove one of the showTest()
function calls then I can get it work.
Any feedback would be highly appreciated.
#include <iostream>
struct Test
{
static int Userid;
int data[100000] = { };
Test()
{
++Userid;
};
};
int Test::Userid = 0;
void showTest(Test i_myint)
{
std::cout << "test" << std::endl;
}
int main()
{
Test *pint = new Test();
showTest(*pint);
Test *pint2 = new Test();
showTest(*pint2);
Test *pint3 = new Test();
showTest(*pint3);
return 0;
}