Stack Overflow

Help!!.

The program worked well (with init() in place) until it got bigger, after compling, it generates stack overflow error, the debug shown

"First Chance exception in ResistorCalc.exe (MFC42D.DLL):- 0xC0000FD: Stack Overflow"

I admit I use global variable but well within the dialogue classes (all public).

I am using Visual Studio V6 and working on Win98 (will plan to upgrade later to Win2000).

When replace init() with routine program, the problem goes away. Why it failed now when program got bigger or more complex?.

Please advise how to fix this and advise if I broken any visual programming rules.

Many Thanks

The programs:-

BOOL CResistorCalcDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     // Add "About..." menu item to system menu.

     // IDM_ABOUTBOX must be in the system command range.
     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
     ASSERT(IDM_ABOUTBOX < 0xF000);

     CMenu* pSysMenu = GetSystemMenu(FALSE);
     if (pSysMenu != NULL)
     {
          CString strAboutMenu;
          strAboutMenu.LoadString(IDS_ABOUTBOX);
          if (!strAboutMenu.IsEmpty())
          {
               pSysMenu->AppendMenu(MF_SEPARATOR);
               pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
          }
     }

     // Set the icon for this dialog.  The framework does this automatically
     //  when the application's main window is not a dialog
     SetIcon(m_hIcon, TRUE);               // Set big icon
     SetIcon(m_hIcon, FALSE);          // Set small icon
     
     // TODO: Add extra initialization here
//=================================================================================

     m_CListResult.InsertColumn(0,"Value1",LVCFMT_LEFT,70);
     m_CListResult.InsertColumn(1,"Value2",LVCFMT_LEFT,70);
     m_CListResult.InsertColumn(2,"Result",LVCFMT_LEFT,70);
     m_CListResult.InsertColumn(3,"Tol",LVCFMT_LEFT,50);
     m_Required_Value=1;
     m_Initial_Value=1;
     m_Lower_Limit_Range=1;
     m_Upper_Limit_Range=100e6;
     m_Tolerance=1;
     init();                 <====== cause stack overflow error.
     UpdateData(false);
...


riscyAsked:
Who is Participating?
 
SalteConnect With a Mentor Commented:
There's no chance that stack overflow can be caused by global data.

Stack overflow is caused by infinite recursion. When you get stack overflow, check the stack trace of your debugger, if you see a recurring sequence of function calls that's a good hint it is the infinite recursion that causes the stack overflow.

Stack overflow is typically caused by functions that are called without doing any "progress".

The way that recursion works is always that you "gain" something in each step of the recursion, consider the simple factorial function:

int fact(int n)
{
   if (n == 0)
      return 1;
   else
      return n * fact(n-1);
}

Here you always get closer towards 0, if you start out with a number n == 5 you see that it returns 5 * fact(4). Since 4 is less than 5 you have gained some progress. Finally, when the value reaches 0 you have a step without recursion that simply return the value 1 and from then on the recursion is done and the function will stop the recursion.

Infinite recursion can thus occur by making two errors:

1. Fail to provide progress...

while n! == n * (n-1)! it is also true that (n+1)! == (n+1)*n! and so you can write:

n! == (n+1)!/(n+1).

so you could write:

int fact(int n)
{
   if (n == 0)
      return 1;
   else
      return fact(n+1)/(n+1);
}

Mathematically this is also correct but instead of coming closer to 0 for each step you even move away from it. This function will therefore cause stack overflow unless you give it a value exactly equal to 0.

Similarly we could have:

int fact(int n)
{
   if (n == 0)
      return 1;
   else
      return fact(n);
}

Also this has no progress and so it will cause infinite recursion and stack overflow unless n happen to be 0. The reason is lack of progress.

2. No stop test.

This is another way recursion can fail, if we had the following function:

int fact(int n)
{
   return n * fact(n-1);
}

We will also have stack overflow. We simply do not have any test for when to stop the recursion, so it will go on without stopping.

Now, stack overflow may come in more complicated situations than the above but they all boils down to the two causes above. You can have a funciton A that calls a function B and that function calls a function C etc which in turn calls function A and if there is no progress between the two calls of function A you will end up in stack overflow. If the function do not have a test for when to stop the recursion you will also have stack overflow.

Unless you run on some old MS-DOS system there is no way that a program can get stack overflow by a regular program simply having too many (valid) function calls. The stack is fairly large and is large enough for all applications. If an application has stack overflow it is invariably not the stack that is too small but infinite recursion. A bigger stack won't help you, it will just delay the time it takes to get stack overflow.

Of course, there is one other way to get stack overflow, and that is allocating an infinite amount of heap data. If you do:

while (true) {
   int * p = new int[30000];
   func();
}

You will get in stack overflow fairly fast. This isn't because the stack is too small per se but because the stack and the heap share storage space and allocating huge amount of stack space and not freeing it will eventually eat from the stack space. However, initially the distance between the stack and the heap is fairly large and so unless you really grab huge amount of heap space without freeing it you shouldn't get in trouble.

Global data area are stored elsewhere in memory and will NOT affect the stack or the heap. So if you declare global variables - even if they are big - it shouldn't cause any stack overflow of any kind.

// in global scope (outside of functions)
int global_arr[100000];

or if in a function or in a class a 'static' declaration will also allocate the space in the same area.

such a declaration cannot cause stack overflow.

If you declare it inside a function however, it might because then it will use space on the stack for the array:

void func()
{
   int arr[100000];

   // code here...
}

will probably cause stack overflow, but such an array is simply too big for a local array.

Alf
0
 
captnoordCommented:
I think you'l better change the name of the funcion becouse i think that's one of the reservered funcion of c++ and is it possible to see the source of the function
greets captnoord
0
 
jkrCommented:
>>   init();                 <====== cause stack overflow error.

So, why don't you post the code of 'init()'?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
SalteCommented:
Hmm....I am guessing here but is it possible that init is something like this:

void init()
{
   bool first_time = true;

   if (first_time) {
     first_time = false;
     call_some_other_func();
   }
}

and then call_some_other_func will eventually call init() again. The idea here is that the variable first_time should remember if you have ever called init() earlier and then only call it once. This code would work if the variable first_time had been a static variable but as written above it won't and it will cause infinite recursion.

Changing to:

   static bool first_time = true;

will fix that since then all the calls to init will share the variable first_time and once one function set it to true another call to init will do nothing.

Another error might be that it is static but it is set to false at the wrong place:

void init()
{
   static bool first_time = true;

   if (first_time) {
     call_some_other_func();
     first_time = false;
   }
}

Here the variable isn't set to true until call_some_other_func() returns, so if call_some_other_func in turn call init() again we have infinite recursion once again.

If you really need to differentiate between 'init not done', 'init in progress' and 'init complete' you should have three values and the first_time should check for 'init not done':

void init()
{
   enum {
     init_not_done,
     init_in_progress,
     init_complete,
   };

   static int init_status = init_not_done;
   if (init_status == init_not_done) {
      init_status = init_in_progress;
      call_some_other_func();
      init_status = init_complete;
   }
}

One problem here is that if init_status is not init_complete you probably should have done something else.

Also, the variable first_time or init_status or whatever you call it doesn't have to be a static variable in the function but static variable in the class, that has the same effect but other functions than init() can read/set the variable.

In particular you should be careful if you need A to be initialized before B which in turn must be initialized before C. If C then need A to be initialized first you're in trouble. So figure out what to be initialized first and in what order is here important and then write the init function accordingly.

Alf
0
 
riscyAuthor Commented:
Good news

I found the recursive statement deep in the program, which cause function call around 5 time per loop, this is fixed.

Many thank for very useful response, I look forward to continue learning Visual C++.

I changed init() to Startup_init() as init() is too seems generic.

Once again thank you all.

Riscy
0
 
SalteCommented:
If you are happy with the answers given you should close this question.

If you feel you managed well without the answers you can post a message in community support and ask to get the points back for this question. You can then spend those points on other questions.

If you feel that one answer helped you a lot you can accept that answer and give that expert the points.

If you feel that more than one answer helped you you can again post to community support and ask that the the points should be reduced so that you can accept this answer for one of the experts and then post a "points for <EXPERT>" posting to give those you think should get points as well. You must point one such "points for <EXPERT>" for each expert and typically you put the name of the expert instead of "<EXPERT>" so if you think jkr helped you but you accepted someone else here you can post a "Points for jkr" and when he respond to that message you can accept that message to give him the points.

In either case, don't leave the question open if you are done with it.

Alf
0
All Courses

From novice to tech pro — start learning today.