This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: assigning to const int via pointer
- From: Sriharsha <sriharsha dot v at redpinesignals dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 30 Dec 2004 12:52:11 +0530
- Subject: Re: assigning to const int via pointer
- Organization: Redpine Signals Inc.
- References: <87652k5lcy.fsf@proxy.tcenet>
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)
*****************************