2 Replies - 755 Views - Last Post: 07 June 2010 - 03:41 AM

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Call Stack

Posted 03 June 2010 - 03:36 PM

Basically im trying to understand the Call Stack and what it does etc. Reading off wiki its primary purpose is to store return addresses. Im not sure im understanding this correctly so... is it like:

So sub main is called when the program starts:

Random Program

Sub Main
<Random Code>
Call Test
<Random Code 2>
End Sub

Function Test

Random function

End Function


So in this terrible example a call stack would be used so when Test is called it knows when Test has finished that it needs to go back to the beginning of Random Code 2?

Another question, does recursion often cause an overflow as theres lots of calls being made so lots of return addresses being added to the stack?

Is This A Good Question/Topic? 0
  • +

Replies To: Call Stack

#2 GWatt  Icon User is online

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: Call Stack

Posted 03 June 2010 - 04:55 PM

Well, when you call a function b() from another function a() the function b() has to know where in the function a() to return. b() can't just call a() again. So what happens is that the current point of execution in a() is stored on the call stack, and when b() finishes the program execution jumps back to that point.
Was This Post Helpful? 1
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Call Stack

Posted 07 June 2010 - 03:41 AM

View PostMatty919, on 03 June 2010 - 05:36 PM, said:

Another question, does recursion often cause an overflow as theres lots of calls being made so lots of return addresses being added to the stack?

Basically yes.

View PostMatty919, on 03 June 2010 - 05:36 PM, said:

Basically im trying to understand the Call Stack and what it does etc. Reading off wiki its primary purpose is to store return addresses. Im not sure im understanding this correctly so... is it like:

If you want to know more about the stack you might want to do some assembly language programming.

In a nutshell, when you call a function it saves the current position in memory into a register (so that we can come back when we're done in the function). The problem is, we may want to call another function from within the function we just called. We can't just clobber the original position we saved, so we push it onto the stack for safe-keeping.

You should know that most C compilers (32bit anyway) use the stack to pass arguments as well. Before you call a function you push your arguments/parameters onto the stack first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1