Re: Newbie Question about Value and Reference Types

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On 2006-04-21, Gabor Törö <gfm@xxxxxxxxxx> wrote:

You're pretty much correct about all this, but one quibble.



If an instance is passed by value semantics then a copy of the data on
the stack is made and the adress of this copy is passed to the
implementation while by reference semantics pass the adress of the
original data on the stack.

The address of the copy isn't passed to the implementation. Rather, the
caller simply puts the value on the stack, and the caller *already*
knows where to find the value relative to the stack pointer. IAW,

public void foo(int i, int j, int k) {

A caller doesn't need to send foo the addresses of i,j and k. It just
sets up the stack correctly, and the foo function knows that it can find
i,j and k at the appropriate positions on the stack.



If so then I have another question: If a reference type is passed by
reference to a procedure and within this procedure a new instance of
the same type is generated and assigned to the passed reference
variable what happens to the instacne on the heap if the program
execution leaves the procedure? I think the original instacne on the
heap is now in danger to be killed by the garbage collector as no
reference points to it anymore:

Correct.

.



Relevant Pages

  • Re: Downgrading to D7
    ... I think there is a lot of use to store things on the stack, ... I.e. you declare variables as value or reference, ... Value types have different semantics to reference types. ... affect the original passed-in value. ...
    (borland.public.delphi.non-technical)
  • Re: How does managed code work?
    ... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why does this work?
    ... You probably assume that the return value is a reference to the local variable, ... The caller gets a copy of the reference to the object. ... variable doesn't affect the value on the stack. ... 32-Bit values are usually returned in a processor registers. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Difference between nullable class and nullable<> structure
    ... Page 313 (Understanding Value Types and Reference Types): ... a .NET data type may be value-based or ... the heap and the stack is, at a minimum, bound to fall short of precision, ... If the student already understand planetary orbital ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How java passes object references?
    ... uses the same sort of stack convention as many other languages, including C++ and C#. Local variables are allocated on the stack, not the heap. ... But my understanding is that Java byte-code is compiled "just-in-time" into a platform-efficient representation that is actually executed, and that would mean using platform-supported run-time mechanisms like a stack. ... It's theoretically possible to implement Java with individual heap allocations for each function call, but that would be relatively inefficient and I don't believe any mainstream implementation of Java would do it that way. ... At least then you won't risk confusing people who think you're saying, "pass by reference", which Java doesn't have. ...
    (comp.lang.java.programmer)