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 321131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:49:02+00:00 2026-05-12T08:49:02+00:00

This is similar to What happens when you run a program? , but not

  • 0

This is similar to What happens when you run a program?, but not a dupe.

Let’s say I have a simple console program with two methods Aand B.

    public static void RunSnippet()
    {
        TestClass t = new TestClass();
        t.A(1, 2);

        t.B(3, 4);
    }

    public class TestClass
    {
        public void A(int param1, int param2)
        {
            //do something
            C();
        }

        private void C()
        {
            //do
        }

        public bool B(int param1, int param2)
        {
            //do something
            bool result = true;

            return result;
        }
    }

Can someone explain in detail (but please keep it in simple plain English), what really happens when RunSnippet calls method A and method B (and they internally call some other methods). I want to understand what really happens under the hood…meaning how are params passed, where are they stored, what happens to local vars, how are return values passed, What will happen if another thread starts running when A has called C, what will happen if an exception is thrown.

  • 1 1 Answer
  • 1 View
  • 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-12T08:49:02+00:00Added an answer on May 12, 2026 at 8:49 am

    I’m not quite sure what level of detail you’re looking for, but here’s my stab at explaining what’s happening:

    1. A new process is created for your executable. That process has a stack segment containing each thread’s stack, a data segment for static variables as well as a memory block called the heap for dynamically allocated memory, and a code segment containing the compiled code.
    2. Your code is loaded into the code segment, the instruction pointer is set to the first instruction in your main() method, and the code begins executing.
    3. Object t is allocated from the heap. The address of t is stored on the stack (each thread has it’s own stack).
    4. t.A() is called by placing the return address to main() on the stack and changing the instruction pointer to the start of t.A()’s code. The return address is placed on the stack along with the values 1 and 2.
    5. t.A() calls t.C() by placing the return address to t.A() on the stack and changing the instruction pointer to the start address of t.C()’s code.
    6. t.C() returns by popping the return address to t.A() off of the stack and setting the instruction pointer to that value.
    7. t.A() returns in a similar manner to t.C().
    8. The call to t.B() is very similar to the call to t.A() except that it returns a value. The exact mechanism to return that value is language and platform dependent. Often the value will be returned in a CPU register.

    Note: Since your methods are very small, modern compilers will often “inline” them instead of making a classic call. Inlining means taking the code from the methods and injecting them straight into the main() method rather than going through the (slight) overhead of making a function call.

    Given your example I don’t see how threading could come into the picture directly. If you were to start the executable a second time, it would run in a new process. That means it would get it’s own code segment, data segment and stack segment completely isolating it from the first process.

    If your code were run inside a larger program that called main() on several threads, it would run almost exactly as previously described. The code is thread safe because it doesn’t access any potentially shared resources such as static variables. There is no way that Thread 1 could “see” Thread 2 because all key data (values and pointers to objects) is stored on the thread’s local stack.

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

Sidebar

Related Questions

This is similar to my previous posting. But this time I want to call
This is similar to this question , but I want to include the path
This is similar to the cutting stock problem , but with a slight difference.
This is similar to a previous question , but the answers there don't satisfy
I know this is similar to this question , but I'm using SQL Server
This is perhaps similar to previous posts, but I want to be specific about
Similar to this question, but for VB.NET since I learned this is a language
This happens when I try to run rinari-cap: ~/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `gem_original_require': no such file to
Very similar to this question , except for Java. What is the recommended way
Similar to this question , after running the following code the browser dialog does

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.