none
Where the data is stored RRS feed

  • Question

  • Hi,

           Please check the following programs and inline comments,Please verify and explain.

            public class Genration
            {
                public int i = 0; // Does this goes into stack
                public static int j = 0; // will this go to AppDomain's loader 
                [ThreadStatic]
                static string globl;  // Will this go to threads local storage
                public static int j = 0; // will this go to AppDomain's loader 
                public int count { get; set; }
                public Genration generation = new Genration();
    
                private void Display()
                {
                    int value = 0; // goes into stack am i wrong
                    Genration gn = new Genration(); // goes to heap
                }
            }

    Thursday, July 4, 2013 8:51 AM

Answers

  • "public int i = 0; // Does this goes into stack"

    No. That's a member of a reference type, it's allocated as part of the object and since the object is allocated on the heap the "i" variable is also on the heap.

    "public int i = 0; // Does this goes into stack"

    More or less. "The stack is an implementation detail" - http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

    A local variable can be stored on stack or in a CPU register or nowhere if the compile figures out that it isn't used. It could also be allocated on the heap but probably no implementation would do that for performance reasons. In summary, where a local variable is stored is mostly irrelevant.

    ""Genration gn = new Genration(); // goes to heap""

    The newly created Generation object is stored on the heap. The gn reference is a local variable and it is stored like all other local variables, on stack/register/nowhere.

    The rest of your comments are ok.

    • Marked as answer by Shijith M.C Saturday, July 6, 2013 1:39 PM
    Thursday, July 4, 2013 9:24 AM
    Moderator

All replies

  • "public int i = 0; // Does this goes into stack"

    No. That's a member of a reference type, it's allocated as part of the object and since the object is allocated on the heap the "i" variable is also on the heap.

    "public int i = 0; // Does this goes into stack"

    More or less. "The stack is an implementation detail" - http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

    A local variable can be stored on stack or in a CPU register or nowhere if the compile figures out that it isn't used. It could also be allocated on the heap but probably no implementation would do that for performance reasons. In summary, where a local variable is stored is mostly irrelevant.

    ""Genration gn = new Genration(); // goes to heap""

    The newly created Generation object is stored on the heap. The gn reference is a local variable and it is stored like all other local variables, on stack/register/nowhere.

    The rest of your comments are ok.

    • Marked as answer by Shijith M.C Saturday, July 6, 2013 1:39 PM
    Thursday, July 4, 2013 9:24 AM
    Moderator
  • Thanks for your reply,

        Genration gn = new Genration(); // goes to heap

    "The newly created Generation object is stored on the heap. The gn reference is a local variable and it is stored like all other local variables, on stack/register/nowhere."

    How this get stored in stack?

    Thursday, July 4, 2013 9:33 AM
  • "How this get stored in stack?"

    Hmm, not sure how this can be explained better. You can consider references as some special kind of values, they're stored exactly the same way as value type are.

    Thursday, July 4, 2013 10:47 AM
    Moderator