🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
6 messages
6 total messages Started by Kolja Mon, 19 Jun 2006 14:39
About "int" value storage.
#205
Author: Kolja
Date: Mon, 19 Jun 2006 14:39
8 lines
1524 bytes
I have one stupid question which do not gove me a rest.
Given that "int" value datatype is represented by "struct" in unboxed 
condition and "class ValueType" when in boxed condition.
Let us write in our progam: "int i = 5;" . Now plz tell me WHERE in both 
struct or class representation store the value "5" of "int i" itself? I 
tried to disassemble the "int i" through Reflection and found some field 
"m_value" in it. I wonder if is it?
Thanks, Kolja. 
Re: About "int" value storage.
#207
Author: Marc Rohloff TeamB
Date: Mon, 19 Jun 2006 23:07
16 lines
1763 bytes
On Mon, 19 Jun 2006 18:39:22 +0400, Kolja wrote:

> I have one stupid question which do not gove me a rest.
> Given that "int" value datatype is represented by "struct" in unboxed 
> condition and "class ValueType" when in boxed condition.
> Let us write in our progam: "int i = 5;" . Now plz tell me WHERE in both 
> struct or class representation store the value "5" of "int i" itself? I 
> tried to disassemble the "int i" through Reflection and found some field 
> "m_value" in it. I wonder if is it?

It isn't really a struct when its unboxed, it is just the way the 
runtime sometimes represents it.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Re: About "int" value storage.
#209
Author: Kolja
Date: Tue, 20 Jun 2006 08:09
34 lines
2763 bytes
"Marc Rohloff [TeamB]" <"on request"> wrote in message 
news:15m76f8teid2h$.dlg@dlg.marcrohloff.com...
> On Mon, 19 Jun 2006 18:39:22 +0400, Kolja wrote:
>
>> I have one stupid question which do not gove me a rest.
>> Given that "int" value datatype is represented by "struct" in unboxed
>> condition and "class ValueType" when in boxed condition.
>> Let us write in our progam: "int i = 5;" . Now plz tell me WHERE in both
>> struct or class representation store the value "5" of "int i" itself? I
>> tried to disassemble the "int i" through Reflection and found some field
>> "m_value" in it. I wonder if is it?
>
> It isn't really a struct when its unboxed, it is just the way the
> runtime sometimes represents it.
>
> -- 
> Marc Rohloff [TeamB]
> marc rohloff -at- myrealbox -dot- com

Can you elaborate?

All specifications which I red tell that the value type is a struct in 
unboxed form. It is stored in thread stack. See this: 
http://msdn2.microsoft.com/en-us/library/s1ax56ch(VS.80).aspx. Then this: 
http://msdn2.microsoft.com/en-us/library/system.int32.aspx. The System.Int32 
struct.
When it is boxed then it is moved to Heap and becomes the ValueType class.

I really confused where this System.Int32 struct stores accual value? Notice 
that if I make my own struct then I create the included members in my struct 
which carries values.
But I really wonder where Microsoft hides the value of int in System.Int32? 
I tried Reflection to clarify it and found the "m_value" member of 
System.Int32? Is this it? 
Re: About "int" value storage.
#211
Author: Marc Rohloff TeamB
Date: Tue, 20 Jun 2006 23:13
19 lines
1841 bytes
On Tue, 20 Jun 2006 12:09:24 +0400, Kolja wrote:

> Can you elaborate?

Internally (i.e deep down and out of site) the CLI source represents
an int as a struct with a single field, which to all intents and
purposes is the same thing (they are the same size and layout and
could be treated either way).

However the C# compiler (as well as ILASM) hides this fact from you.
It is either an int (unboxed) or an object that looks just like an
integer. 

If I were you I would forget that it is a struct, it is just an
internal implementation detail.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Re: About "int" value storage.
#212
Author: Kolja
Date: Wed, 21 Jun 2006 11:01
97 lines
4344 bytes
I disassebled my test example by Reflector and had found that compilator 
creates each variable as class object (struct 'int' too). But for common 
classes it inserts the "newobj" command despite of there are already created 
one object for each object variable. For me it seems that  Netframework 
squanders the memory.

For example:

class Program
{
    class myclass : System.Data.DataSet
    {
        int k;
    }
    static void Main(string[] args)
    {
        int i = 1;
        myclass o = new myclass();
        Console.WriteLine(i.ToString());
        Console.WriteLine(o.GetType().ToString());
    }
}

The implementation:

.class nested private auto ansi beforefieldinit myclass
      extends [System.Data]System.Data.DataSet
{
      .method public hidebysig specialname rtspecialname instance void 
.ctor() cil managed
      {
      }
      .field private int32 k
}

.method private hidebysig static void Main(string[] args) cil managed
{
      .entrypoint
      .maxstack 1
      .locals init (
            [0] int32 i,
            [1] ConsoleApplication1.Program/myclass o)
      L_0000: nop
      L_0001: ldc.i4.1
      L_0002: stloc.0
      L_0003: newobj instance void 
ConsoleApplication1.Program/myclass::.ctor()
      L_0008: stloc.1
      L_0009: ldloca.s i
      L_000b: call instance string int32::ToString()
      L_0010: call void [mscorlib]System.Console::WriteLine(string)
      L_0015: nop
      L_0016: ldloc.1
      L_0017: callvirt instance [mscorlib]System.Type object::GetType()
      L_001c: callvirt instance string object::ToString()
      L_0021: call void [mscorlib]System.Console::WriteLine(string)
      L_0026: nop
      L_0027: ret
}

Seems like '.locals init' creates full copies of 'int32' and 'myclass' but 
not pointers. I conclude that from how it calls 'i.ToString'.

L_0009: ldloca.s i
L_000b: call instance string int32::ToString()

It uses address of 'i' like if whole object is already created. Can you 
confirm am I right?

BTW one more conclusion. Since the address of  variable 'i' references the 
address of structure 'int i' then the value of variable 'i' is left 
nonusable. So I think this value should be the integer number the varialbe 
'i' represents. So despite the 'i' is the variable of complex data structure 
it values the integer number but not address of the structure 'int i'.
Am I right?

"Marc Rohloff [TeamB]" <"on request"> wrote in message 
news:aguodigtf9ee$.dlg@dlg.marcrohloff.com...
> On Tue, 20 Jun 2006 12:09:24 +0400, Kolja wrote:
>
>> Can you elaborate?
>
> Internally (i.e deep down and out of site) the CLI source represents
> an int as a struct with a single field, which to all intents and
> purposes is the same thing (they are the same size and layout and
> could be treated either way).
>
> However the C# compiler (as well as ILASM) hides this fact from you.
> It is either an int (unboxed) or an object that looks just like an
> integer.
>
> If I were you I would forget that it is a struct, it is just an
> internal implementation detail.
>
> -- 
> Marc Rohloff [TeamB]
> marc rohloff -at- myrealbox -dot- com 
Re: About "int" value storage.
#214
Author: Marc Rohloff TeamB
Date: Thu, 22 Jun 2006 01:51
11 lines
1654 bytes
On Wed, 21 Jun 2006 15:01:40 +0400, Kolja wrote:

> I disassebled my test example by Reflector ...

The value of 'i' stored is not a pointer it is the actual value on the 
stack. In this example it is never treated as a pointer or as an 
object, even though you are calling a method on it.

-- 
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads