Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > call stack structure

Reply
Thread Tools

call stack structure

 
 
harryos
Guest
Posts: n/a
 
      11-02-2010
hi,

while going through the java tutorial on Exceptions ,I came across a
picture of call stack.
http://download.oracle.com/javase/tu...efinition.html

In this figure,the called method is shown at the top and caller is at
bottom.I thought if method A calls B and B calls C,then call stack
should be like-

main
A
B
C

Which one is correct?
Also in the same tutorial ,on page
http://download.oracle.com/javase/tu...declaring.html
,there is a statement
'Sometimes, it's appropriate for code to catch exceptions that can
occur within it. In other cases, however, it's better to let a method
further up the call stack handle the exception'
Is that not contradictory to what was given in the picture?

regards,
harry
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-02-2010
On Mon, 1 Nov 2010 21:49:17 -0700 (PDT), harryos
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>In this figure,the called method is shown at the top and caller is at
>bottom.I thought if method A calls B and B calls C,then call stack
>should be like-


When the car was invented, the steering wheel could go on either side.
So what we do, BOTH.

Same with stack representation, the expression TOP OF STACK suggests
the most accessible item should be on top. However, stacks grow from
high addresses down to lower ones in RAM. You just have to look at the
context to see which convention is being used.

--
Roedy Green Canadian Mind Products
http://mindprod.com

Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
~ Dave Barry
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      11-02-2010
On Mon, 1 Nov 2010, harryos wrote:

> while going through the java tutorial on Exceptions ,I came across a
> picture of call stack.
> http://download.oracle.com/javase/tu...efinition.html
>
> In this figure,the called method is shown at the top and caller is at
> bottom.I thought if method A calls B and B calls C,then call stack
> should be like-
>
> main
> A
> B
> C
>
> Which one is correct?


Both. Both are ways of representing a structure that exists in a computer;
both work, neither has any particular claim to being more correct than the
other.

> Also in the same tutorial ,on page
> http://download.oracle.com/javase/tu...declaring.html
> ,there is a statement
> 'Sometimes, it's appropriate for code to catch exceptions that can
> occur within it. In other cases, however, it's better to let a method
> further up the call stack handle the exception'
> Is that not contradictory to what was given in the picture?


In the tutorial's notation, 'up' is towards the bottom of the page.

tom

--
Scheme is simple and elegant *if you're a computer*.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2010
On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
<(E-Mail Removed)> wrote, quoted or indirectly quoted
someone who said :


>When the car was invented, the steering wheel could go on either side.
>So what do we do, BOTH.


If you are experimenting on paper, you will find it is easier to draw
changing stacks if your stacks grow down.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
~ Dave Barry
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      11-04-2010
On Nov 3, 7:11*pm, Roedy Green <(E-Mail Removed)>
wrote:
> On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
> <(E-Mail Removed)> wrote, quoted or indirectly quoted
> someone who said :
>
> >When the car was invented, the steering wheel could go on either side.
> >So what do we do, BOTH.

>
> If you are experimenting on paper, you will find it is easier to draw
> changing stacks if your stacks grow down.


Also, consider when you might want to show only a limited set of the
callers (it may be hundreds deep). You almost always want to know
where the exception really occurred. Most debuggers show it as:
problem in D
called from C
called from B
called from A

If you want to limit the information to 5 deep, it is easier and
faster to do it this way, just halting after five outputs. For the
other direction one would have to unwind the full stack, then discard
all but the last five (depending on how the 'stack' information is
kept).
--
Fred K
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-04-2010
On Nov 4, 2:29*pm, Fred <(E-Mail Removed)> wrote:
> On Nov 3, 7:11*pm, Roedy Green <(E-Mail Removed)>
> wrote:
>
> > On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
> > <(E-Mail Removed)> wrote, quoted or indirectly quoted
> > someone who said :

>
> > >When the car was invented, the steering wheel could go on either side.
> > >So what do we do, BOTH.

>
> > If you are experimenting on paper, you will find it is easier to draw
> > changing stacks if your stacks grow down.

>
> Also, consider when you might want to show only a limited set of the
> callers (it may be hundreds deep). *You almost always want to know
> where the exception really occurred. Most debuggers show it as:
> problem in D
> called from C
> called from B
> called from A
>
> If you want to limit the information to 5 deep, it is easier and
> faster to do it this way, just halting after five outputs. For the
> other direction one would have to unwind the full stack, then discard
> all but the last five (depending on how the 'stack' information is
> kept).
> --


When you get the array of 'StackTraceElement' from a 'Throwable':
<http://download.oracle.com/javase/6/docs/api/java/lang/
Throwable.html#getStackTrace()>
"The zeroth element of the array (assuming the array's length is non-
zero) represents the top of the stack, which is the last method
invocation in the sequence. Typically, this is the point at which this
throwable was created and thrown."

The 'printStackTrace()' method shows the zeroth element at the top of
the display:
<http://download.oracle.com/javase/6/docs/api/java/lang/
Throwable.html#printStackTrace()>
"the following example may be regarded as typical:
HighLevelException: MidLevelException: LowLevelException
at Junk.a(Junk.java:13)
at Junk.main(Junk.java:4)
Caused by: MidLevelException: LowLevelException
at Junk.c(Junk.java:23)
at Junk.b(Junk.java:17)
at Junk.a(Junk.java:11)
... 1 more
Caused by: LowLevelException
at Junk.e(Junk.java:30)
at Junk.d(Junk.java:27)
at Junk.c(Junk.java:21)
... 3 more"

It seems that the definition of "top" and "bottom" is situational.

--
Lew

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      11-16-2010
On 02-11-2010 01:04, Roedy Green wrote:
> Same with stack representation, the expression TOP OF STACK suggests
> the most accessible item should be on top. However, stacks grow from
> high addresses down to lower ones in RAM.


On most common hardware. It is not given that it has to be so.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments