Re: Boxing and Unboxing ??
- From: Barry Kelly <barry.j.kelly@xxxxxxxxx>
- Date: Wed, 17 Jan 2007 23:14:33 +0000
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/
.
- References:
- Re: Boxing and Unboxing ??
- From: Bob Graham
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Bob Graham
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Jesse McGrew
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Boxing and Unboxing ??
- From: Barry Kelly
- Re: Boxing and Unboxing ??
- From: Peter Olcott
- Re: Boxing and Unboxing ??
- Prev by Date: Re: Detect inactivity
- Next by Date: Re: Boxing and Unboxing ??
- Previous by thread: Re: Boxing and Unboxing ??
- Next by thread: Re: Boxing and Unboxing ??
- Index(es):
Relevant Pages
|