Re: Value Types and Reference Types




"Dom" <dolivastro@xxxxxxxxx> wrote in message
news:93ea4342-9661-452c-824f-d953451aacb5@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Let me explain my mental image, and then tell me where I've gone
wrong.

There is a stack and a heap. A reference type sits on the heap, and
its address sits on the stack. When you pass it to a method, you pass
the contents of the stack, which is the same as passing the address of
the object.

A value type just sits on the stack. When you pass it to a method,
you pass the contents of the stack, just as before. Only now the
method knows not to "dereference" it.

Now, here's the problem. I had a method that received an integer, and
I wanted to modify it so it could also receive a DateTime. I could
have overloaded the method, but because I am stuck in VB-think, i just
changed the argument from integer to object, assuming it worked like
VB's "Any". And it worked. But according to my mental image, the
method should have taken the value sent and tried to look it up on the
heap, and then fail. Or the compiler should have complained about a
mismatch.


All types derive from System.Object. Value types are special types that can
"fit" on the stack. The compiler basically has a list of types that are
special and will stick them on the stack instead of the heap when used.
(struct is a general type that gets this priviledge but is made up of the
more simple types)

BUT, if those value types are told to be put on the heap then they will.
i.e., If you tell the compile you want to treat it as a ref type then it
will "convert" it to a reference type and use the heap instead.

e.g.,

int x;

gives a value type x.

System.Int32 x;

will give a referenec type x;

Basically the compiler says "Oh, x is a value type so we can optimize it and
put it on the stack". If you do int x and then object y = (object)x. Then
the compiler will convert x into an object(after all, its base type is an
object since they all derive from it). This is slow cause now the compiler
has to create the object on the heap and its called boxing.

I think if you think of it as an optimization of special types then its not
a difficult concept. Basically using the special keywords you tell the
compiler they are value types... which just means that they are to be
optimized and put on the stack rather than the heap. And remember that if
you convert from optimized to non-optimzed or vice versa that there is a
penalty.

Other than that they pretty much are exactly the same. Again, int x and
Int32 x; are pretty much identical except for speed/memory issues. The heap
and stack are just memory. An object has more overhead of course and thats
why value types are faster. No reason to have overhead for something like

int x = 3;
int y = 9;
int z = x * y;

The compiler will simply do that in the most optimized way which is using
the ALU of the cpu(done in 1-2 instructions in assembly).

if you do

Int32 x = 3;
Int32 y = 9;
Int32 z = x * y;

then the compiler will treat them as full blown objects and have to find the
operator *, call the constructors, allocate memory, etc... Theres no need
for that. (although its a simple example so...)


Hope that helps,
Jon







.



Relevant Pages

  • Re: GNAT GPL for Mac OSX stack checking problem
    ... The compiler and GPS seem to work, it is the generated test driver ... documentation to increase it under macos, but traditionally, the stack ... Linux GNAT uses the heap, Rational Apex uses the heap, the VADs Ada83 ... The above test driver would not have run on older Linux releases using ...
    (comp.lang.ada)
  • Re: Direction in which the process stack grows
    ... >> int main ... > would force the compiler to create local variables in the same ... > sequence on the stack as you define them in the source code. ... > with gcc and either without optimization or inlining explicitely ...
    (comp.unix.programmer)
  • Re: calling convention stdcalll and cdecl call
    ... the compiler can somehow figure out how the function accesses its parameters, it can figure out how many there are, and pop the stack correctly without any additional information from the caller. ... int find{ ... This is similar to how it must take some action for a __stdcall member function, ...
    (microsoft.public.vc.language)
  • Re: where do the automatic variables go ?
    ... and bss segments etc...but there is notthing like stack and heap ... Stack variables are implied by the compiler (the compiler generates code ... int foo ...
    (comp.os.linux.misc)
  • Re: Value Types and Reference Types
    ... the compiler doesn't have a list of them. ... So your saying that an int is not special? ... put it on the stack". ... Int32 x; are pretty much identical except for speed/memory issues. ...
    (microsoft.public.dotnet.languages.csharp)