| | |
Pointer to const value modifiable?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hell "o" to all programmers out there.
I was just fiddling around with pointers to
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 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.
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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.
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.
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
So does this mean that the
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.
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.
•
•
•
•
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? •
•
•
•
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 ?
[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
•
•
•
•
Originally Posted by Dave Sinkula
There are not different values at the same memory location.
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.
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.
•
•
•
•
Originally Posted by ~s.o.s~
Does it make a difference if i would have compiled it usign a pure C compiler?
But given this:
C Syntax (Toggle Plain Text)
const int i = 20;
C Syntax (Toggle Plain Text)
printf ("\nThe value of i is %d", i);
C Syntax (Toggle Plain Text)
printf ("\nThe value of i is %d", 20);
[edit]If you got a little jiggy[*] with the code, you might produce this with a C++ compiler.
C Syntax (Toggle Plain Text)
#include <stdio.h> void foo(const int *ptr) { printf ("*ptr = %d\n", *ptr); } int main (void) { const int i = 20; int* ptr = (int*) &i; *ptr = 40; printf (" i = %d\n", i); printf ("*ptr = %d\n", *ptr); foo(&i); foo(ptr); return 0; } /* my output i = 20 *ptr = 40 *ptr = 40 *ptr = 40 */
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
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.
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Design Patterns
- Next Thread: help!! about ADT list please
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






