Is a boxed value just a pointer to a copy of the value stored in the managed heap?
Asked Answered
W

2

7

In my mind this is what I think of as boxing and unboxing. Nothing more. Can someone confirm that this is correct?

enter image description here

Wallsend answered 23/5, 2013 at 19:37 Comment(43)
@WhileTrueSleep: Is this the wrong forum to post this? Is there another forum for amateurs?Wallsend
@Wallsend i think this a fair question and nice diagrams.Buoyant
Well, yes/no questions are generally a bad idea. On SO we want answers that contain more than two or three letters.Tadtada
@DanielA.White He could provide more info, but it wouldn't be answering the question. The question is, "is this correct?" Either it is, and you say yes, or it's not, and you say no. It didn't ask, "What is boxing?" for which a detailed answer could be given. (Note that that would be a poor question for entirely different reasons though.)Tadtada
@Tadtada - but the question content could also be incorrect and then you could provide further details about why it isn't.Feast
This question needs a lot of work to be re-opened; right now it's a "Yes or No" question with very little for a googler to latch on to. Images don't fare well in search results for text. A better question would be, "If I create an integer and then cast it to an object and cast it again to an integer does the compiler box and then unbox the result? of course, this question has been asked a million times: #4795200Extinctive
@GeorgeStocker - Is this what SO is about? For future googlers/visitors to latch onto in the future? I always thought SO was about community experts sharing their knowledge when a question has been asked. IMO this question should not be closed - if there is an answer on SO that answers this question directly, please link it, otherwise let the the OP modify his question to conform to the SO requirements.Feast
@DarrenDavies Yes, this is what Stack Overflow is about. Remember, we aim to be a "Repository of useful programming information". We can't be useful if our repository can't be searched. A question is closed specifically so a user can take some time and improve it.Extinctive
@DarrenDavies, the benefit of "googlers" is precisely what this site is about. This is certainly not a site intended to answer the question purely for the benefit of the one that asked it. There exists several forums that do that already. When the question can only help the single individual, we have recourse for that: close as Too Localized.Straka
@GeorgeStocker - but we can be useful be answering the OP's question. I have seen many times questions with a lot lower quality in content (sometimes not even English, no code examples) pass through the threshold and remain open.Feast
@DarrenDavies If you know of any questions that ought to be closed then either vote to close or flag for the appropriate close reason. If you are unsure if they should be closed then ask a question on meta/chat and others can help you figure it out (and probably also vote themselves if it should be closed). The fact that some other question that should be closed was missed doesn't mean that we shouldn't close this question, given that it meets the criteria for closure.Tadtada
@user414076 - take Jon Skeet's famous answer - Why is subtracting these two times (in 1927) giving a strange result? ..... how many times will a googler ask that? Should that question not be closed? Seems more like a novelty question for the site rather to help future visitors like user414076 states. This question was closed on the basis of it has difficult to tell what is being asked here, I personally don't see how it is when the user describes what is required in the title and provides an illustration.Feast
@DarrenDavies I could always re-open it and then reclose is as 'too localized', but that would wipe off your re-open votes. Are you really sure you want me to do that?Extinctive
@GeorgeStocker - Well it should have been marked as that in the first place, which kind of proves my point about closing it (People just hit close and obviously selected the incorrect reason). However I don't mean to come across as an a$$ and would be grateful if you can leave it as it stands and hopefully it will get reopened as I do think it is a useful question.Feast
@GeorgeStocker It seems you'll have to, yes.Tadtada
@servy Flag it for moderator attention or vote to close yourself; It's not a great idea for me to re-close it (even though it really is too localized because the text for the question isn't useful for a googler). The improved title helped, though.Extinctive
@Tadtada - are you really going to flag for moderators attention/attempt to close it again after the community has decided to keep it open?Feast
@GeorgeStocker I've already voted to close as well, which was why I pinged you ;)Tadtada
@DarrenDavies Sure. By that logic why would someone vote to reopen when the decision was made to close it? Controversial questions frequently get closed/reopened several times. It's just how life works on SO.Tadtada
@Tadtada - showing up as no closed flags on my screen. It was directly closed by yourself, some other guy and a moderator, otherwise it would have required more votes. I've been on SO around about the same amount of time as yourself so I don't need a tutorial on how SO works ty, although I think it's a bit petty more than anything to close a question (again) when it has been reopened by the community just to get your way.Feast
@DarrenDavies If I wanted the question closed just because I had voted to close earlier then yes, you would be correct. If I sincerely believe that this question ought to be closed, then it's not being petty at all. I sincerely believe that the question ought to be closed. Why do you think that just because you've voted to reopen the question that nobody has the right to think it should be closed? If 5 more people do vote to close would you think it petty for you to want the question re-opened?Tadtada
@Tadtada - no not at all, but that wasn't the case. 5 people didn't vote to close it. Also it was closed under the wrong status not a real question which proves that you were either: Trigger happy or just wanted the question closed in the first place.Feast
@DarrenDavies Or that I sincerely believe that it's appropriate to close this question as Not A Real Question. I sincerely believe that it's appropriate to close this question as Not A Real Question. It could also be closed as Too Localized as well; I would think that both apply. You seem to be assuming malice or ignorance on my part. Neither is the case. I put a lot of thought into my decision to vote to close this question.Tadtada
@Tadtada - This question was closed on the basis of it has difficult to tell what is being asked here, it is ambiguous etc. However it was clear what the OP required and they event provided a diagram, it was closed on the wrong grounds. If it was originally closed on too localized fair play but like I state how is that any different to the famous Jon Skeet answer? How will that help future visitors? If the question was a duplicate, please link it to a post that provides the OP information they can refer to.Feast
@DarrenDavies If you think the question really is Too Localised then why did you vote to reopen? Why are you not voting to close as TL if you think that it applies? As to Jon's question, are you saying that nobody, ever, for the rest of time will ever deal with a timespan that crosses the particular date in question in the appropriate timezone? It was [eventually] closed as TL because the behavior changed, not because it's not useful. I never said this question was a duplicate, nor did I vote as such.Tadtada
@DarrenDavies As to why I do think it's NARQ, it's because I don't see how it's possible to answer it with an answer that would both properly answer the question as well as live up to SO's quality standards, given that it's a yes/no question. The existing answers are essentially treating the question as if it read, "What is boxing?" and that's too broad for an SO question.Tadtada
@DarrenDavies, it's hard to comment on how things compare to "the famous" answer since I don't know what it is. However, as noted often enough meta, past questions left open are not indicators of future questions being accepted. SO evolves.Straka
@Tadtada - I don't think it's that localized, not in a way I'd vote to close it. I'm not saying that no one will deal with that timespan, however C# boxing is a much more common issue than that specific question and will help future visitors which is what user414076's argument was about.Feast
@user414076 This is the question he's referring to.Tadtada
@DarrenDavies It sounds like you do believe that the question pretty much is, "What is boxing?" or at least that's what you expect answers to state. That question is very much NARQ.Tadtada
@user414076 - 3years + on SO and you've never seen the highest reputation's users most popular anwser. I find it hard to believe, however thats away from the point. That answer will not help future 'googlers' as you previously state.Feast
@DarrenDavies As I said before, are you asserting that nobody, ever, for the rest of time will ever end up using a timespan that crosses the time mentioned in that answer that might be confused as to why the difference in times is ~5 minutes less than it should be? You're saying that that one person is the only one who can ever possibly have that problem?Tadtada
@Tadtada - no I don't believe that. Otherwise it would be marked as a duplicate.Feast
@DarrenDavis, I dispute that nobody else could be bitten by the specific timechange in Shanghai nor other possibly time discontinuities, as another answer states it. You could easily envision data analysis problems involving historical dates that would reproduce the problem.Straka
@DarrenDavies Why would it be marked as a duplicate? Someone else could have a timespan crossing that time, be confused as to why it doesn't seem right, search Google for it, come upon that question, and see an explanation.Tadtada
@Tadtada - no but from what user414076 states ..the benefit of "googlers" is precisely what this site is about. This is certainly not a site intended to answer the question purely for the benefit of the one that asked it...... I entirely disagree. I have provided many C#, SQL, JavaScript, jQuery etc custom solutions that answered the OP's question and would provide no benefits to future visitors.Feast
@Tadtada - I wouldnt mark that question as a duplicate. I was stating I'd mark this one as a duplicate if it was titled What is boxing.Feast
@DarrenDavies Well, as the site's founders and owners currently disagree with your viewpoint, and have very publicly and explicitly stated that SO does exist to create a repository of knowledge and not just a means to help someone asking a single question, I'd say that it's you that'll need to learn to change your behavior, not the rest of the site.Tadtada
@Tadtada - not really, thats why the question was reopened because the community agreed with my decision.Feast
@DarrenDavies Okay. Regardless the question is also not a real question; whatever you closed it as a dup of would also need to be closed for that same reason.Tadtada
@DarrenDavies 4 other people disagreed that it was NARQ. You yourself have said that you consider the question Too Localized. You just don't care. That doesn't mean everyone else doesn't care.Tadtada
@Tadtada - it wasnt 4 other people, it was you, another person and the moderator. I count that as 3. And it was reopened based upon a community vote. I personally don't see the question as too localized, if I did I would vote to close it. As it stands the question is open so therefore I must be correct (the community agrees with me that the question is a real question, not ambiguous etc). Until the question is closed again I remain correct.Feast
If you want to hash out the merits of this question, meta is a better place than here.Extinctive
R
6

No.

While the general idea is right, it isn't quite correct. Boxed values are complete objects which conform to the memory layout for System.Object. This means a v-table pointer (which provides the type-specific overloads for System.Object virtual methods such as Equals and GetHashCode as well as serving as a type tag to prevent unboxing to an incompatible type), and an (optional) synchronization monitor.

The actual address stored in a handle to boxed value doesn't point to the content, but to the attached metadata.

Renaud answered 23/5, 2013 at 19:46 Comment(1)
Good answer. Additionally supplies OPs demand for "Yes" / "No"Kammerer
L
1

Every value-type in .net actually defines two different kinds of things: a storage-location type, and a heap-object type. The heap type will, from an external point of view, behave much like a class

class Holder<T> where T:struct
{
  public T This;
  public override String ToString() { return This.ToString(); }
  public override bool Equals(object other) { return This.Equals(other); }
  etc.
}

which wraps exposes all of the value type's public methods. The heap type will have some additional magic, however, since it will implement any interfaces which the underlying value-type implements [as above, by wrapping calls to the value-type method]. Further, the Type associated with a heap object will be the same as the Type associated with a storage location.

Note that value-type storage locations hold instances of a value types and behave with value semantics, but a reference-type storage locations hold heap object references (or else null) and behave with mutable reference semantics (note that all value types, when boxed, behave as mutable reference types); the system does not terribly-conveniently provide for mutation, but boxed value-type instances can be mutated without the underlying value type having any say in the matter.

Lotic answered 23/5, 2013 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.