If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Stack Overflow Error?

I am writing a program to act as a postage calculator. I am getting a stack overflow error and I can't for the life of me figure out WHY it is happening. Can one of you gents point me in the right direction??

Re: Stack Overflow Error?

This is why. Look closely at what you are returning.

Code:

public string State1
{
get
{
return State1;
}//end get
}

That is an example of infinite recursion.

This is one argument against naming private instance members as you do. in C#, private member variables usually begin with an underscore and are followed by a lowercase letter (same with local variables.) So, "State" becomes _state. This is of course not a hard rule, but it is idiomatic and generally accepted as the style to use by the C# community.

Also, you have a lot of comments that are absolutely useless. I know that almost all teachers require comments like that, so if this is a school project I understand, but n the real world comments like that just clutter up the code and add no value. Comments are supposed to explain *why* the code does what it does, not *what* the code does. Any programmer can tell what the code is doing by simply reading it, no comments required.

Re: Stack Overflow Error?

StackOverflowExceptions occur when you have tried to call too many methods deep into the callstack. This is usually a sign of recursion without a base case or two methods which circularly call each other. In this case, the problem can be solved by modifying the Getter for State1 in the Package class. It currently reads:

Which will return the member variable State of package rather than calling the getter again.

This sort of problem can be avoided by using some common conventions. Often, I use lowercase letter to name the member variables of my classes and Uppercase letters to name their getters/setters. Another common convention is to preceed member variables in the class with an underscore (_) to remember that they are member variables and not exposed.

Note that solving that problem exposes another, but I will let you play with the debugging on that. What debugger/IDE are you using? Visual Studio is an excellent choice. There are some others that are good too. My favorite free, open-source one is SharpDevelop which you can get here: http://www.icsharpcode.net/opensource/sd/. A good IDE with a good debugger is a programmer's best friend. I understand though why you might be confused if this was your first encounter with StackOverflowExceptions.

As a general note too, unless your instructor explicitly requires you to comment all curly brace closures ( } ), I would omit them. They merely clutter the code which hinders readability without actually convening that much information.

I hope that helps!

Best Regards,

BioPhysEngrhttp://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Stack Overflow Error?

Originally Posted by BioPhysEngr

StackOverflowExceptions occur when you have tried to call too many methods deep into the callstack.

We posted the same exact response

To be a bit pedantic though, a stack overflow does not occur when you call too many methods, it occurs when you exceed the amount of memory allocated to the stack space of the current thread. Of course, infinite recursion is a way to cause that, but there are other ways as well.

Re: Stack Overflow Error?

Yeah, it's pretty rare to be anything other than this in C# due to most objects being allocated on the heap. It would take a struct with a lot of stack allocated fields/member variables to overrun the stack space.

Re: Stack Overflow Error?

Guys,
Thanks a bundle! You'll have to excuse my newbieness, I only had 1 semester in C++ prior to this, but luckily this is my last programming class I need for my EE degree. Everyone said the transition from C++ to C# would be easy, but to be honest I REALLY don't get it(I wanted to do java). I commented the living crap out of my code so that I could keep MYSELF in line, and the instructor has no issues with it.

Again, I'll try some of the changes you guys mentioned and report back, thanks guys!

Re: Stack Overflow Error?

I lopped off everything after {8}\n and it ran to completion .... did you forget to add the operands to support the format ?

Another thought ... just noticed that you might wanna add a "\n" here and there ... I don't think the display is quite what you had in mind.

If you run the debugger in the 'start debugging' mode with no breaks set, the debugger comes to a halt at the offending statement. MS really has equipped VS with a world-class debugger (IMHO) (an' the Exception printout is also very informative - what a great IDE !).

Re: Stack Overflow Error?

Originally Posted by jetter2

Everyone said the transition from C++ to C# would be easy, but to be honest I REALLY don't get it(I wanted to do java).

Yeah, they didn't know what they were talking about . The biggest similarity between C# and C++ is the syntax, pretty much everything else is different. You would have had the same issue going to java, which is similar in many ways to C# (well, probably the other way around since java came first, but C# has outdone java imo)

I lopped off everything after {8}\n and it ran to completion .... did you forget to add the operands to support the format ?....

So wait, you closed the entire program after {8}/n ? What exactly did you edit out..can you post the code chunk that compiled for you?

Originally Posted by BigEd781

Yeah, they didn't know what they were talking about . The biggest similarity between C# and C++ is the syntax, pretty much everything else is different. You would have had the same issue going to java, which is similar in many ways to C# (well, probably the other way around since java came first, but C# has outdone java imo)

Bah! I'm a EE major as it is, but I still somewhat enjoy programming (I'm a network engineer by trait so coding always kinda seemed foreign to me) but C++ was WAY easier to pickup!

Re: Stack Overflow Error?

Oh Boy am I ever embarrassed !

Yup, you were right to question my action. Yeah, lopping off everything after the 8}\n would fix it too, but that is excessive. The real problem was the {16}, so i changed it to ...
"{0}: {1} \n{2}: {3}: \n{4} {5}: \n{6} {7}: \n{8} {9}: \n{10}: {11}: \n{12} {13:F2}: \n{14} {15:C}\n ",

that's what happens when one makes a change and doesn't even give it a second thought (some might say "or even a first thought")

Re: Stack Overflow Error?

Originally Posted by ThermoSight

Oh Boy am I ever embarrassed !

Yup, you were right to question my action. Yeah, lopping off everything after the 8}\n would fix it too, but that is excessive. The real problem was the {16}, so i changed it to ...
"{0}: {1} \n{2}: {3}: \n{4} {5}: \n{6} {7}: \n{8} {9}: \n{10}: {11}: \n{12} {13:F2}: \n{14} {15:C}\n ",

that's what happens when one makes a change and doesn't even give it a second thought (some might say "or even a first thought")

thank you for pointing out my error.

Thermo, I really appreciate this!

Here is my REAL question (to all you guys)..

As I said before, I'm an Engineering major and I am in part 2 of my programming sequence. Part 1 was just C++ all the way up to structs, now in semester 2 of the sequence you can either take Java, more C++ or C#.
I work full time and the only class I could get into was the C# class. Are there any forums/link/books that I can take up that will help ease the conversion from C++ to C#? Our professor talks like he expects us to understand this already and I just want to get through this course!

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.