Topic: Do references live on the stack?


Author: Tom Payne <thp@cs.ucr.edu>
Date: 1996/11/12
Raw View
John E. Potter <jpotter@falcon.lhup.edu> wrote:
: Tom Payne (thp@cs.ucr.edu) wrote:
: : Bruce Sams <sams@ipp-garching.mpg.de> wrote:
[...]
: : : ... In f2, I think that the pointer to x appears
: : : on the stack.  In f3() I'm not sure.  Does the parameter passed ever
: : : appear on the stack (references are not permitted to take memory space).

: : ...  The
: : C++ Standards Committee has defined objects to be "segments of
: : storage" and insists that "references aren't objects."  Your
: : inference, that references are not permitted to take memory space, is
: : a logical one.

: An open question:
: Is this inference common enough amoung the intended audience of the
: standard to include some clarification?

I think that a vague, unsettling sense of confusion is the more common
reaction to the ambiguities in the current wording of the standard.
Rather than inclusion of a clarification, however, I'd recommend a
minor rewording that makes it clear that the statement that "An object
is a region of storage [1.7.1]" is meant to be a pedagogical note
rather than definition of the term "object."  The best way would be to
rephrase it along the following lines:

  An object is any instance of an object type (i.e., non-reference
  data type).  Each object is identified with a regions of storage via
  the address-of and sizeof operators, which object types (and only
  object types) support.

Tom Payne
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Tom Payne <thp@cs.ucr.edu>
Date: 1996/11/06
Raw View
[Moderator's note: this article is crossposted.  Followups have been
directed by default to comp.std.c++. mha]

Bruce Sams <sams@ipp-garching.mpg.de> wrote:

: Hello

: I am trying to resolve the issue of memory allocation for references
: which are passed to funtions as parameters.  The question is this:
: do they get put on the stack?

: Consider 3 functions passed an integer x

:       FUNCTION                               CALL

:    int f1(int i){return i;}                 y = f1(x);   by value
:    int f2(int* i){return *i;}               y = f2(&x);  by pointer
:    int f3(int& i){return i;}                y = f3(x);   by reference


: OK. I know that in f1, the input parameter (x) will appear on the stack.
: But what about f2 and f3?  In f2, I think that the pointer to x appears
: on the stack.  In f3() I'm not sure.  Does the parameter passed ever appear
: on the stack (references are not permitted to take memory space).

: What is going on here?

: I would be very grateful if anyone out there knows the answer to this
: question.

: Sincerely,


: Bruce Sams


You are the victim of an unintentional and unfortunate hoax.  The
C++ Standards Committee has defined objects to be "segments of
storage" and insists that "references aren't objects."  Your
inference, that references are not permitted to take memory space, is
a logical one.

What you've not been told is that part way thorough the Standard there
is a second implicit definition of object:

  An object type is a (possibly cv-qualified) type that is not a func-
  tion type, not a reference type, and not incomplete (except for an
  incompletely-defined object type).

When the Committee insists that "references aren't objects," it refers
to this second definition plus the natural inference that "objects"
are "instance of object types."

Don't let them fool you.  References have no "magic" way to remember
their referrents without the using memory.  They have the same need
for memory as ints and pointers, with the qualification that they are
constant and, de facto, their addresses can't be found, so there are
frequently opportunities to optimize away their storage.

Tom Payne
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/11/09
Raw View
Tom Payne (thp@cs.ucr.edu) wrote:
: Bruce Sams <sams@ipp-garching.mpg.de> wrote:
: : I am trying to resolve the issue of memory allocation for references
: : which are passed to funtions as parameters.  The question is this:
: : do they get put on the stack?
: : Consider 3 functions passed an integer x
: :       FUNCTION                               CALL
: :    int f1(int i){return i;}                 y = f1(x);   by value
: :    int f2(int* i){return *i;}               y = f2(&x);  by pointer
: :    int f3(int& i){return i;}                y = f3(x);   by reference
: : OK. I know that in f1, the input parameter (x) will appear on the stack.
: : But what about f2 and f3?  In f2, I think that the pointer to x appears
: : on the stack.  In f3() I'm not sure.  Does the parameter passed ever
: : appear on the stack (references are not permitted to take memory space).

I think it says that it is unspecified whether references take space, ie.
they are not required to take space nor are they required not to take
space.

It has been pointed out that in the f3 above, there is little choice but
to implement the reference as a pointer.

I don't think that the "stack" is part of the language specification.  I
use a risc box on which all three functions would pass the parameter in a
register and nothing would happen to the software stack.  So neither the
int, pointer, nor reference would take memory space.  For the int and
pointer, I could take the address of the parameter and force the
inefficient use of the software stack; however, for reference, I could
not do that and it would remain in a register, magically not taking space.

: You are the victim of an unintentional and unfortunate hoax.  The
: C++ Standards Committee has defined objects to be "segments of
: storage" and insists that "references aren't objects."  Your
: inference, that references are not permitted to take memory space, is
: a logical one.

An open question:
Is this inference common enough amoung the intended audience of the
standard to include some clarification?

John
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]