Re: About reference

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 08/11/04


Date: Wed, 11 Aug 2004 13:52:07 +0200

newbiecpp wrote:
>
> I got more confused regarding the size of a reference. I wrote the program:
>
> char c;
> char& rc = c;
>
> struct A {
> A() : i(c) {}
> char i;
> int j;
> };
>
> int main()
> {
> std::cout << sizeof(rc) << std::endl << sizeof(A) << std::endl;
> return 0;
> }
>
> The sizeof(rc) is 1 and sizeof(A) is 8. The first looks like the reference
> doesn't have a size but the second looks like the reference just like a
> point and has a size.

How do you conclude that. In struct A there is no reference involved :-)
(I guess this is a typo in posting which also shows, that you should
always use cut&paste to copy your real code to the newsreader instead
of retyping it. I gues the above should read:

 struct A {
     A() : i(c) {}
     char& i;
     int j;
 };

)

> Can someone explain this to me? I appreciate!

First of all: A reference is another name for an already existing object.

So what does that mean in practice?
It means there are situations where the compiler can do the substitution
on its own. Just like in your example for rc. The compiler tracks that
rc is just another name for c and whatever you do to rc it applies to c.
So this reference doesn't take up space in the final executable. The compiler
simply uses c whenever you write rc.
But then: The situation isn't always like this. There are situations where
the compiler doesn't know to what other object the reference is another
name for. So it has to implement the reference semantics somehow. Most
often (as far as we know: always), the compiler does this by the means
of a pointer. So in your case of the struct, the compiler puts a pointer
underneath of i and adjust the code in such a way that whenever you use
i somewhere, it dereferences that pointer.

So the conclusion is: A reference may or may not take up space in the
final executable. It all depends in which way the reference is used and
how the compiler implements things.
But for you, the programmer, all of this should not care. For you a reference
is simply: another name for an otherwise existing object. How the compiler
implements this, is none of your business.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: List<> of struct with property. Cannot change value of property. why?
    ... method changes the struct that owns it. ... how would the compiler know the method changes the struct? ... Should the linker be required to carry this flag around too, so that when you import a reference to a struct type not compiled with the current project, you still have that information? ... There's a time and place for a value type, and in fact they even have their place in lists. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: List<> of struct with property. Cannot change value of property. why?
    ... method changes the struct that owns it. ... so that when you import a reference to a struct type not ... tell it specifically that it doesn't change it, then the compiler ... have their place in lists. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using early-bound interface on a late-bound object
    ... > the compiler determines which interfaces to use, ... > supports the declared interface. ... >> help if someone can point me to some authoritative document or reference ... within customer shops when they mirrored this 'division' to keep their ...
    (microsoft.public.vb.general.discussion)
  • Re: Garbage Collection Eligibility and portability
    ... (in the parlance of compiler writers). ... There isn'tany way to get a given Lisp to act that way. ... that alters exactly when a variable reference to a memory location ceases holding it from collection. ... The other, which I've seen called "Aggressive garbage collection" in some articles on this behavior in C#/.Net, appears to make a memory location eligible the instant the last reference to the location is used. ...
    (comp.lang.lisp)
  • Re: Why we should (not?) have closures after all
    ... Programmers should have tools that can tell them as much as possible about their code's likely behavior. ... The wart can be used anywhere a type is used in declaring a reference ... The compiler does nothing special if a "can be null" reference is ... it would be nice if there was a variant of the "assert" statement that was always enabled and threw IllegalArgumentException instead of AssertionError. ...
    (comp.lang.java.programmer)