PDA

View Full Version : Doubt regarding const......


myself_rajat
10-09-2004, 09:07 AM
Hi all,

See the below code. Can u tell me what will be the output and why??


int main()
{
const int c = 16;
int *p = &c;
*p = 4;
printf("c=%d, *p=%d",c,*p);
return 0;
}

I tested this on gcc, the result was c=16 *p= 4. As far as I think it should give error while changing the read only memory content i.e. c, or if it allow changes then it should reflect for bot c and *p.

i3839
10-09-2004, 12:42 PM
z $ gcc -Wall bla.c
bla.c: In function `main':
bla.c:7: warning: initialization discards qualifiers from pointer target type
z $ ./a.out
c=4, *p=4
z $ gcc -Wall bla.c -O
bla.c: In function `main':
bla.c:7: warning: initialization discards qualifiers from pointer target type
z $ ./a.out
c=16, *p=4

RobSeace
10-09-2004, 07:36 PM
In other words, it's something GCC is doing as part of its code
optimization trickery... My guess is it's probably directly substituting
the constant value into the code at the point it sees "c" referenced,
since it knows its value at compile time... Ie: similar to how it would
work if you made it a #define'd macro instead of a const variable...
So, your reference to "c" disappears from your code completely,
and you're directly referencing "16", instead... So, the changed
variable value never shows up, except when referenced via a
pointer to it...

The basic point is that you shouldn't be doing this... You're breaking
the rules, and what happens at that point is pretty much undefined...
You can't fault GCC for anything it chooses to do at that point,
since your code is no longer valid, and hence ANYTHING it chooses
to do is equally acceptable...

Nope
10-09-2004, 11:05 PM
Hmm. I tried to write into a const string and while gcc accepted the coding my Linux killed the process with a segvfault.

i3839
10-10-2004, 12:07 PM
This doesn't happen here because 'c' is not a global var, it will be killed if you put it outside of main. I guess gcc sees that the idiot still referenced to it, and thus put c on the stack to avoid the crash. Or const values on the stack are always made while static vars aren't, no idea what the C standard says here.

RobSeace
10-10-2004, 06:42 PM
Yeah, I believe all string literals reside in read-only memory space,
so trying to trash one of those will indeed seg-fault you... And, if
you defined a "const char *", that would simply always point at that
actual string literal residing in read-only memory space... However,
if instead you made a "const char []", then it'd make a copy of the
contents of the string, and would behave roughly the same way
as above... (Though, it seems to always reflect the changes, and
never optimize away the reference to the const and replace it with
the actual value, like it does for the const int...) And, yes, if you
moved the const variables outside of the function, so they're global,
then they'd also be placed in read-only memory, along with the
string literals, so they'd also be protected from modification...