public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug c/56647] New: use return value of malloc() as initializer @ 2013-03-18 5:30 zhabgyuan1993 at gmail dot com 2013-03-18 5:49 ` [Bug c/56647] " pinskia at gcc dot gnu.org ` (4 more replies) 0 siblings, 5 replies; 6+ messages in thread From: zhabgyuan1993 at gmail dot com @ 2013-03-18 5:30 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 Bug #: 56647 Summary: use return value of malloc() as initializer Classification: Unclassified Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: zhabgyuan1993@gmail.com Created attachment 29687 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29687 the preprocessed file for r.c $:cat r.c ------------------------------------------------------ #include<stdio.h> #include<stdlib.h> int *c; c = (int *)malloc(sizeof(int)); int main(void) { return 0; } error or warning ------------------------------------------------------- r.c:4:1: warning: data definition has no type or storage class [enabled by default] r.c:4:1: error: conflicting types for ‘c’ r.c:3:6: note: previous declaration of ‘c’ was here r.c:4:5: warning: initialization makes integer from pointer without a cast [enabled by default] r.c:4:1: error: initializer element is not constant $: gcc -v -save-temps r.c ------------------------------------------------------- Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1) COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/4.7/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu r.c -mtune=generic -march=x86-64 -fpch-preprocess -fstack-protector -o r.i ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/lib/gcc/x86_64-linux-gnu/4.7/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list. COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/4.7/cc1 -fpreprocessed r.i -quiet -dumpbase r.c -mtune=generic -march=x86-64 -auxbase r -version -fstack-protector -o r.s GNU C (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu) compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu) compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9 GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: efa32fef7aa241fa03ac6d7ad2a4a2cf r.c:4:1: warning: data definition has no type or storage class [enabled by default] r.c:4:1: error: conflicting types for ‘c’ r.c:3:6: note: previous declaration of ‘c’ was here r.c:4:5: warning: initialization makes integer from pointer without a cast [enabled by default] r.c:4:1: error: initializer element is not constant ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/56647] use return value of malloc() as initializer 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com @ 2013-03-18 5:49 ` pinskia at gcc dot gnu.org 2013-03-18 5:55 ` zhabgyuan1993 at gmail dot com ` (3 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2013-03-18 5:49 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 --- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-03-18 05:48:41 UTC --- The diagnostic seems correct for me. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/56647] use return value of malloc() as initializer 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com 2013-03-18 5:49 ` [Bug c/56647] " pinskia at gcc dot gnu.org @ 2013-03-18 5:55 ` zhabgyuan1993 at gmail dot com 2013-03-18 6:12 ` pinskia at gcc dot gnu.org ` (2 subsequent siblings) 4 siblings, 0 replies; 6+ messages in thread From: zhabgyuan1993 at gmail dot com @ 2013-03-18 5:55 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 --- Comment #2 from shia <zhabgyuan1993 at gmail dot com> 2013-03-18 05:54:54 UTC --- I think maybe only this one is right: r.c:4:1: error: initializer element is not constant the other part may cause little confused. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/56647] use return value of malloc() as initializer 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com 2013-03-18 5:49 ` [Bug c/56647] " pinskia at gcc dot gnu.org 2013-03-18 5:55 ` zhabgyuan1993 at gmail dot com @ 2013-03-18 6:12 ` pinskia at gcc dot gnu.org 2013-03-18 6:35 ` zhabgyuan1993 at gmail dot com 2013-03-18 9:39 ` redi at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: pinskia at gcc dot gnu.org @ 2013-03-18 6:12 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 --- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-03-18 06:11:55 UTC --- (In reply to comment #0) > ------------------------------------------------------- > r.c:4:1: warning: data definition has no type or storage class [enabled by > default] > r.c:4:1: error: conflicting types for ‘c’ > r.c:3:6: note: previous declaration of ‘c’ was here > r.c:4:5: warning: initialization makes integer from pointer without a cast > [enabled by default] > r.c:4:1: error: initializer element is not constant > Since this is at the top level, only definitions are allowed which is what GCC is assuming. GCC has an extension of not needing a type or storage class for some variable and assume int as the type. That comes to the second error message of the conflicting types. The next warning is due to the int of c now being int from the extension. And the last error message is saying the initializer element is not constant as this is the top level. That explains exactly what is going on and why GCC's warnings/error messages are correct. The trunk gives: t1.c:4:1: warning: data definition has no type or storage class [enabled by default] c = (int *)malloc(sizeof(int)); ^ t1.c:4:1: error: conflicting types for ‘c’ t1.c:3:6: note: previous declaration of ‘c’ was here int *c; ^ t1.c:4:5: warning: initialization makes integer from pointer without a cast [enabled by default] c = (int *)malloc(sizeof(int)); ^ t1.c:4:1: error: initializer element is not constant c = (int *)malloc(sizeof(int)); ^ Which should better explain what is going on here. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/56647] use return value of malloc() as initializer 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com ` (2 preceding siblings ...) 2013-03-18 6:12 ` pinskia at gcc dot gnu.org @ 2013-03-18 6:35 ` zhabgyuan1993 at gmail dot com 2013-03-18 9:39 ` redi at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: zhabgyuan1993 at gmail dot com @ 2013-03-18 6:35 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 shia <zhabgyuan1993 at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |RESOLVED Resolution| |FIXED --- Comment #4 from shia <zhabgyuan1993 at gmail dot com> 2013-03-18 06:34:54 UTC --- Thank you.It's clear after your clarifying patiently ^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug c/56647] use return value of malloc() as initializer 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com ` (3 preceding siblings ...) 2013-03-18 6:35 ` zhabgyuan1993 at gmail dot com @ 2013-03-18 9:39 ` redi at gcc dot gnu.org 4 siblings, 0 replies; 6+ messages in thread From: redi at gcc dot gnu.org @ 2013-03-18 9:39 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56647 Jonathan Wakely <redi at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|FIXED |INVALID --- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-03-18 09:38:41 UTC --- . ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-03-18 9:39 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2013-03-18 5:30 [Bug c/56647] New: use return value of malloc() as initializer zhabgyuan1993 at gmail dot com 2013-03-18 5:49 ` [Bug c/56647] " pinskia at gcc dot gnu.org 2013-03-18 5:55 ` zhabgyuan1993 at gmail dot com 2013-03-18 6:12 ` pinskia at gcc dot gnu.org 2013-03-18 6:35 ` zhabgyuan1993 at gmail dot com 2013-03-18 9:39 ` redi at gcc dot gnu.org
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).