The CLR doesn't create structures using new(), it just zeroes out the structure. When you say SomeTruct x = new SomeStruct() // Default Constructor; it means initialize my var to zeroed values.
And BTW, it is not necesary to alocate space for local variables of value type, they exist in the *prealocated* stack space. When a method is entered wich contains local vars, from the next available stack space, some portions of it are reserved for those variables, and again I must point out that no alocations are made, the stack is prealocated. .locals init (int32 V_0) -- This is where stack space for the local variable is reserved There is a special newobj IL opcode wich is used when alocations are made and tha's the only way to alocate mem in a managed environment. ldc.i4,0 opcode does't create a int, it just loads an int32 into the evaluation stack. It is equivalent to mov eax, 0 assemly intruction for example. Which loads zero into eax register. On 7/4/06, Girish Jain <[EMAIL PROTECTED]> wrote:
**For the following C# code** int k = new int(); k = 100; Console.WriteLine(k.ToString()); **the following IL code was generated.** method public hidebysig static void Main() cil managed { .entrypoint // Code size 18 (0x12) .maxstack 1 .locals init (int32 V_0) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: ldc.i4.s 100 IL_0004: stloc.0 IL_0005: ldloca.s V_0 IL_0007: call instance string [mscorlib]System.Int32::ToString() IL_000c: call void [mscorlib]System.Console::WriteLine(string) IL_0011: ret } and looking at the above it is clear that it creates two ints (the reason of which is still not clear to me as to why it is creating two ints?). Also, why would anyone need to use new operator with value types (unless looping over a collection and adding items to it by using the same variable but creating a new object each time) valType valTypeInstance; for (...) { valTypeInstance = new ValType(); valTypeInstance.prop1 = <some value>; valTypeInstance.prop2 = <some value>; coll.Add(valTypeInstance); } Thanks for taking time out for this.. Regards, Girish Jain ----- Original Message ----- From: "Chris Anderson" <[EMAIL PROTECTED]> To: <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> Sent: Tuesday, July 04, 2006 1:16 PM Subject: Re: [ADVANCED-DOTNET] New Type decision Criterias In the 2nd case, you are actually creating 2 ints. i=10 is creating a new int (10) and assigning it to i, replacing the new int() it previously held. Having said that, I believe the new int() was in the stack. OTOH this would put it in the heap as it would be first created on the stack, then copied (boxed) to the heap due to the object reference: object o = new int(); -----Original Message----- From: "Girish Jain"<[EMAIL PROTECTED]> Sent: 04/07/06 06:45:36 To: "ADVANCED-DOTNET@DISCUSS.DEVELOP.COM"<ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> Subject: Re: [ADVANCED-DOTNET] New Type decision Criterias I was just going through the archive and found this... > I think that, as the struct is allocated in the stack (if we don't use > the "new" command to allocate it) Just to confirm... I dont think if you use the new command, the value type instance would be allocated on the heap.. int i = 10; OR int i = new int(); i = 10; In both the above cases, i is allocated on the stack.. Let me know..if I am wrong. Regards, Girish Jain ----- Original Message ----- From: "Jean-Michel Reghem" <[EMAIL PROTECTED]> To: <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> Sent: Thursday, January 12, 2006 7:20 PM Subject: Re: [ADVANCED-DOTNET] New Type decision Criterias > Paul Cowan wrote: >> Than Jean-Michel. >> >> I did know that but in what scenario would I want to use the value >> type over >> the reference type. >> >> Are we strictly talking about performance. Is this the only reason?? >> They >> just seem like a quick and dirty class. >> > I think that, as the struct is allocated in the stack (if we don't use > the "new" command to allocate it) , there is no garbage collector and > thus, yes, it is done for performance ... > > =================================== > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com -----Unmodified Original Message----- I was just going through the archive and found this... > I think that, as the struct is allocated in the stack (if we don't use > the "new" command to allocate it) Just to confirm... I dont think if you use the new command, the value type instance would be allocated on the heap.. int i = 10; OR int i = new int(); i = 10; In both the above cases, i is allocated on the stack.. Let me know..if I am wrong. Regards, Girish Jain ----- Original Message ----- From: "Jean-Michel Reghem" <[EMAIL PROTECTED]> To: <ADVANCED-DOTNET@DISCUSS.DEVELOP.COM> Sent: Thursday, January 12, 2006 7:20 PM Subject: Re: [ADVANCED-DOTNET] New Type decision Criterias > Paul Cowan wrote: >> Than Jean-Michel. >> >> I did know that but in what scenario would I want to use the value >> type over >> the reference type. >> >> Are we strictly talking about performance. Is this the only reason?? >> They >> just seem like a quick and dirty class. >> > I think that, as the struct is allocated in the stack (if we don't use > the "new" command to allocate it) , there is no garbage collector and > thus, yes, it is done for performance ... > > =================================== > This list is hosted by DevelopMentor(r) http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com > =================================== This list is hosted by DevelopMentor(r) http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentor� http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com