943,537 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3116
  • C RSS
Jul 14th, 2006
0

Pointer to const value modifiable?

Expand Post »
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.
Similar Threads
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

So you deliberately break the type system and then wonder why it doesn't work? What were you expecting to happen?
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

Quote 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.

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

Quote 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.

Quote 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?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 14th, 2006
0

Re: Pointer to const value modifiable?

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 15th, 2006
0

Re: Pointer to const value modifiable?

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Ask a problem,about template:)
Next Thread in C Forum Timeline: help!! about ADT list please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC