views:

210

answers:

5

Hi all,

as we know reference types are always stored on heap and value types are in stack memory.

e.g.

public Class A{
  public int myInt=10;
  public void Display(){
  }
}

here, if i create object of class A it goes in heap and myInt goes in stack right..?

now, how class object (heap) interact with myInt public variable (stack)?

can anybody please explain it..

+4  A: 

I'm afraid your assumption is wrong if you're talking about .NET. Value types are only stored on the stack when they are not part of an instance of a reference type. I.e. your myInt is stored as part of any instance of A on the heap.

Brian Rasmussen
thanks, but then where veriables values types stored, if they are part of reference type.. in above example where would be stored "myInt" veriable.
Rahul Somwanshi
`myInt` is stored as a field of the instance of `A`. I.e. the value 10 is part of the memory on the heap that belongs to the instance of the type A.
Brian Rasmussen
ok,that means all the value types(with any access modifiers) defined in the reference type (Class) are goes on heap..and veriables defined in struct are goes in stack...?
Rahul Somwanshi
A: 

Generally, locating variables is compilers task.

Gabriel Ščerbák
+1  A: 

as we know reference types are always stored in heap

Ask yourself: which part of the reference type is stored on the heap? What kind of memory? What does the reference type consist of?

– Primarily, it consists of the memory of its member variables.1) These are the data that is stored on the heap. So in your example, that would be the myInt variable.

Value types are only stored on the stack (as you assumed) if they are local variables inside a method, or their parameters. This is what the stack is there for: storing local variables and parameters (and the return pointers of function calls).


1) And also sometimes a so-called vtable which stores the addresses of virtual functions. But this is irrelevant for this question.

Konrad Rudolph
A: 

If you create an instance of class A, say A obj=new A(); an object of type A is created on the heap. This object on the heap constitutes of the instance varialbe myInt also on the heap along with other member variables, if there were any. And you are referring to this object on the heap with a reference variable obj which will be on the stack. For example,

class Mainclass{
main(){
A obj=new A();
//......all other code......
}
}

EDIT:

Must-read for .net developers:
Memory in .Net and Reference and values.

Zaki
A: 

To Summarize:

The stack is always used to store the following two things:

  • The reference portion of reference-typed local variables in the functions and their parameters.
  • Value-typed local variables and method parameters (structs, as well as integ$ers, bools, chars, DateTimes, etc.)

The following data is stored on the heap:

  • The content of reference-type objects.
  • Anything structured inside a reference-type object.

E.g.

Main() { MyClass obj = new MyClass(); }

Class MyClass { int32 i; Dataset dst;

private void MyMethod(int32 j, dataset dst2) { int32 k; Dataset dst3; }

Here are the memory allocation details:

  1. obj which is a object reference - Stack
  2. Instance of Myclass to which obj variable is pointing - Managed Heap
  3. Value type Member variable i - Managed Heap
  4. dst which is a dataset object reference - Managed Heap
  5. Instance of dataset to which dst variable is pointing - Managed Heap
  6. Value type parameter j - Stack
  7. dst2 which is a dataset object reference - Stack
  8. Instance of dataset to which dst2 variable is pointing - Managed Heap
  9. Value type Local variable k - Stack
  10. dst3 which is a dataset object reference - Stack
  11. Instance of dataset to which dst3 is pointing - Managed heap

I think I've covered all permutations and combination. Hope this helps!

Rasik Bihari