none
Where static field in a struct stored in memory? RRS feed

  • Question

  • By default structures are value types and are stored in thread stack..

    But, where the static field "x"  in this struct gets stored in memory?

    eg :

     public struct MyClass
        {
             static int x;
            public static void ModifyX()
            {
                x++;
            }
        }

    Tuesday, April 9, 2013 12:50 PM

Answers

  • Hi,

    Try perhaps http://msdn.microsoft.com/en-us/magazine/cc163791.aspx which is likely the best reference I found so far.

    The strict relation between a type and its not storage is not as tight as often said and is rather a common but not 100% accurate shortcut.

    IMO it's best to consider that structures are NOT stored "by default" somewhere but that all types depending on its nature *AND* possibly depending on the usage context is stored where it makes sense (and could even spread several storage locations such as for a local value type variable with static members). It's even worse if you consider also how metadata related to the type are stored (or even if you distinguish the variable itself and where it points to)...

    Note finally that unless you do have a particular goal, understanding in details what is stored where has little pratical interest. Is this an academical interest or do you have a particular goal ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, April 10, 2013 12:20 PM

All replies

  • static fields are always stored on heap - however, this is a special area inside the heap known as 'High Frequency Heap' which is not garbage collected.

    For more info, read this article.

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, April 9, 2013 2:38 PM
  • Thanks for the information.

    But i would like to know about this high frequency heap

    Wednesday, April 10, 2013 8:24 AM
  • Hi,

    Try perhaps http://msdn.microsoft.com/en-us/magazine/cc163791.aspx which is likely the best reference I found so far.

    The strict relation between a type and its not storage is not as tight as often said and is rather a common but not 100% accurate shortcut.

    IMO it's best to consider that structures are NOT stored "by default" somewhere but that all types depending on its nature *AND* possibly depending on the usage context is stored where it makes sense (and could even spread several storage locations such as for a local value type variable with static members). It's even worse if you consider also how metadata related to the type are stored (or even if you distinguish the variable itself and where it points to)...

    Note finally that unless you do have a particular goal, understanding in details what is stored where has little pratical interest. Is this an academical interest or do you have a particular goal ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Wednesday, April 10, 2013 12:20 PM
  • Thanks Patrice
    Thursday, April 11, 2013 4:41 AM