|
Natza Mitzi wrote: MS are not letting you to catch a StackOverFlowException. I wrote a evaluator/interperter using C# 1.1 . The language supports recursion about a year ago I moved to C# 2.0 and only now we found out that a too deep of a recursion throws the application without any notice. It just goes away. A simple factorial like:
fact(n)(if(n == 0,1,n * fact(n-1)))
vaporizes the application at fact(46)
That's how the CLR works. In fact even a tail recursive language like Scheme will fail on that.
You have 2 options:
1. Increase stack space.
2. Rewrite to use iteration or tail-recursion.
|
|
|
|
|
Natza Mitzi wrote: Guys stay away from null pointers!!
And from people posting on the wrong forum.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: people posting on the wrong forum
Geeze, another one
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Yes, another one.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Seems like a rash of 'em lately
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I think his point might have been a coding horror on the part of Microsoft.
He who asks a question is a fool for five minutes. He who does not ask a question remains a fool forever. [Chineese Proverb]
Jonathan C Dickinson (C# Software Engineer)
|
|
|
|
|
CPallini wrote: And from people posting on the wrong forum.
That throws WrongForumException and UraNoobException, neither of which are derived from StackOverFlowException.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
You either have problems understanding the issue or just happy to jump in with someone that can not understand the issue.
Natza Mitzi
|
|
|
|
|
Natza Mitzi wrote: Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default
yes, that is unfortunate. On the other hand, recovering from a stack overflow
in a reliable way is probably rather tricky.
Natza Mitzi wrote: A simple factorial like:
fact(n)(if(n == 0,1,n * fact(n-1)))
vaporizes the application at fact(46)
That is crap. How much does one stack frame take, 100 bytes? 1KB?
Now take 46 of those and your stack has run out???
IIRC the default stack size is 1 MB.
I just ran a little test and 10000! runs just fine.
|
|
|
|
|
I can use a recursive factorial with numbers up to and including 170
Any higher overflows the UInt1024 that I used for it - no StackOverFlowException
So what happened there?
Though I agree that the advice is pointless..
|
|
|
|
|
I think that it depends on:
a) The cost of opening a function on the stack changes
b) Whether the recursion is a tail recursion or not
Natza Mitzi
|
|
|
|
|
My answer below:
I reduced the occurrence of the problem by splitting some methods and using a dictionary instead of large switches that cause stack bloats. Now a simple recursive factorial method works with 400 instead of 46, that is almost 900% better
Natza Mitzi
|
|
|
|
|
I reduced the occurrence of the problem by splitting some methods and using a dictionary instead of large switches that cause stack bloats. Now a simple recursive factorial method works with 400 instead of 46, that is almost 900% better
Natza Mitzi
|
|
|
|
|
A while ago I inherited a project that was full of the following :
if( CString( str1 ) == CString( str2 ) )
{
}
That was the most annoying thing for a variety of reasons. I should mention that this was a communications-oriented project and all of the character strings had defined sizes so that as OK but apparently someone had an aversion to the strcmp function. I couldn't believe it. Even when there was an actual CString object, like from a dialog, they would cast the other argument to a CString to do the comparison. I guess they didn't know that CString has a const char * operator.
|
|
|
|
|
That's typical of people without C background.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: That's typical of people without C background.
I think you mean C++.
|
|
|
|
|
leppie wrote: think you mean C++.
Well, C is the C++ 's background foundation.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: Well, C is the C++'s background foundation.
But C has no types (well not like the OP has ).
|
|
|
|
|
leppie wrote: But C has no types
That's precisely my point.
Only with a (solid maybe) C background he would appreciate the introduced overhead. While, for instance, VB6 people (don't blame me for the cheap shot) maybe used to thinking String is a native type.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
BTW Suppose CString equality operator implemented in a Java -string fashion: possibly the fun would be more.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Who knows what the compiler and linker will made of this statement.
I try hard to make parameters const so it can better be optimized.
Greetings from Germany
|
|
|
|
|
Nothing wrong with that statement. Creates two CString objects and do case sensitive compare with CString == operator.
|
|
|
|
|
steveb wrote: Nothing wrong with that statement
In fact it isn't wrong. But it is unnecessary (just added overhead).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
if this was the production code of some server that kept on crashing while communicating with clients, the code snippet mentioned is the exact thing I would put in in place of the strcmp which in unsafe. yeah yeah there is strcmp_s, but that just come out in 2005. Code is probably older.
|
|
|
|
|
Well using temporary objects to wrap unreliable code doesn't make sense to me.
I simply won't use unreliable code on a server.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|