Pointer to const value modifiable?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Pointer to const value modifiable?

 
0
  #1
Jul 14th, 2006
Hell "o" to all programmers out there.
I was just fiddling around with pointers to const when i wrote this snippet.

int main (void) 
{
  int* ptr = NULL;
  const int i = 20;
  ptr = (int*) &i;
  *ptr = 40;
  printf ("\nThe addr of the const var is %p", &i);
  printf ("\nThe addr in ptr is %p", ptr);
  printf ("\nThe value of i is %d", i);
  printf ("\nThe value of location pointed by ptr is %d",   *ptr);
  getchar ();
  return 0;
}

THe problem i am facing is that the addr of the constant variable and that present in the pointer variable turns out to be the same but still when i modify the value using pointer to constant variable it raises no error. But when i try to query the value persent in the memory location pointed by the pointer and the value in the const variable it turns out to be different ???

How come the same memory location holds two different values?
Any suggestions or explanations would be gladly accepted.

Thanks to all in advance. Bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: Pointer to const value modifiable?

 
0
  #2
Jul 14th, 2006
So you deliberately break the type system and then wonder why it doesn't work? What were you expecting to happen?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Pointer to const value modifiable?

 
0
  #3
Jul 14th, 2006
Here is why it happened:
C programs live in memory as segments. The segments have
names like TEXT and BSS. Anyway, const values get placed in read-only memory, one of the segments. The read/write DATA segment is always created, and it looks like your compiler bent over backwards to allow you to write someplace, probably there.

Bottom line: you've got undefined behavior. That means there is no real explanation for the behavior you see and no expectation or guarantee that it will ever work.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,403
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer to const value modifiable?

 
0
  #4
Jul 14th, 2006
In C, const does not mean constant. It means read-only (whether or not such objects are placed in read-only memory or not is up to the system). If you choose to circumvent the compiler and write to read-only memory, behavior is undefined -- as already mentioned.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pointer to const value modifiable?

 
0
  #5
Jul 14th, 2006
So does this mean that the const qualified variables are placed in the CODE segment of the program rather than the normal DATA segment of the program like the array names?

So is this behaviour i.e. different values present at the same memory location different for different operating systems and different compiler designs ?

Thanks for your help. Bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,403
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer to const value modifiable?

 
0
  #6
Jul 14th, 2006
Originally Posted by ~s.o.s~
So does this mean that the const qualified variables are placed in the CODE segment of the program rather than the normal DATA segment of the program like the array names?
Maybe yes, maybe no. If it's const qualified, you're not supposed to modify it.

Originally Posted by ~s.o.s~
So is this behaviour i.e. different values present at the same memory location different for different operating systems and different compiler designs ?
There are not different values at the same memory location.

[edit]You probably compiled this with a C++ compiler, where const has a different meaning and optimization may be made that would appear to confuse you.
Last edited by Dave Sinkula; Jul 14th, 2006 at 6:10 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pointer to const value modifiable?

 
0
  #7
Jul 14th, 2006
Originally Posted by Dave Sinkula
There are not different values at the same memory location.
Maybe you are right but this is the output of a single run ;

The addr of the const var is 0012FEC8
The addr in ptr is 0012FEC8
The value of i is 20
The value of location pointed by ptr is 40

This is wat confuses me since you can note that both the addr are the same.

Originally Posted by Dave Sinkula
You probably compiled this with a C++ compiler, where const has a different meaning and optimization may be made that would appear to confuse you.
Yes you are right, i compiled this thing with a VC++ 8 compiler.

Does it make a difference if i would have compiled it usign a pure C compiler?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,403
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Pointer to const value modifiable?

 
0
  #8
Jul 14th, 2006
Originally Posted by ~s.o.s~
Does it make a difference if i would have compiled it usign a pure C compiler?
Yes, I believe so. I can't find any references off hand, but it's like the fact that you can use a const int to declare an array size in C++ because it is a true constant -- thus the compiler may not use the value at the location but instead just plug in what the const value was assigned. Whereas in C you cannot declare an array size using a const int because it is not a true constant. Something like that.

But given this:
  1. const int i = 20;
the compiler can then take this:
  1. printf ("\nThe value of i is %d", i);
and turn it into this:
  1. printf ("\nThe value of i is %d", 20);
I believe.

[edit]If you got a little jiggy[*] with the code, you might produce this with a C++ compiler.
  1. #include <stdio.h>
  2.  
  3. void foo(const int *ptr)
  4. {
  5. printf ("*ptr = %d\n", *ptr);
  6. }
  7.  
  8. int main (void)
  9. {
  10. const int i = 20;
  11. int* ptr = (int*) &i;
  12. *ptr = 40;
  13. printf (" i = %d\n", i);
  14. printf ("*ptr = %d\n", *ptr);
  15. foo(&i);
  16. foo(ptr);
  17. return 0;
  18. }
  19.  
  20. /* my output
  21.   i = 20
  22. *ptr = 40
  23. *ptr = 40
  24. *ptr = 40
  25. */
[*]Not a technical term.
Last edited by Dave Sinkula; Jul 14th, 2006 at 6:55 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Pointer to const value modifiable?

 
0
  #9
Jul 15th, 2006
Yes you are right even i get the same output.
Maybe the behaviour of the const whose value is being modified is undefined afterall, so better not walk on the broken glass of undefined behaviour.

Well to the ppl who have come to this thread to learn somthign about constants in C and C++ maybe this can be of some help :-

http://en.wikipedia.org/wiki/Const

Hope this helped. Bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC