Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:13:03+00:00 2026-05-25T17:13:03+00:00

According to Wiki: the caller pushes the return address onto the stack, and the

  • 0

According to Wiki:

the caller pushes the return address onto the stack, and the called
subroutine, when it finishes, pops the return address off the call
stack and transfers control to that address.

Pic from Wiki:

enter image description here

I don’t quite understand this.
Say I have a C program as follows:

#include <stdio.h>

int foo(int x)
{
    return x+1;
}

void spam()
{
    int a = 1;  //local variable
    int b = foo(a);  //subroutine called
    int c = b;  //local variable
}

int main()
{
    spam();
    return 0;
}

And I think the call stack should be something like a drawing as follows:

<None> means none local variables or params

      _| parameters for foo() <int x>  |_
top    | local of spam() <int c>       |
^      | return address of foo()       |<---foo() called, when finishes, return here?
|      | local of spam() <int b>       |
bot    | local of spam() <int a>       |
      _| parameters for spam() <None>  |_
       | locals of main() <None>       | 
       | return address of spam()      |<---spam() called, when finishes, return here?
       | parameters for main() <None>  |

Question:

According to the words quoted from Wiki,

the called subroutine, when it finishes, pops the return address off
the call stack and transfers control to that address.

1.Is my drawing right?

2.If it is right, then when foo() finishes, it will

pop the return address off the call stack and transfer control to
that address

, but how can it pop off the return address?
Because when foo finishes, the current stack pointer is pointing to spam’s local ,
right?

UPDATE:

what if the main() looks like this:

int main()
{ 
    spam();
    foo();
}

then what the call stack should look like?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:13:04+00:00Added an answer on May 25, 2026 at 5:13 pm

    Your drawing is not correct. The local stack variables for a function are all below any return addresses. Otherwise, as you have observed, the locals would get lost when you call a function.

    It should be like this:

    | parameters for foo() <int x>  |
    | return address of foo()       |
    | local of spam() <int c>       |
    | local of spam() <int b>       |
    | local of spam() <int a>       |
    | parameters for spam() <None>  |
    | return address of spam()      |
    | locals of main() <None>       | 
    | parameters for main() <None>  |
    

    I think the confusion is that you believe that variable declarations are treated as statements and executed in order. In fact the compiler will typically analyse a function to decide how much stack space is needed for all the local variables. Then it emits code to adjust the stack pointer accordingly and that adjustment is made on entry to the function. Any calls to other functions can then push onto the stack without interfering with this function’s stack frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the WordPress wiki pages on developing themes, I have to call wp_head()
According to this: http://www.codeplex.com/IronPython/Wiki/View.aspx?title=IP20VsCPy25Perf&referringTitle=IronPython%20Performance IronPython (Python for .Net) is faster than regular Python (cPython)
According to wiki shifts can be used to calculate powers of 2: A left
I am trying to use the jQuery CSV plugin, as documented here: http://code.google.com/p/js-tables/wiki/CSV According
According to http://www.assembla.com/wiki/show/scala-ide/Troubleshooting in order to have the Scala Eclipse plugin to work with
According to wikipedia! http://en.wikipedia.org/wiki/ORDBMS IBM's DB2, Oracle database, and Microsoft SQL Server, make claims
According to Wiki , the disadvantages of registry: Installers and uninstallers become more complex.
According to this article http://wiki.eclipse.org/FAQ_How_do_I_load_and_save_plug-in_preferences%3F we're supposed to use preferences as shown below. Preferences
According to tips from MySQL performance wiki: Don't use DISTINCT when you have or
I have a question about the skype protocol. Supposedly, according to wiki, the supernodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.