Re: Boxing and Unboxing ??



Peter Olcott wrote:

Peter, what you wrote doesn't make sense. It implies either you don't
know what boxing means, or you don't know what a reference type is, or
both.

If you simply keep everything in a box

Boxes are on the heap. They are on the heap to avoid lifetime issues. If
they were not on the heap, and were instead on the stack for locals,
then one could store such a local in a global structure and violate
memory safety. For example, permitting that would permit the following
(in C for your ease of understanding):

---8<---
#include <stdio.h>

static int *value;

void store(int *x)
{
value = x;
}

int *retrieve(void)
{
return value;
}

void do_store(void)
{
int x = 42; // here's my local
store(&x); // here I am passing it by reference (boxed)
}

void recurse(int count)
{
if (count > 0)
recurse(count - 1);
}

int main(void)
{
do_store();
recurse(10); // trashing stack
printf("%d\n", *retrieve()); // whups! CORRUPTED!
return 0;
}
--->8---

We don't store fundamental types like 'int' on the heap for performance
reasons. That's why they are value types. Value types are usually copied
instead of passed by reference. When they are passed by reference (via
'ref'), then their usefulness is severly constrained in order to avoid
the above problem (demonstrated in the C program).

, then there is no boxing and unboxing
overhead, merely boxing initialization.

Small value types such as integer and
double can have the functions that use them serve as their box.

Functions cannot serve as a box. Functions are code. Boxes are objects
allocated on the heap. Functions are not mutable objects. You can't
store values inside functions.

-- Barry

--
http://barrkel.blogspot.com/
.



Relevant Pages

  • Re: Abstract class variables question
    ... I think I understand boxing a little better now. ... the object that is on the heap. ... value types are copied to the heap and made into an object and reference ... String types are already reference types and all we are doing when we do ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class variables question
    ... value can be anything (bool, int, string, etc). ... the heap and _objCurrent is just a pointer to that location. ... reference types, which means that all classes are allocated from the heap. ... As far as boxing and the heap go, I assumed it was a pointer to the heap ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Boxing and Unboxing of Value-Types when passed to a method call
    ... I believe that this would act the same as passing an int as int* in C. ... Whether there is heap space allocated depends very much on where the ... myMethod will be passed a pointer to the memory location ... The pointer (reference) will ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ref parameter for any object
    ... create a new instance of object in the heap. ... heap the int. ... what happen is that with a ref parameter you cannot do this, ... this is needed cause otherwise the reference used is lost at return, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: int number = new int();
    ... a copy of this actual value number is put on the heap. ... If you box an int like. ... // here number is still a value type of type Int32 but is ... reference number which is stored on the heap. ...
    (microsoft.public.dotnet.languages.csharp)