www.digitalmars.com         C & C++   DMDScript  

c++ - Odd behavior in toy program

reply Victor Lam <Victor_member pathlink.com> writes:
Hello group,

I just downloaded the Digital Mars C/C++ compiler and was
playing around with the piece of code shown at the end of
this post.  When I compile and run said code, I get the
following output:

dog = 8, dog_p points to 7

However, when I uncomment the printf("hello world") line
I get the output I expect:

dog = 7, dog_p points to 7

I'm confused as to what is going on.  Anyone care to
enlighten me?

Thanks!
Vic
-------------------------------------------------------
#include <stdio.h>

main() {
const int dog = 8;
int *dog_p = (int *)&dog;
*dog_p = 7;
//printf("hello world!\n");
printf("dog = %d, dog_p points to %d\n", dog, *dog_p);
}
-------------------------------------------------------
Aug 24 2005
parent reply "tjulian" <tjulian removethis.oldi.com> writes:
 const int dog = 8;
dog is const 8 and will not change
 *dog_p = 7;
change dog Is it const or not? Either remove the const or make dog_p a (const int *). -- TimJ
Aug 25 2005
parent reply Victor Lam <Victor_member pathlink.com> writes:
In article <dekb43$19lu$1 digitaldaemon.com>, tjulian says...
 const int dog = 8;
dog is const 8 and will not change
 *dog_p = 7;
change dog Is it const or not? Either remove the const or make dog_p a (const int *). -- TimJ
here is the code again. of course this code is not meant to do anything useful, which is why i have the int being const, and a non-const int * pointing to the int. it was meant for experimental purposes. #include <stdio.h> main() { const int dog = 8; int *dog_p = (int *)&dog; *dog_p = 7; //printf("hello world!\n"); printf("dog = %d, dog_p points to %d\n", dog, *dog_p); } dog_p, along with the cast, is used to circumvent the const-ness of dog, which is why i expect to see the printf show that the value of dog is 7. in any case, i am able to change the value of dog from 8 to 7 in this manner, but only after i uncomment printf("hello world") which doesn't seem like it should affect the value of dog. that is the question i'm trying to answer here. vic
Aug 25 2005
parent "Walter" <newshound digitalmars.com> writes:
"Victor Lam" <Victor_member pathlink.com> wrote in message
news:deksaf$2601$1 digitaldaemon.com...
 here is the code again.  of course this code is not meant to do anything
 useful, which is why i have the int being const, and a non-const int *
 pointing to the int.  it was meant for experimental purposes.

 #include <stdio.h>

 main() {
 const int dog = 8;
 int *dog_p = (int *)&dog;
 *dog_p = 7;
 //printf("hello world!\n");
 printf("dog = %d, dog_p points to %d\n", dog, *dog_p);
 }

 dog_p, along with the cast, is used to circumvent the const-ness of dog,
 which is why i expect to see the printf show that the value of dog is 7.
 in any case, i am able to change the value of dog from 8 to 7 in this
 manner, but only after i uncomment printf("hello world") which doesn't
 seem like it should affect the value of dog.  that is the question i'm
 trying to answer here.
What you're seeing here is called "undefined behavior", in this case the program subverts the const'ness of dog with a cast. It's in the same category as: i = 3; i = i++; // what's the value of i now?
Aug 25 2005