Deutsch English Français Italiano |
<vhpuod$3mlgf$2@paganini.bofh.team> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail From: antispam@fricas.org (Waldek Hebisch) Newsgroups: comp.lang.c Subject: Re: else ladders practice Date: Fri, 22 Nov 2024 12:51:27 -0000 (UTC) Organization: To protect and to server Message-ID: <vhpuod$3mlgf$2@paganini.bofh.team> References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org> <vgdt36$2r682$2@paganini.bofh.team> <vge8un$1o57r$3@dont-email.me> <vgpi5h$6s5t$1@paganini.bofh.team> <vgtsli$1690f$1@dont-email.me> <vhgr1v$2ovnd$1@paganini.bofh.team> <vhic66$1thk0$1@dont-email.me> <vhins8$1vuvp$1@dont-email.me> <vhj7nc$2svjh$1@paganini.bofh.team> <vhje8l$2412p$1@dont-email.me> <vhl1up$5vdg$1@dont-email.me> <vhlg53$8lff$1@dont-email.me> <vhnasl$l8h5$1@dont-email.me> <vhnj3n$mk94$1@dont-email.me> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 22 Nov 2024 12:51:27 -0000 (UTC) Injection-Info: paganini.bofh.team; logging-data="3888655"; posting-host="WwiNTD3IIceGeoS5hCc4+A.user.paganini.bofh.team"; mail-complaints-to="usenet@bofh.team"; posting-account="9dIQLXBM7WM9KzA+yjdR4A"; User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64)) X-Notice: Filtered by postfilter v. 0.9.3 Bytes: 3020 Lines: 73 Bart <bc@freeuk.com> wrote: > > int main(void) { > int a; > int* p = 0; > a = *p; > } > > Here's what happens with my C compiler when told to interpret it: > > c:\cx>cc -i c > Compiling c.c to c.(int) > Error: Null ptr access > > Here's what happens with gcc: > > c:\cx>gcc c.c > c:\cx>a > <crashes> > > Is there some option to insert such a check with gcc? I've no idea; most > people don't. I would do gcc -g c.c gdb a.out run and gdb would show me place with bad access. Things like bound checking array access or overflow checking makes a big difference. Null pointer access is reliably detected by hardware so no big deal. Say what you 'cc' will do with the following function: int foo(int n) { int a[10]; int i; int res = 0; for(i = 0; i <= 10; i++) { a[i] = n + i; } for(i = 0; i <= 10; i++) { res += a[i]; } res; } Here gcc at compile time says: foo.c: In function ‘foo’: foo.c:15:17: warning: iteration 10 invokes undefined behavior [-Waggressive-loop-optimizations] 15 | res += a[i]; | ~^~~ foo.c:14:18: note: within this loop 14 | for(i = 0; i <= 10; i++) { | ~~^~~~~ Of course, there are also cases like void bar(int n, int a[n]) { int i; for(i = 0; i <= n; i++) { a[i] = i; } } which are really wrong, but IIUC C standard considers them OK. Still, good compiler should have an option to flag them either at compile time or at runtime. -- Waldek Hebisch