Topic: Interesting Observation !!


Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/08/28
Raw View
"Anand J. Bhalerao" <anand@abnar-telsoft.com> writes:

 > Try this sample C++ code on a solaris box.
 >
 >#include <stdio.h>
 >int SetValue(const int &x)
 >{
 >    int &z = (int &)x ;
 >    z = 10 ;

Here you modify an object (the const int y in main)
that was defined as const.  This causes undefined behaviour.
Hence the compiler is allowed to print anything it wants.

 >    printf("address of x = %d - value of x = %d \n", &x, x) ;
 >    printf("address of y = %d - value of z = %d \n", &z, z) ;

This is unrelated to your real question, but here is another
source of undefined behaviour: above you pass an `int *' to
printf("...%d...", ...).  You should either use iostreams instead,
or cast to `(void *)' and use "%p" rather than "%d".

 >    return y ;
 >}
 >int main() {
 >    const int y = 20 ;
 >
 >    printf("address of y = %d - value of y = %d \n", &y, y) ;
 >    SetValue(y) ;

--
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
                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: Hyman Rosen <uunet!jyacc!calumny!hymie@ncar.UCAR.EDU>
Date: 1997/08/28
Raw View
"Anand J. Bhalerao" <anand@abnar-telsoft.com> writes:
>  Try this sample C++ code on a solaris box.
> int SetValue(const int &x)
> {
>     int &z = (int &)x ;
>     z = 10 ;
...

This is undefined. You have taken an object declared to be const,
cast away its constness, and then modified it. The program may
therefore proceed to do anything at all.
---
[ 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: "Scott Jones" <sjones@sequeltech.com>
Date: 1997/08/29
Raw View
The answer to your question is in the following line:

>     int &z = (int &)x ;

You're casting away the constness of the integer reference x in assigning
it to z.
---
[ 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
]