0

There are two types:

First is a value type which is stored on the stack and the second is a reference type which is stored on the heap.

Now if we have one static variable, let's say:

static int a= 10;
static string str = "Question";

How is the memory allocation done?

Also if we have an struct:

Struct struct
{
    int i;
    static string name;
    float f;
};

How will memory be allocated for this struct?

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
KamalDeep
  • 791
  • 5
  • 8
  • AFAIK The fact that a variable is static should not change the way it's stored on memory, so the ints would be stored in the stack and the string in the heap. – Zohar Peled Apr 30 '17 at 11:54
  • 1
    _... value type which stores in stack"_ is not true for starters. – H H Apr 30 '17 at 11:57
  • Let me know if this isn't a duplicate, one of the c# language designers explains it all here: http://stackoverflow.com/q/40564712/495455 – Jeremy Thompson Apr 30 '17 at 12:01
  • Possible duplicate of [How is memory allocated for a static variable?](http://stackoverflow.com/questions/337019/how-is-memory-allocated-for-a-static-variable) – dandan78 Apr 30 '17 at 12:06
  • 5
    Static variables are normally stored in the *loader heap*, an internal CLR heap that is associated with the AppDomain. The jitter allocates from it, they die when the AppDomain gets unloaded. Lots of nasty little details, like [ThreadStatic] and static member variables of generic types. It is designed so you never have to worry about it. – Hans Passant Apr 30 '17 at 12:07
  • 1
    As a side note, please take a little bit of care when asking questions. There's no need to spam question marks, for example. Your questions will have all the more chance to be answered by someone *donating* their time and effort if you show you've put some of *your* time and effort into your question. – Yannick Meeus Apr 30 '17 at 12:30

2 Answers2

1

First is a value type which is stored on the stack and the second is a reference type which is stored on the heap.

This is a frequently repeated statement but it isn't true. How variables are stored is an 'implementation detail'.

The language spec cannot totally avoid the fact that the instances of reference types are stored on a Heap, but for local variables, be they value types or the references to the instances, the story is not so simple.

 class A { int b; }  // int b is of a value-type but will always live on the Heap

 void M() { int a, b; ... x => a+1; ... }  // a stored different from b

And how static fields are stored is not really specified. Nor is it relevant to a C# programmer.

Virtually they are stored in a static segment, which is neither stack nor heap. But I think they are actually allocated on the Heap as part of the Type. An implementation detail that may vary between runtimes.

H H
  • 263,252
  • 30
  • 330
  • 514
  • And how static fields are stored is not really specified. Nor is it relevant to a C# programmer. it does matter.... if you have understood the GC functionality properly... GC identifies which object is not in use and needs to be garbage collected using roots... When CLR meets static object (class member, variable, or event), it creates a global instance of this object. The object can be accessed during the entire app life time, so static objects are almost never collected. – KamalDeep Apr 30 '17 at 14:00
  • Yes, static reference vars are roots for GC but that has nothing to do with how they are stored. Typing `static` is all there is for a C# programmer. – H H Apr 30 '17 at 15:22
0

Static has nothing to do with memory allocation, static just means that the field can be accessed without creating an instance.

string is a reference type and thus it is allocated on the heap regardless of whether it is static field or non-static field.

areller
  • 4,800
  • 9
  • 29
  • 57