Re: assigning to const int via pointer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



One more addition;

If you declare x as volatile, i.e.
volatile const int x=5;

then, even with Optimization, the result is 10.

Another thing, I observed with gdb, is that the variable ptr is actually eliminated during Optimization O2.
to confirm, compile the code as:
gcc -O2 -g -Wall test.c
Now run it in gdb:


gdb a.out

here, the listing will show the code as it is, but try:
print *ptr
It says, "No symbol "ptr" in the current context"

Regards,
Harsha.

Joe Steeve wrote:

The following code assigns a value to a `const int` via a
pointer.,

#include <stdio.h>

int main()
{
const int x=5;
int *ptr;
ptr = &x;
*ptr = 10;
printf("%d",x);
}


The code gives `10` for the following compilation

   $gcc -o test test.c

It gives `5` when using optimisations switches.,

   $gcc -o test -O2 test.c

Feature or bug or any explanation for this?




--
*****************************
* Sriharsha Vedurmudi
* Software Engineer
* * Redpine Signals Inc.
* Gate #395, Plot 87,88
* Sagar Society, Road #2, * Banjara Hills,
* Hyderabad - 500 034
* www.redpinesignals.com
*
* +91-40-23559911 (Office)
* +91-98851-37338 (Mobile)
*****************************



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux