StackSize

 

Hi All,

When setting the compilation options there is a setting for stack size. I'm quite familiar with the concept of stack size (I'm from a technology background) but am surprised to find no posts about this on the forum and documentation gives nothing meaningful.

Any ideas as to when we may need to use more than the default stack size? Is there a particular error that is thrown when we run out of space on the stack?

Seems odd to have this option and no info on usage of it.

Stewart

 
There are some stack related error messages, see 4003, 4004 & 4005
 
stewart:

Hi All,

When setting the compilation options there is a setting for stack size. I'm quite familiar with the concept of stack size (I'm from a technology background) but am surprised to find no posts about this on the forum and documentation gives nothing meaningful.

Any ideas as to when we may need to use more than the default stack size? Is there a particular error that is thrown when we run out of space on the stack?

Seems odd to have this option and no info on usage of it.

Stewart

You may need more stack if you are programming like this recursive horror EA (it is only for testing):

int start() {
   callRecursiveFunction(0, 1000);
   Print("return from start()");
   return(0);
}

int deinit() {
   Print("deinit() called");
   return(0);
}

void callRecursiveFunction(int level, int maxLevelToTest) {
   Print("level = " + level);
   if (level < maxLevelToTest) {
      callRecursiveFunction(level + 1, maxLevelToTest);
      int lastError = GetLastError();
      if (lastError > 0) {
         Print("level = " + level + ", lastError = " + lastError);
      }
   }
   Print("return: level = " + level);
   return;
}

Works as expected, if you write 10 instead of 1000.

I tested with 1000, and there was no error code, just stoped working after 369 level of recursive call, and did not start()-ed at next tick.

But using #define stacksize [big enough], the result was not printed correctly. I think it is a dark corner of MT and should not be used.

 
erzo:

But using #define stacksize [big enough], the result was not printed correctly. I think it is a dark corner of MT and should not be used.

should be

#property stacksize 123456

where 123456 is just some "random" number.

It is not at all clear what the default value is, and this is the first time I have heard of it!

 
stewart:

... but am surprised to find no posts about this on the forum ..

https://www.mql5.com/en/articles/1544

https://www.mql5.com/en/forum/102262

https://www.mql5.com/en/forum/111089

https://www.mql5.com/ru/code/7107


the trick here is the google search string "site:mql4.com stacksize"

 
dabbler:

should be

#property stacksize 123456

where 123456 is just some "random" number.

Yes, it really works fine, thanks for your correcting.

Only the printing queue joked me again, but finally I got the correct test result, by writing a Sleep(100) before every Print().
 

Many thanks guys.

I'm guessing the (randomly chosen) size of 123456 is bytes. This would give a stack size of 123kb which should generally be big enough for 1000 of the recursive iterations in the program above. As a comparison, the default stack size in Visual C++ is 1Mb. So, the stack size being measured in bytes in this case would seem a fair assumption.

I guess the important thing is that a stack overflow terminates the program with future calls to start() no longer being made (or at least that is how it behaves in the example above). An error is given in the Experts Log being 'stack overflow'. Uninit() is called when the EA is unloaded.

Thanks again.

Reason: