Topic: Is there a restriction on using reference variables?
Author: jodle@bix.com (jodle)
Date: 1996/07/14 Raw View
rozen@ix.netcom.com wrote:
: Let's say you have a function that accepts arguments by reference.
: When you call this function and "inside the call" you use casting on
: the arguments (example: foo( (char*) some_pointer ); ) you get a
: few warnings and the variable is not passed as a reference, thus it's
: value cannot be changed. Strange!
Generally, this is the result of the cast generating a temporary. The
temporary is a copy of the specified argument and the reference is bound
to the temporary rather than the specified argument. Therefore, any
changes made to the referenced variable from inside the function will be
made to the copy instead of the specified argument. Since the temporary
is anonymous, the value in it after the function call is effectively lost.
This can be a particularly nasty situation if the circumstances are
similar to this example:
struct X
{
char *pBuffer;
X( char *pBuf ) : pBuffer( pBuf ) {}
~X() { delete pBuffer; }
};
void make_const_X_delete_buffer( X & ) {}
main()
{
const X x( new [1000] char );
make_const_X_delete_buffer( x );
return 0;
}
The culprit here is the hidden, compiler-generated copy constructor that
performs the bitwise copy of the data in the source X. The "cast" creates
a temporary that is bit-copied from x. Since x.pBuffer is then owned by
two X objects, it is deleted twice. In a less trivial class, other
operations on pBuffer are likely to be attempted in the interval between
the two deletes. The result of these operations are almost certainly
unpleasant and always undesirable.
---
[ 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: rozen@ix.netcom.com
Date: 1996/07/06 Raw View
I have noticed the following phenomenon:
Let's say you have a function that accepts arguments by reference.
When you call this function and "inside the call" you use casting on
the arguments (example: foo( (char*) some_pointer ); ) you get a
few warnings and the variable is not passed as a reference, thus it's
value cannot be changed. Strange!
Has anyone else seen this before. Could it be that I am using a
stinky
compiler under Linux, or is this a standard restriction?
Mike
P.S CC your responses via email as well, please
---
[ 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: rozen@ix.netcom.com
Date: 1996/07/06 Raw View
I have noticed the following phenomenon:
Let's say you have a function that accepts arguments by reference.
When you call this function and "inside the call" you use casting on
the arguments (example: foo( (char*) some_pointer ); ) you get a
few warnings and the variable is not passed as a reference, thus it's
value cannot be changed. Strange!
Has anyone else seen this before. Could it be that I am using a stinky
compiler under Linux, or is this a standard restriction?
Mike
P.S CC your responses via email as well, please
[ 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: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/07/08 Raw View
In article <31dd748d.440840@nntp.ix.netcom.com> rozen@ix.netcom.com
writes:
|> I have noticed the following phenomenon:
|> Let's say you have a function that accepts arguments by reference.
|> When you call this function and "inside the call" you use casting on
|> the arguments (example: foo( (char*) some_pointer ); ) you get a
|> few warnings and the variable is not passed as a reference, thus it's
|> value cannot be changed. Strange!
|> Has anyone else seen this before. Could it be that I am using a
|> stinky
|> compiler under Linux, or is this a standard restriction?
It must be a stinky compiler: you should get an error, and not a
warning:-).
I presume that the reference in question was not const, since you want
to change the value. Only an lvalue can bind to a non-const reference.
The result of a cast is not an l-value (unless the cast is to a
reference type).
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
---
[ 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: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/07/08 Raw View
kanze@gabi-soft.fr (J. Kanze) writes:
>rozen@ix.netcom.com writes:
>
>|> I have noticed the following phenomenon:
>|> Let's say you have a function that accepts arguments by reference.
>|> When you call this function and "inside the call" you use casting on
>|> the arguments (example: foo( (char*) some_pointer ); ) you get a
>|> few warnings and the variable is not passed as a reference, thus it's
>|> value cannot be changed. Strange!
>
>|> Has anyone else seen this before. Could it be that I am using a
>|> stinky compiler under Linux, or is this a standard restriction?
>
>It must be a stinky compiler: you should get an error, and not a
>warning:-).
Strictly speaking, according to the C++ committee's draft working
paper, you should get a _diagnostic_. Whether that diagnostic is an
error or a warning is a quality-of-implementation issue. A high
quality implementation would offer a mode in which you got an error.
However, this is an area in which the C++ language has changed, and
there may still be some old programs that rely on such code being
allowed, so a high quality implementation might well also offer a mode
in which the diagnostic was only a warning, so that old code could
continue to compile. (Incidentally, the most commonly used C++ compiler
for Linux, GNU C++, does indeed offer both of these modes.)
>I presume that the reference in question was not const, since you want
>to change the value. Only an lvalue can bind to a non-const reference.
>The result of a cast is not an l-value (unless the cast is to a
>reference type).
Note also that casting to a reference type in this sort of situation
will probably shut up the warning or error, but it may result in
undefined behaviour at runtime.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]