Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Drawing activation stack for program with Exception

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    NYC
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing activation stack for program with Exception

    Hi.
    I'm a beginner to Java programming and I'm having a hard time grasping how the activation stack works.
    I need help drawing the activation stack for this program:

    1 public class Q2 {
    2     public static void main(String[] args) {
    3        String inputStr;
    4        int input = 8;
    5
    6        System.out.print("Input: ");
    7        inputStr = StdIn.readLine();
    8        try {
    9           input = Integer.parseInt(inputStr);
    10       } catch (NumberFormatException ex) {
    11       }
    12       a(input);
    13       return;
    14    }
    15
    16
    17    static void a(int limit) {
    18       int[] array;
    19       int size = 10;
    20
    21       array = new int[size];
    22       for (int i=0; i<=limit; i++) {
    23          array[i] = 3*i;
    24       }
    25    }
    26 }

    When the program is given an input of 20, it crashes with this report:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
         at Q2.a(Q2.java:23)
         at Q2.main(Q2.java:12)

    I need to draw the activation stack as it exists at the moment of the crash. Indicate the name
    and value of every variable in each activation record. If the variable is an object
    reference, indicate that fact and draw the object’s value(s) off to the side of the
    stack (since object values are stored in the heap, not the stack).

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing activation stack for program with Exception

    by "activation stack" Do you mean the call stack?
    What are you trying to draw? Can you give an example?
    draw the object’s value(s) off to the side of the stack
    I've never seen any way to access the local variables that the JVM would save on the stack. That is not a normal topic for these forums.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    NYC
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing activation stack for program with Exception

    Yes, the call stack.
    When a method is called("invoked"), an activation record(region of memory where all that method's local variables are stored) for it is pushed onto the top of the stack.
    So as you return, you remove information from top of the stack.

    Here are some notes on the topic:
    Wikisend: free file sharing service

    It gives an idea what the stack looks like.

    "off to the side of the stack" is referring to the heap where the object values are stored.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing activation stack for program with Exception

    I understand the concept and have worked with the stack and heap in assembly language.
    I don't know of any way to get that information from the JVM.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Location
    NYC
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing activation stack for program with Exception

    I think I need to identify the variables and their values at the moment of the crash.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing activation stack for program with Exception

    Sounds like a useful tool. No idea where to get the data from.
    I think I've heard of a debug interface into the JVM but have never seen any samples of it being used.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Location
    NYC
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing activation stack for program with Exception

    Okay. I'll see what I can come up with.
    Thanks for your time.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing activation stack for program with Exception

    Be sure to let us know what you find and if you are able to get the names and values of the variables on the stack.

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Drawing activation stack for program with Exception

    Norm: You can get a stack trace trivially by creating an Exception (but not throwing it): its stacktracelements will all be there. As for the local variables, not a clue off the top of my head - but how hard can it be? Debuggers do it and so does jmap / visualvm. I see this indicated as one way to do it - but I assume that's not going to work for a non-Oracle JVM?

    http://docs.oracle.com/javase/6/docs...ticMXBean.html

    InfiinteSound: do you just want to debug your code? ArrayIndexOutOfBoundsException is always caused by failing to check array limits. You pass in the variable 'limit' and create an array of size 'size' and then iterate until 'limit' again. You should be checking i against your array's .length member.
    Last edited by Sean4u; March 4th, 2012 at 03:13 PM. Reason: omitted link

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing activation stack for program with Exception

    how hard can it be?
    Probably not hard with the right API. I've never looked for it so I don't know what is available.

Similar Threads

  1. Java Program to Check whether a Expression is Valid using Stack
    By rainbow9 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 02:11 AM
  2. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  3. Replies: 1
    Last Post: August 2nd, 2011, 05:13 PM
  4. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  5. Line Drawing Program
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2010, 03:54 PM

Tags for this Thread