Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

prog output?

1 view
Skip to first unread message

vijay

unread,
May 31, 2006, 3:55:25 PM5/31/06
to
Hi All,

Code:

#include "iostream.h"

void main(void)
{
const int i=10;
int *p;
p=const_cast<int*>(&i);
*p=111;
cout<<i<<" "<<*p;
}

output:

10 111


Can you plaese tell me why the value of i and *p is different??

Thanks and regards,
Vijay


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Ulrich Eckhardt

unread,
Jun 1, 2006, 7:04:07 AM6/1/06
to
vijay wrote:
> #include "iostream.h"

If it was a system header, it would have to be <iostream.h>. Further, even
that would be an ancient (pre-standard) iostream header. Check the book
you are learning from with the reviews at http://accu.org and dump the
book if it isn't good (or the teacher, in case you're learning at a
school).

> void main(void)

main() returns an int. Always.

> {
> const int i=10;
> int *p;
> p=const_cast<int*>(&i);
> *p=111;

Using const_cast to remove the const from a pointer/reference that refers
to a const object (i.e. when the const in the pointer/ref was not added
lateron) invokes undefined behaviour. In other words, your program is
broken and could output whatever it wants.

[...]


> output:
>
> 10 111
>
>
> Can you plaese tell me why the value of i and *p is different??

See above, your program is broken.

Uli

Thomas Maeder

unread,
Jun 1, 2006, 7:18:11 AM6/1/06
to

"vijay" <mt.v...@gmail.com> writes:

> #include "iostream.h"

This a non-Standard header. Consider #includeing <iostream> (and
writing std::cout etc.). For your code to reliably compile and work,
you also have to #include <ostream>, by the way.


> void main(void)

The return type of main() has to be int.

> {
> const int i=10;
> int *p;
> p=const_cast<int*>(&i);
> *p=111;

Modifying a constant object has undefined behavior.


> cout<<i<<" "<<*p;
> }
>
> output:
>
> 10 111
>
>
> Can you plaese tell me why the value of i and *p is different??

This is just an instance of undefined behavior.

We can speculate that the compiler "knows" that i, being const, will
never change, and substitutes usages of i with usages of the value 10.

kanze

unread,
Jun 1, 2006, 7:24:43 AM6/1/06
to
vijay wrote:

> Code:

> #include "iostream.h"

Two problems right off the bat: it's more or less dangerous to
use the #include "..." form for headers -- you should use
#include <...> instead --; and with any modern compiler, it's
not iostream.h that you want, but iostream and ostream, .e.g:

#include <iostream>
#include <ostream>

> void main(void)

And this line shouldn't compile -- main returns an int. Also,
the use of void to specify that the function takes no parameters
is a C'ism, which pretty much marks one as unfamiliar with C++.

> {
> const int i=10;
> int *p;
> p=const_cast<int*>(&i);
> *p=111;

And this line has undefined behavior. You're attempting to
modify a const object. On some systems, for example, you might
get a core dump. If i were static, you'd probably get it on
most systems.

> cout<<i<<" "<<*p;
> }

> output:

> 10 111

> Can you plaese tell me why the value of i and *p is
> different??

Because the program has undefined behavior. Anything in
possible. (What's probably happening is that the compiler knows
the value of i in the output statement, and simply uses it
directly. Which is, after all, the intent of const objects.)

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Seungbeom Kim

unread,
Jun 1, 2006, 4:59:01 PM6/1/06
to
Ulrich Eckhardt wrote:
>
> Using const_cast to remove the const from a pointer/reference that refers
> to a const object (i.e. when the const in the pointer/ref was not added
> lateron) invokes undefined behaviour.

Isn't it the action of actually trying to modify a const object
(through a pointer that const was taken off from by const_cast)
that invokes an undefined behaviour?

AFAIK, just using const_cast won't invoke a UB.

--
Seungbeom Kim

Gerhard Menzl

unread,
Jun 1, 2006, 4:56:26 PM6/1/06
to
vijay wrote:

> #include "iostream.h"
>
> void main(void)
> {
> const int i=10;
> int *p;
> p=const_cast<int*>(&i);
> *p=111;
> cout<<i<<" "<<*p;
> }
>
> output:
>
> 10 111
>
>
> Can you plaese tell me why the value of i and *p is different??

Because your code tries to modify a const object, which invokes
undefined behaviour. The program could do anything.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.

Francis Glassborow

unread,
Jun 3, 2006, 10:10:13 AM6/3/06
to
In article <e5mt5d$e2f$1...@news.Stanford.EDU>, Seungbeom Kim
<musi...@bawi.org> writes

>Ulrich Eckhardt wrote:
>>
>> Using const_cast to remove the const from a pointer/reference that refers
>> to a const object (i.e. when the const in the pointer/ref was not added
>> lateron) invokes undefined behaviour.
>
>Isn't it the action of actually trying to modify a const object
>(through a pointer that const was taken off from by const_cast)
>that invokes an undefined behaviour?
>
>AFAIK, just using const_cast won't invoke a UB.

That depends. If your code attempts to generate a pointer to read only
memory which can be written through there is nothing in the Standard
that denies the system from aborting the program.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects

James Kanze

unread,
Jun 3, 2006, 8:38:36 PM6/3/06
to
Francis Glassborow wrote:
> In article <e5mt5d$e2f$1...@news.Stanford.EDU>, Seungbeom Kim
> <musi...@bawi.org> writes
>> Ulrich Eckhardt wrote:
>>> Using const_cast to remove the const from a pointer/reference that
refers
>>> to a const object (i.e. when the const in the pointer/ref was not added
>>> lateron) invokes undefined behaviour.
>> Isn't it the action of actually trying to modify a const object
>> (through a pointer that const was taken off from by const_cast)
>> that invokes an undefined behaviour?

>> AFAIK, just using const_cast won't invoke a UB.

> That depends. If your code attempts to generate a pointer to
> read only memory which can be written through there is nothing
> in the Standard that denies the system from aborting the
> program.

Where do you read that in the standard. I don't think the
const_cast is a problem in itself; only what you do with the
results. And I'm pretty sure that a char* to const data is
legal.

--
James Kanze kanze...@neuf.fr


Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung

9 place Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

0 new messages