public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* assigning to const int via pointer
@ 2004-12-30  6:27 Joe Steeve
  2004-12-30  7:19 ` Sriharsha
  2004-12-30 16:02 ` Ian Lance Taylor
  0 siblings, 2 replies; 8+ messages in thread
From: Joe Steeve @ 2004-12-30  6:27 UTC (permalink / raw)
  To: GCC Mailing list

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]


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? 

-- 
.O. A proud GNU user
..O http://www.joesteeve.tk/
OOO http://gnukid.5gigs.com/

[-- Attachment #2: Type: application/pgp-signature, Size: 188 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-30  6:27 assigning to const int via pointer Joe Steeve
@ 2004-12-30  7:19 ` Sriharsha
  2004-12-30 16:02 ` Ian Lance Taylor
  1 sibling, 0 replies; 8+ messages in thread
From: Sriharsha @ 2004-12-30  7:19 UTC (permalink / raw)
  To: gcc-help

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)
 *****************************

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-30  6:27 assigning to const int via pointer Joe Steeve
  2004-12-30  7:19 ` Sriharsha
@ 2004-12-30 16:02 ` Ian Lance Taylor
  2004-12-31  6:44   ` Sriharsha
  2004-12-31 21:43   ` Joe Steeve
  1 sibling, 2 replies; 8+ messages in thread
From: Ian Lance Taylor @ 2004-12-30 16:02 UTC (permalink / raw)
  To: Joe Steeve; +Cc: GCC Mailing list

Joe Steeve <joe_steeve@gmx.net> writes:

> 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? 

When you declare that the variable is const, you are declaring that
the value does not change.  When you do change it, you are using
undefined behaviour.  When the compiler sees undefined behaviour, it
does not behave predictably.

ISO C99 6.7.3: "If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with
non-const-qualified type, the behavior is undefined."

Ian

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-30 16:02 ` Ian Lance Taylor
@ 2004-12-31  6:44   ` Sriharsha
  2004-12-31 11:08     ` Gabriel Dos Reis
  2004-12-31 14:52     ` Ian Lance Taylor
  2004-12-31 21:43   ` Joe Steeve
  1 sibling, 2 replies; 8+ messages in thread
From: Sriharsha @ 2004-12-31  6:44 UTC (permalink / raw)
  To: gcc-help


Ian Lance Taylor wrote:

>Joe Steeve <joe_steeve@gmx.net> writes:
>
>  
>
>>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? 
>>    
>>
>
>When you declare that the variable is const, you are declaring that
>the value does not change.  When you do change it, you are using
>undefined behaviour.  When the compiler sees undefined behaviour, it
>does not behave predictably.
>
>ISO C99 6.7.3: "If an attempt is made to modify an object defined with
>a const-qualified type through use of an lvalue with
>non-const-qualified type, the behavior is undefined."
>  
>
I am a little confused. The rule says:
If an attempt is made to modify an object, defined with a 
const-qualified type, through use of an lvalue with a 
non-const-qualified type, the behavior is undefined.

Now, in the above, program, we are not trying to alter the value of the 
variable x as follows:
x = 10;

But we are trying to alter the contents of a memory location, which 
happens to be where the variable 'x' refers to,  by using a pointer, 
which is defined behaviour.

According to the rule, you cannot use the variable, declared as a 
const-qualified type, on the left hand side (lvalue, or assigned-to 
variable), but it does not say anything about the memory being modified.

Please correct me if I am wrong. Also, when you qualify the type of x to 
be a volatile, then the behaviour is as expected. So, there must be some 
other reason why the variable "int *ptr" is being discarded by 
Optimization. Check out that without optimization, you are able to 
change the contents. Also, I've tried this with a few other compilers 
(without optimization) and they all change the contents.

Harsha

>Ian
>
>  
>

-- 
 *****************************
 * 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)
 *****************************

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-31  6:44   ` Sriharsha
@ 2004-12-31 11:08     ` Gabriel Dos Reis
  2004-12-31 14:52     ` Ian Lance Taylor
  1 sibling, 0 replies; 8+ messages in thread
From: Gabriel Dos Reis @ 2004-12-31 11:08 UTC (permalink / raw)
  To: Sriharsha; +Cc: gcc-help

Sriharsha <sriharsha.v@redpinesignals.com> writes:

| Ian Lance Taylor wrote:
| 
| >Joe Steeve <joe_steeve@gmx.net> writes:
| >
| >
| >>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?
| >
| >When you declare that the variable is const, you are declaring that
| >the value does not change.  When you do change it, you are using
| >undefined behaviour.  When the compiler sees undefined behaviour, it
| >does not behave predictably.
| >
| >ISO C99 6.7.3: "If an attempt is made to modify an object defined with
| >a const-qualified type through use of an lvalue with
| >non-const-qualified type, the behavior is undefined."
| >
| I am a little confused. The rule says:
| If an attempt is made to modify an object, defined with a
| const-qualified type, through use of an lvalue with a
| non-const-qualified type, the behavior is undefined.
| 
| Now, in the above, program, we are not trying to alter the value of
| the variable x as follows:
| x = 10;
| 
| But we are trying to alter the contents of a memory location, which
| happens to be where the variable 'x' refers to,  by using a pointer,
| which is defined behaviour.

which is precisely what the rule outlaws.  

-- Gaby

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-31  6:44   ` Sriharsha
  2004-12-31 11:08     ` Gabriel Dos Reis
@ 2004-12-31 14:52     ` Ian Lance Taylor
  2005-01-03  6:57       ` Sriharsha
  1 sibling, 1 reply; 8+ messages in thread
From: Ian Lance Taylor @ 2004-12-31 14:52 UTC (permalink / raw)
  To: Sriharsha; +Cc: gcc-help

Sriharsha <sriharsha.v@redpinesignals.com> writes:

> >>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?
> >
> >When you declare that the variable is const, you are declaring that
> >the value does not change.  When you do change it, you are using
> >undefined behaviour.  When the compiler sees undefined behaviour, it
> >does not behave predictably.
> >
> >ISO C99 6.7.3: "If an attempt is made to modify an object defined with
> >a const-qualified type through use of an lvalue with
> >non-const-qualified type, the behavior is undefined."
> >
> I am a little confused.

Yes.

> The rule says:
> If an attempt is made to modify an object, defined with a
> const-qualified type, through use of an lvalue with a
> non-const-qualified type, the behavior is undefined.
> 
> Now, in the above, program, we are not trying to alter the value of
> the variable x as follows:
> x = 10;
> 
> But we are trying to alter the contents of a memory location, which
> happens to be where the variable 'x' refers to,  by using a pointer,
> which is defined behaviour.

No.  Note that the rule is about modifying "an object".  The object in
question was defined by the definition of 'x', and thus was defined
using a const-qualified type.  The assignment through 'ptr' is
modifying an object defined with a const-qualified type.

> According to the rule, you cannot use the variable, declared as a
> const-qualified type, on the left hand side (lvalue, or assigned-to
> variable), but it does not say anything about the memory being
> modified.

No.  The rule I quoted doesn't say that you can't use a
const-qualified type as an lvalue.  It says that you can't use an
lvalue with a non-const-qualified type to modify an object defined
with a const-qualified type.  *ptr is an lvalue with a
non-const-qualified type.  It is being used to modify the object
defined by the declaration of x with a const-qualified type.

> Please correct me if I am wrong. Also, when you qualify the type of x
> to be a volatile, then the behaviour is as expected. So, there must be
> some other reason why the variable "int *ptr" is being discarded by
> Optimization. Check out that without optimization, you are able to
> change the contents. Also, I've tried this with a few other compilers
> (without optimization) and they all change the contents.

The program performs undefined behaviour.  Therefore the compiled
results are unpredictable.  It is not surprising that the behaviour is
different among different compilers and different optimization levels.

Ian

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-30 16:02 ` Ian Lance Taylor
  2004-12-31  6:44   ` Sriharsha
@ 2004-12-31 21:43   ` Joe Steeve
  1 sibling, 0 replies; 8+ messages in thread
From: Joe Steeve @ 2004-12-31 21:43 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: GCC Mailing list

[-- Attachment #1: Type: text/plain, Size: 351 bytes --]

Ian Lance Taylor <ian@airs.com> writes:
> ISO C99 6.7.3: "If an attempt is made to modify an object
> defined with a const-qualified type through use of an lvalue
> with non-const-qualified type, the behavior is undefined."

Thanks., that clarifies the confusion. :)

-- 
.O. A proud GNU user
..O http://www.joesteeve.tk/
OOO http://gnukid.5gigs.com/

[-- Attachment #2: Type: application/pgp-signature, Size: 188 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: assigning to const int via pointer
  2004-12-31 14:52     ` Ian Lance Taylor
@ 2005-01-03  6:57       ` Sriharsha
  0 siblings, 0 replies; 8+ messages in thread
From: Sriharsha @ 2005-01-03  6:57 UTC (permalink / raw)
  To: gcc-help

Got it now. Thank you.

Ian Lance Taylor wrote:

>Sriharsha <sriharsha.v@redpinesignals.com> writes:
>
>  
>
>>>>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?
>>>>        
>>>>
>>>When you declare that the variable is const, you are declaring that
>>>the value does not change.  When you do change it, you are using
>>>undefined behaviour.  When the compiler sees undefined behaviour, it
>>>does not behave predictably.
>>>
>>>ISO C99 6.7.3: "If an attempt is made to modify an object defined with
>>>a const-qualified type through use of an lvalue with
>>>non-const-qualified type, the behavior is undefined."
>>>
>>>      
>>>
>>I am a little confused.
>>    
>>
>
>Yes.
>
>  
>
>>The rule says:
>>If an attempt is made to modify an object, defined with a
>>const-qualified type, through use of an lvalue with a
>>non-const-qualified type, the behavior is undefined.
>>
>>Now, in the above, program, we are not trying to alter the value of
>>the variable x as follows:
>>x = 10;
>>
>>But we are trying to alter the contents of a memory location, which
>>happens to be where the variable 'x' refers to,  by using a pointer,
>>which is defined behaviour.
>>    
>>
>
>No.  Note that the rule is about modifying "an object".  The object in
>question was defined by the definition of 'x', and thus was defined
>using a const-qualified type.  The assignment through 'ptr' is
>modifying an object defined with a const-qualified type.
>
>  
>
>>According to the rule, you cannot use the variable, declared as a
>>const-qualified type, on the left hand side (lvalue, or assigned-to
>>variable), but it does not say anything about the memory being
>>modified.
>>    
>>
>
>No.  The rule I quoted doesn't say that you can't use a
>const-qualified type as an lvalue.  It says that you can't use an
>lvalue with a non-const-qualified type to modify an object defined
>with a const-qualified type.  *ptr is an lvalue with a
>non-const-qualified type.  It is being used to modify the object
>defined by the declaration of x with a const-qualified type.
>
>  
>
>>Please correct me if I am wrong. Also, when you qualify the type of x
>>to be a volatile, then the behaviour is as expected. So, there must be
>>some other reason why the variable "int *ptr" is being discarded by
>>Optimization. Check out that without optimization, you are able to
>>change the contents. Also, I've tried this with a few other compilers
>>(without optimization) and they all change the contents.
>>    
>>
>
>The program performs undefined behaviour.  Therefore the compiled
>results are unpredictable.  It is not surprising that the behaviour is
>different among different compilers and different optimization levels.
>
>Ian
>
>  
>

-- 
 *****************************
 * 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)
 *****************************

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-01-03  6:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-30  6:27 assigning to const int via pointer Joe Steeve
2004-12-30  7:19 ` Sriharsha
2004-12-30 16:02 ` Ian Lance Taylor
2004-12-31  6:44   ` Sriharsha
2004-12-31 11:08     ` Gabriel Dos Reis
2004-12-31 14:52     ` Ian Lance Taylor
2005-01-03  6:57       ` Sriharsha
2004-12-31 21:43   ` Joe Steeve

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).