1

I know that all the primitive types like int are value types and are created on the stack where as strings, object arrays etc are reference types and are created on heap.

My doubt is as follows. If I create an object of an integer like int i = new int(); then since its an object, will it be created on stack or heap?

Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • You also need to read about [boxing and unboxing](http://msdn.microsoft.com/en-us/library/yz2be5wk(v=vs.110).aspx) for a better appreciation of the possibilities. – Roger Rowland Jun 25 '13 at 09:28
  • 6
    "I know that all the primitive types like int are value types and are created on the stack" is already incorrect. – Jon Jun 25 '13 at 09:29
  • Take a look at this, http://stackoverflow.com/questions/2559271/are-these-objectss-references-on-the-stack-or-on-the-heap . If that isn't helping you then this shorter question will answer your question http://stackoverflow.com/questions/5996751/is-an-int-inside-a-class-stored-on-the-stack-or-the-heap . Your question is a possible duplicate of both, please research before asking. – Philip Gullick Jun 25 '13 at 09:30
  • 1
    a `new int()` doesn't create an object-version of an int, it's just the default value of int, which is `0`. Compare to `new DateTime()` - that's still a struct (value type). – Hans Kesting Jun 25 '13 at 09:31
  • 1
    Your doubt is not demonstrating why your belief is completely wrong but a new int array would. Ints do not live only on the stack. Arrays are one counterexample illustrating this fact. – Eric Lippert Jun 25 '13 at 22:37

4 Answers4

4

Short answer: Implementation detail.

Long answer: Implementation detail - completely up the implementation.

It can be created on either.. depending on how it is created. Is it a member of a class? Is it a local variable? ..

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
4

Eric Lippert states that you should not really care about it. Read it here.

Int is a value type and it is, almost always, created in the stack.

It is not created in the stack when the integer is a variable inside a class that you instantiate: in that case the instance is in the heap and all its members, including value types.

The link i provided above explains it much better than i do, of course

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
3

It can be created on the stack, heap or stored in a register:

The way in the past I've usually pushed back on this myth is to say that the real statement should be "in the Microsoft implementation of C# on the desktop CLR, value types are stored on the stack when the value is a local variable or temporary that is not a closed-over local variable of a lambda or anonymous method, and the method body is not an iterator block, and the jitter chooses to not enregister the value."

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
2

int i = new int(); is equivalent to int i = 0;, so no, it does not make a difference with respect to stack/heap allocation.

Note that value types are not always created on the stack, see this fine blog entry for a detailed discussion:

Heinzi
  • 167,459
  • 57
  • 363
  • 519