Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Allocation for Variable Declared in Class

As Value type variable allocates memory in Stack where as Reference Type allocates it in Heap.

So how the memory allocated when a value type variable(eg int i =4;) is declared in the reference type (eg. in a class).

How the overall memory allocation works in .net for value type & reference typ, and also value type inside the scope of refence type.

Please explain it or provide any links regarding that.

Thanks

like image 346
usr021986 Avatar asked Sep 08 '11 05:09

usr021986


People also ask

Is memory allocated when a variable is declared?

When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution.

How is memory allocated to variables declared in a structure?

When you declare a variable or an instance of a structure or class. The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory.

Is memory allocated when a variable is declared Java?

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object).

What happens in memory when a variable is declared?

Every time a function is called, the machine allocates some stack memory for it. When a new local variables is declared, more stack memory is allocated for that function to store the variable. Such allocations make the stack grow downwards.


1 Answers

A value type variable allocates memory on the stack whereas a reference type allocates it in heap.

No, that statement is completely wrong. Lots of people believe that, but it is obviously false, as you have discovered.

How is the memory allocated when a value type variable int i = 4; is declared as a field of a reference type?

Clearly you know why your first statement is completely wrong. The integer field of the class cannot be allocated on the stack because the object might live longer than the stack frame.

To understand what is really going on, first you have to realize that there are three kinds of things:

  • value types
  • references
  • instances of reference type

References and instances of reference type are completely different, just as a piece of paper containing my address and my actual house are completely different.

The next thing you have to understand is that there are two kinds of storage: long-term and temporary storage. Long-term storage is usually called "the heap", but I prefer to think of it simply as long-term storage. Temporary storage is usually called "the stack" but that is also misleading because of course there could be multiple stacks, there could be temporaries stored in registers, and so on.

An instance of a reference type occupies memory in the long-term storage. (Sometimes it would be possible to determine that an instance of a reference type is short-lived, and put it in temporary storage, but we do not do this optimization in practice.)

A variable is a storage location which stores either a value of value type or a reference.

Where the storage location of the variable is allocated depends on the lifetime of the variable. If the variable is a local variable known to be of short lifetime, it is allocated from the temporary storage pool. If the variable is known to be of long lifetime (because, say, it is an outer variable of a closure) then it is allocated off the long-term storage pool.

If the variable is a field of a class, we already know that its storage comes from the long-term pool. If the variable is a field of a value type, that value type inhabits storage somewhere; the field inhabits the same storage.

If the variable is an array element, it is allocated off the long-term storage pool; arrays are instances of reference type.

The key to getting your understanding correct is to simply stop believing the myth that whether a variable is of reference or value type affects where the storage is allocated. That is not true and has never been true, and doesn't even make any sense.

The only thing that affects where a variable is stored is how long does the variable live for. Short-lived variables are allocated off the temporary pool -- the stack, or registers -- and long-lived variables are allocated off the long-term storage pool -- the heap.

like image 63
Eric Lippert Avatar answered Oct 09 '22 08:10

Eric Lippert