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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:05:19+00:00 2026-05-25T20:05:19+00:00

Someone in stack-overflow answered stack is attached to a thread, so when the thread

  • 0

Someone in stack-overflow answered “stack is attached to a thread, so when the thread exits the stack is reclaimed.” But what about public variables how they are managed once thread execution is completed.

For Ex. Declared public variable like

public partial class Form1 : Form
{
        int k = 10;

        private void button1_Click(object sender, EventArgs e)
        {            
            k = k + 5;
        }
}

So in this whenever we click button1 a thread is created which updates value of k and then exist so as per this statement stack should be reclaimed and recent value of k should be lost but in reality it store that value and next button click will changes its retain value for ex. from 1) 10 to 15 2)15 to 20 3) 20 to 25 etc….

So My question is where such global variables are stored. Whether any other stack used for global variables. Also I will use object of a class inside a function for ex. such as

public function add (int a, int b)
{
       int c; 
       clsitem objitem = new clsitem();
       c=a+b + objitem.id;
       retun c;
}

so what will happened of objitem , whether it will store in stack and reclaimed when thread exits or it will stay in heap as it is an object

  • 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-25T20:05:20+00:00Added an answer on May 25, 2026 at 8:05 pm

    In your example here, k is not a stack-based variable – it’s a field of the Form1 class and is hence stored with the instance of the form.

    The two arguments to button1_click are examples of stack-based variables.

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

Sidebar

Related Questions

I was just messing around to answer someone's question here on Stack Overflow, when
I've been Googling and checking on Stack Overflow and nothing has helped... hopefully someone
I've just found out about Stack Overflow and I'm just checking if there are
Hey guys, I have this great scripts someone on stack overflow helped me out
Someone (w69rdy) in Stack Overflow helped me out with a great example to handle
I've recently thought about this case of stack overflow: int f() { return f();
Recently someone on Stack Overflow told me that the code below does not leak,
In the Stack Overflow podcasts, Joel Spolsky constantly harps on Jeff Atwood about Jeff
I've seen a few mentions of this on Stack Overflow, but staring at Wikipedia
Can someone share a good way to save the program state (UINavigationController stack, etc)

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.