Re: Boxing and Unboxing ??




Peter Olcott wrote:
"Barry Kelly" <barry.j.kelly@xxxxxxxxx> wrote in message
news:2dmiq2t5l3pr1mlsmetkea1se29hfi721t@xxxxxxxxxx
Peter Olcott wrote:

"Barry Kelly" <barry.j.kelly@xxxxxxxxx> wrote:

So a member function can not add two integer members without unboxing them
first? That would sound like horrendous design.

If you carefully read what I wrote, you'll notice:

From the point of view of C#, an integer (or any other value type) is
only boxed if it's been assigned to a location of type 'object' -
whether local variable, argument or field.

You *cannot*, I repeat *CANNOT*, have two boxed integer members in C# -
the members would need to be of type *object*, not int, in order for
them to be boxed.

-- Barry

I carefully read it, yet, did not fully understand the meaning of all of the
terminology that was used. For one thing, I don't see why there is ever any need
for boxing and unboxing. I know that there is no such need in C++.

That's because in C# (and Java) you _can't_ say:

int x = 3;
int *p = &x;

because the "&" operator simply doesn't exist. You can't take the
address of an arbitrary variable.

I also know that it must somehow support GC, and that is why it is needed.

Well, more to the point, a language that supports garbage collection
can't allow one to take addresses of arbitrary memory locations, as the
garbage collector could then never determine what objects were
referenced and which weren't (because an address into the midst of an
object would then be legal).

Is it something like maintaining a chain of pointers indicating who owns what?

Well, sort of. The GC walks the stack and all static objects, looking
for references to objects on the heap. It then follows references
stored in those objects, etc, until it exhausts the network of
references. Any objects left thus unmarked are available for
collection.

Of course, it's rather more complex than that, but you get the idea. If
you allow references into the midst of objects, then it's much more
difficult to decide what is referenced and what isn't.

In C# and Java, every reference that you can directly manipulate in
code is to a valid object on the heap. That's why, if you want to treat
an int as an object (and thus have a reference to it) then the CLR has
to create an object wrapper for it and put it on the heap.

.



Relevant Pages

  • Re: THIS Resplendent Beauty
    ... House of Justice members, or your references to the five year Plan. ... I ask this of all the SRB moderators is why they really refused to post ...
    (talk.religion.bahai)
  • Re: Attack a sacred Python Cow
    ... When a function attribute is accessed via an instance of the class, it is *wrapped* with a bound method object that basically consists of references to the function and instance. ... Collection/container objects collect/contain references to their members, just as a club roster does, but they only metaphorically 'contain' their members. ... What is true is that functions have a read-only reference to the global namespace of the module in which they are defined. ...
    (comp.lang.python)
  • Re: Disposing by reflection?
    ... > but at a certain point its becoming boring when i have to take care about ... > setting all members in a class to null..sometimes i forgot that also.. ... or references to other objects which implement ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implementation of Aggregation and Composition
    ... references to those members. ... Aggregation is your only option. ... references to those objects are stored. ... Arrays are untyped collections. ...
    (comp.lang.ruby)
  • Re: RAD vs. performance
    ... ref counting is very prone to stalls whereas OCaml's ... errm, I don't keep inverse references or anything, only a count (gathering ... state is used by the mark/sweep collector ('grey' is a conventional holdover ... the initial heap size is 512kB, so this is an overhead of about 64kB ...
    (comp.lang.misc)