When you create an instance of a class with the new operator, memory gets allocated on the heap. When you create an instance of a struct with the new operator where does the memory get allocated, on the heap or on the stack ?
When you create an instance of a class with the new operator, memory gets
Share
Okay, let’s see if I can make this any clearer.
Firstly, Ash is right: the question is not about where value type variables are allocated. That’s a different question – and one to which the answer isn’t just ‘on the stack’. It’s more complicated than that (and made even more complicated by C# 2). I have an article on the topic and will expand on it if requested, but let’s deal with just the
newoperator.Secondly, all of this really depends on what level you’re talking about. I’m looking at what the compiler does with the source code, in terms of the IL it creates. It’s more than possible that the JIT compiler will do clever things in terms of optimising away quite a lot of ‘logical’ allocation.
Thirdly, I’m ignoring generics, mostly because I don’t actually know the answer, and partly because it would complicate things too much.
Finally, all of this is just with the current implementation. The C# spec doesn’t specify much of this – it’s effectively an implementation detail. There are those who believe that managed code developers really shouldn’t care. I’m not sure I’d go that far, but it’s worth imagining a world where in fact all local variables live on the heap – which would still conform with the spec.
There are two different situations with the
newoperator on value types: you can either call a parameterless constructor (e.g.new Guid()) or a parameterful constructor (e.g.new Guid(someString)). These generate significantly different IL. To understand why, you need to compare the C# and CLI specs: according to C#, all value types have a parameterless constructor. According to the CLI spec, no value types have parameterless constructors. (Fetch the constructors of a value type with reflection some time – you won’t find a parameterless one.)It makes sense for C# to treat the ‘initialize a value with zeroes’ as a constructor, because it keeps the language consistent – you can think of
new(...)as always calling a constructor. It makes sense for the CLI to think of it differently, as there’s no real code to call – and certainly no type-specific code.It also makes a difference what you’re going to do with the value after you’ve initialized it. The IL used for
is different to the IL used for:
In addition, if the value is used as an intermediate value, e.g. an argument to a method call, things are slightly different again. To show all these differences, here’s a short test program. It doesn’t show the difference between static variables and instance variables: the IL would differ between
stfldandstsfld, but that’s all.Here’s the IL for the class, excluding irrelevant bits (such as nops):
As you can see, there are lots of different instructions used for calling the constructor:
newobj: Allocates the value on the stack, calls a parameterised constructor. Used for intermediate values, e.g. for assignment to a field or use as a method argument.call instance: Uses an already-allocated storage location (whether on the stack or not). This is used in the code above for assigning to a local variable. If the same local variable is assigned a value several times using severalnewcalls, it just initializes the data over the top of the old value – it doesn’t allocate more stack space each time.initobj: Uses an already-allocated storage location and just wipes the data. This is used for all our parameterless constructor calls, including those which assign to a local variable. For the method call, an intermediate local variable is effectively introduced, and its value wiped byinitobj.I hope this shows how complicated the topic is, while shining a bit of light on it at the same time. In some conceptual senses, every call to
newallocates space on the stack – but as we’ve seen, that isn’t what really happens even at the IL level. I’d like to highlight one particular case. Take this method:That ‘logically’ has 4 stack allocations – one for the variable, and one for each of the three
newcalls – but in fact (for that specific code) the stack is only allocated once, and then the same storage location is reused.EDIT: Just to be clear, this is only true in some cases… in particular, the value of
guidwon’t be visible if theGuidconstructor throws an exception, which is why the C# compiler is able to reuse the same stack slot. See Eric Lippert’s blog post on value type construction for more details and a case where it doesn’t apply.I’ve learned a lot in writing this answer – please ask for clarification if any of it is unclear!