Be a Supporter!

Java StackOverflowErro r question

  • 265 Views
  • 6 Replies
New Topic Respond to this Topic
Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Java StackOverflowErro r question 2011-10-08 11:48:18 Reply

Hey there,

In my programming course we have a challenge each week, consisting of a hard assignment that you can make. This week we got a obfuscated code and our task it to make it:
A readable so the original code has good names and comments.
B fix a weird bug that is hidden somewhere in it.

I got a question about the code I got:

static int aahFunciton(int parameterInt)
    {
        someIntergeerrrr = secondTimeFunciton(1);
        if (false)
            ;
        {
            try
            {
                tryFunction();
            } catch (StackOverflowError error)
            {
                return someIntergeerrrr + parameterInt;
            }
        }
        return secondTimeFunciton(3) + parameterInt;
    }

The names are silly but its still better than: _ and __ and __1. Im not asking to make it for me but I want to understand how the catch and try and StackOverflowError thing works.

everette00
everette00
  • Member since: Nov. 13, 2008
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Java StackOverflowErro r question 2011-10-08 13:34:26 Reply

At 10/8/11 11:48 AM, Sandremss128 wrote: Hey there,

In my programming course we have a challenge each week, consisting of a hard assignment that you can make. This week we got a obfuscated code and our task it to make it:
A readable so the original code has good names and comments.
B fix a weird bug that is hidden somewhere in it.

I got a question about the code I got:

static int aahFunciton(int parameterInt)
{
someIntergeerrrr = secondTimeFunciton(1);
if (false)
;
{
try
{
tryFunction();
} catch (StackOverflowError error)
{
return someIntergeerrrr + parameterInt;
}
}
return secondTimeFunciton(3) + parameterInt;
}

The names are silly but its still better than: _ and __ and __1. Im not asking to make it for me but I want to understand how the catch and try and StackOverflowError thing works.

To begin, you have a semi-colon after the end-parenthesis for the conditional; this prompts a line-break that is supposed to be the beginning of a block of code.

The try and catch is for exception programming. In your try statement, you have a routine that you'd like to "try". If your routine fails in any way, the program will jump to your catch statement, which will execute an alternative routine. If you know of a specific issue that would arise inside of the try statement, you can catch that error, helping you better address this issue and preventing a crash.

In your code, the catch statement is looking for a stack overflow error, which is when your program floods the stack portion of your memory, attempting to use more stack than what is available. A case where this can easily happen is when using a method inside of itself (static or otherwise), then calling the method somewhere else in your code.

I hope this helped!

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Java StackOverflowErro r question 2011-10-09 06:36:13 Reply

So this actually does nothing?:

if (false) ;

And this actually opens a new scope?:

{ 
     //statements
}

I got another question:
What does this code do?

for (intParNR2 = localInt2;; localInt3++)
            {
                localInt3++;
                break;
            }

All the variables are defined integers. My questions is what happens if you have a for loop and the part where you normally put the condition is empty?
My compiler warned here for 'dead code' (2 times).
Even if the loop executes it only increments the local integer nr 3 by 1. My question is:
Will intParNR2 be assigned to localInt2?
Will localInt3 be incremented by the loop part?
Will localInt3 be incremented in the body of the loop?

And thank you for your previous post, it was helpfully :)

gumOnShoe
gumOnShoe
  • Member since: May. 29, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Java StackOverflowErro r question 2011-10-09 11:21:51 Reply

At 10/9/11 06:36 AM, Sandremss128 wrote: for (intParNR2 = localInt2;; localInt3++)
{
localInt3++;
break;
}

A for loop consists of 3 parts (normally). Variable definition ; Condition (if the condition evaluates to true, you go through the code again, otherwise exit) ; some code to run after a loop has completed, but before the condition is evaluated.

Basically, NR2 gets set to localInt2. There is no condition, so I'm not sure if the code even runs (you could check with a debugger or System.out.println() ). If it did run, it would increment localInt3 by 1, and then break would exit the for loop.

All the variables are defined integers. My questions is what happens if you have a for loop and the part where you normally put the condition is empty?

Why don't you try it out? That should take 2 seconds to check.

My compiler warned here for 'dead code' (2 times).

Warnings tell you that there is an issue, but not one that breaks the program. What the compiler is suggesting is that the for loop does nothing, which is probably true.

Even if the loop executes it only increments the local integer nr 3 by 1. My question is:
Will intParNR2 be assigned to localInt2?

Is there any code that does that? Because I don't see it.

Will localInt3 be incremented by the loop part?

Unlikely, but test to make sure. That's the point of homework.

Will localInt3 be incremented in the body of the loop?

That depends on whether the code executes. Test and find out.


Newgrounds Anthology? 20,000 Word Max. [Submit]

Music? Click Sig:

BBS Signature
everette00
everette00
  • Member since: Nov. 13, 2008
  • Offline.
Forum Stats
Member
Level 04
Programmer
Response to Java StackOverflowErro r question 2011-10-09 11:22:11 Reply

At 10/9/11 06:36 AM, Sandremss128 wrote: So this actually does nothing?:

if (false) ;

And this actually opens a new scope?:

{
//statements
}

I got another question:
What does this code do?

for (intParNR2 = localInt2;; localInt3++)
{
localInt3++;
break;
}

All the variables are defined integers. My questions is what happens if you have a for loop and the part where you normally put the condition is empty?
My compiler warned here for 'dead code' (2 times).
Even if the loop executes it only increments the local integer nr 3 by 1. My question is:
Will intParNR2 be assigned to localInt2?
Will localInt3 be incremented by the loop part?
Will localInt3 be incremented in the body of the loop?

And thank you for your previous post, it was helpfully :)

Oh, I didn't even see the condition for the if statement, I was actually talking about the semi-colon that is placed after the parenthesis. When you use conditionals, such as an if statement, you have to provide a condition to check a value against. An example would be:

if(age == 21)
    AllowEntranceToBar();

Plainly stating "false" isn't a proper condition. What is false? But yes, other than the improper condition, the semi-colon is a syntactical error, because semi-colons are to break lines. You are trying to executing a routine that is only allowed to when something happens to be true (even if a boolean is false, if you are checking for a false value to that boolean, when and if it becomes false, the condition is met).

Yes, curly-braces are, in some languages, the indicators of a new scope. If you were to declare and initialize a variable inside of a if-statement's scope, then attempt to use it outside of its scope, you'd run into an error.

There are two ways you can use a for loop. The first is with a limitation, the second is without a limitation. I'm not sure what they call the first, but the second way to use a for loop is nicknamed "infinite loop," because there is no automated end(condition) to them. When you use a infinite loop, it will iterate a routine paced inside of it's scope, over and over, until you set a specific end to it, like break.

I only know why you would get a "dead code" message once, but I don't know why twice. Because you placed a break at the end of the loop, with no specific reason why you would want to break the loop, once your program reaches the break, the loop ends, only executing once.

Yes, intPartNR2 will be assigned to localInt2.

localInt3 will be incremented twice for every iteration - once by the incremental inside of the loop parameters, and another inside of the loop's scope.

You're quite welcome! I hope this post was of some more help!

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to Java StackOverflowErro r question 2011-10-09 12:25:14 Reply

I got it figured out!
You see the programme we got contains a bug, a bug that is hard to recreate and only happens sometimes.

static int aahFunciton(int inputDevidedBy2)
    {
        someIntergeerrrr = giveSomeInteger(1); //assign zero
        try
        {
            endlessRecursiveFunction();
        } catch (StackOverflowError error)
        {
            //return either input / 2 + either 0 or 1
            return someIntergeerrrr + inputDevidedBy2;
        }
        //this code will never run since the recursive function will cause an error at all cases
        return giveSomeInteger(3) + inputDevidedBy2;
    }

    static void endlessRecursiveFunction()
    {
        someIntergeerrrr = giveSomeInteger(3) - someIntergeerrrr; //1 - someInter (starts at 0)
        //giveSomeInteger(3) will always be 1
        //so this function will switch to zero and one all of the time untill it causes a stackOverFlow
        //01010101010101010101010101
        endlessRecursiveFunction();
    }

So basically it tries a function that we know is infinite. This infinite function changes a variable that it is either zero '0' or one '1'. Since there is apparently some inconsistency in the stackOverFlow error this is the same as saying: Math.round(Math.random) (1 or 0).
Is it safe to say that this part of the code is the source of the bug (since I believe that the stackOverFlow error is inconsistent and will add randomness to the variable)?

gumOnShoe
gumOnShoe
  • Member since: May. 29, 2004
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to Java StackOverflowErro r question 2011-10-09 20:15:23 Reply

I think you've sort of got it down, but you shouldn't use stack overflow (as funny as that is) as a random number generator. Just so that you better understand stack overflow, it happens when you run out of memory. Java has two types of memory: (1) the stack & (2) the heap. Functions are placed on the stack (first in last out). The heap is where most of your variables go to get stored. You can run out of memory in either location, and you can change the size of memory allocated to java to help avoid this issue. Because of that you can never *guarantee a stack overflow will happen. But if you're using an infinitely recurssing function there's a pretty good (certain) chance it will happen on todays computers with todays compilers.

As you've figured out, a when a method calls itself it takes up a bit of memory. Eventually this program fills that up. When that happens is machine dependent. Technically, if you ran this program on the same java machine with the same starting state, over and over, you'd get the same result. Its only random because the java virtual machine (you'll learn about that later) optimizes code on the fly rather than using machine code. This gives you the sense that something random is happening when it isn't.

Certainly a neat homework problem. I'd say you found the bug, hopefully you've learned something. :)


Newgrounds Anthology? 20,000 Word Max. [Submit]

Music? Click Sig:

BBS Signature