Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #103344 > unrolled thread

Re: Error message for a dangling pointer

Started byKeith Thompson <kst-u@mib.org>
First post2017-02-06 18:02 -0800
Last post2017-02-07 16:24 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Error message for a dangling pointer Keith Thompson <kst-u@mib.org> - 2017-02-06 18:02 -0800
    Re: Error message for a dangling pointer BartC <bc@freeuk.com> - 2017-02-07 15:07 +0000
      Re: Error message for a dangling pointer David Brown <david.brown@hesbynett.no> - 2017-02-07 16:24 +0100

#103344 — Re: Error message for a dangling pointer

FromKeith Thompson <kst-u@mib.org>
Date2017-02-06 18:02 -0800
SubjectRe: Error message for a dangling pointer
Message-ID<ln1sva3gh8.fsf@kst-u.example.com>
ram@zedat.fu-berlin.de (Stefan Ram) writes:
>   For teaching purposes, I just wrote this program to show
>   dangling pointers:
>
> #include <stdio.h>
> int main( void )
> { int * p;
>   { int i = 27; p = &i; }
>   printf( "%d\n", *p ); }

(A side note: Your students might later have difficulties with more, um,
conventional C code layouts.)

>   I hope you all agree that »p« in the last line /is/
>   a dangling pointer.

Yes.

>   The error message and output is not what I expected,
>   but still interesting:
>
> main.c: In function 'main':
> main.c:6:3: warning: 'i' is used uninitialized in this function [-Wuninitialized]
>    printf( "%d\n", *p ); }
>    ^
> main.c:5:9: note: 'i' was declared here
>    { int i = 27; p = &i; }
>          ^
> 0
>
>   »i« /was/ initialized in the program,
>   so the error message is not accurate.
>
>   (I do not write »misleading«, since it is so
>   obviously wrong that it won't mislead anyone.)

I get that message with gcc 6.3.0, and with a gcc snapshot from
2017-01-12 that claims to be gcc 7.0.0, but not with gcc 6.2.0
(or with clang).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [next] | [standalone]


#103360

FromBartC <bc@freeuk.com>
Date2017-02-07 15:07 +0000
Message-ID<o7cno2$fav$1@dont-email.me>
In reply to#103344
On 07/02/2017 02:02, Keith Thompson wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>   For teaching purposes, I just wrote this program to show
>>   dangling pointers:
>>
>> #include <stdio.h>
>> int main( void )
>> { int * p;
>>   { int i = 27; p = &i; }
>>   printf( "%d\n", *p ); }
>
> (A side note: Your students might later have difficulties with more, um,
> conventional C code layouts.)
>
>>   I hope you all agree that »p« in the last line /is/
>>   a dangling pointer.
>
> Yes.
>
>>   The error message and output is not what I expected,
>>   but still interesting:
>>
>> main.c: In function 'main':
>> main.c:6:3: warning: 'i' is used uninitialized in this function [-Wuninitialized]
>>    printf( "%d\n", *p ); }
>>    ^
>> main.c:5:9: note: 'i' was declared here
>>    { int i = 27; p = &i; }
>>          ^
>> 0
>>
>>   »i« /was/ initialized in the program,
>>   so the error message is not accurate.
>>
>>   (I do not write »misleading«, since it is so
>>   obviously wrong that it won't mislead anyone.)
>
> I get that message with gcc 6.3.0, and with a gcc snapshot from
> 2017-01-12 that claims to be gcc 7.0.0, but not with gcc 6.2.0
> (or with clang).

I couldn't get any diagnostic with any compiler. That included gcc 5.1.x 
and 6.2.x with all warnings turned on.

-- 
bartc

[toc] | [prev] | [next] | [standalone]


#103365

FromDavid Brown <david.brown@hesbynett.no>
Date2017-02-07 16:24 +0100
Message-ID<o7coog$k0c$1@dont-email.me>
In reply to#103360
On 07/02/17 16:07, BartC wrote:
> On 07/02/2017 02:02, Keith Thompson wrote:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>>   For teaching purposes, I just wrote this program to show
>>>   dangling pointers:
>>>
>>> #include <stdio.h>
>>> int main( void )
>>> { int * p;
>>>   { int i = 27; p = &i; }
>>>   printf( "%d\n", *p ); }
>>
>> (A side note: Your students might later have difficulties with more, um,
>> conventional C code layouts.)
>>
>>>   I hope you all agree that »p« in the last line /is/
>>>   a dangling pointer.
>>
>> Yes.
>>
>>>   The error message and output is not what I expected,
>>>   but still interesting:
>>>
>>> main.c: In function 'main':
>>> main.c:6:3: warning: 'i' is used uninitialized in this function
>>> [-Wuninitialized]
>>>    printf( "%d\n", *p ); }
>>>    ^
>>> main.c:5:9: note: 'i' was declared here
>>>    { int i = 27; p = &i; }
>>>          ^
>>> 0
>>>
>>>   »i« /was/ initialized in the program,
>>>   so the error message is not accurate.
>>>
>>>   (I do not write »misleading«, since it is so
>>>   obviously wrong that it won't mislead anyone.)
>>
>> I get that message with gcc 6.3.0, and with a gcc snapshot from
>> 2017-01-12 that claims to be gcc 7.0.0, but not with gcc 6.2.0
>> (or with clang).
> 
> I couldn't get any diagnostic with any compiler. That included gcc 5.1.x
> and 6.2.x with all warnings turned on.
> 

Testing with <https://gcc.godbolt.org/> I get that for gcc 5 onwards
(including 6.2 and 5.1), but not clang.

However, like many warnings you need to have optimisation enabled so
that gcc will do more analysis of the code.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web