Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Error message for a dangling pointer Date: Mon, 06 Feb 2017 18:02:59 -0800 Organization: None to speak of Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="04056ce4c9c7dd85ee7ce897aab9f52a"; logging-data="30489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ym3YHaIkmMyaO5DcCCDe1" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:sbFGBbuMB3OTjvbs92p1WSusvMg= sha1:9+JjqzzMXqSnlhRu/UltcOdw0Jc= Xref: csiph.com comp.lang.c:103344 ram@zedat.fu-berlin.de (Stefan Ram) writes: > For teaching purposes, I just wrote this program to show > dangling pointers: > > #include > 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 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"