www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Odd behavior in toy program

↑ ↓ ← 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
↑ ↓ "tjulian" <tjulian removethis.oldi.com> writes:
 const int dog = 8;

 *dog_p = 7;

Is it const or not? Either remove the const or make dog_p a (const int *). -- TimJ
Aug 25 2005
↑ ↓ Victor Lam <Victor_member pathlink.com> writes:
In article <dekb43$19lu$1 digitaldaemon.com>, tjulian says...
 const int dog = 8;

 *dog_p = 7;

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
↑ ↓ → "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