This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: how to make gcc warn about arithmetic signed overflow


>> > I think I can but it may cause an undefined behavior:
>>
>> Note, that Jonathan wrote "in a valid program". Your program is not
>> valid, as it contains undefined behaviour - you change const int via
>> pointer to non-const int.
>
> Is p pointer in my code a pointer to non-const int? It points to bc
> int memory address and bc is constant. Does C language bind constness
> to an indentifier instead of memory address?

Yes. I don't know about your version of gcc, but with 4.8.1 I get:

[jd@megalodon ~]$ cat x.c
int main() {
        const int i = 10;
        int* pc = &i;
        *pc = 1;
        return *pc;
}

[jd@megalodon ~]$ gcc x.c -o x
x.c: In function âmainâ:
x.c:3:12: warning: initialization discards âconstâ qualifier from
pointer target type [enabled by default]
  int* pc = &i;

Which means that pc doesn't inherit constness of pointed ...memory?
value? I'm not sure how to call it.
-- 
JÄdrzej Dudkiewicz

I really hate this damn machine, I wish that they would sell it.
It never does just what I want, but only what I tell it.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]