Why this stackover flow is happening where as I am using fflush and free in my code.
Please help me.
using namespace std;
struct abc{
int x;int y;
}abc;
int _tmain(int argc, _TCHAR* argv[])
{
struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
xyz->x = 5;
printf("%d\n", xyz->x);
//system("pause");
free(xyz);
// xyz = NULL;
fflush(stdout);
_tmain(NULL, NULL);
return 0;
}
Edited Code:
xyz:
struct abc *xyz = (struct abc *) malloc(sizeof(struct abc));
xyz->x = 5;
printf("%d\n", xyz->x);
//system("pause");
free(xyz);
xyz = NULL;
fflush(stdout);
goto xyz;
You are unconditonally calling the
_tmain()function from your_tmain()function, causing infinite recursion. Every call needs to allocate space on the stack (which is never freed), causing your StackOverflow exception. Take a look at e.g. Wikipedia too fully understand the problem.Apart from that, calling the
main()function from your own code is ususally not a good idea, as its forbidden by the standard.Why would you need to do that?