| Deutsch English Français Italiano |
|
<vobmm7$tju0$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: comp.lang.c Subject: Re: Two questions on arrays with size defined by variables Date: Mon, 10 Feb 2025 03:01:41 +0100 Organization: A noiseless patient Spider Lines: 37 Message-ID: <vobmm7$tju0$1@dont-email.me> References: <vo9mns$gsd7$1@dont-email.me> <vo9nn3$gtph$1@dont-email.me> <vo9u0u$i0n8$1@dont-email.me> <voae3a$2rke4$1@paganini.bofh.team> <voao29$o6uh$2@dont-email.me> <voaol9$o8ic$1@dont-email.me> <voapm4$oh21$1@dont-email.me> <voaq2u$ojms$1@dont-email.me> <vob4od$qdjb$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 10 Feb 2025 03:01:43 +0100 (CET) Injection-Info: dont-email.me; posting-host="7ae7436750940773d2334410c5fdf1e5"; logging-data="970688"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1/G7J4xDu5rgLke9tYHSF" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:9VbA4CWjVkdyLsJFgbF2I3lylpM= In-Reply-To: <vob4od$qdjb$2@dont-email.me> On 09.02.2025 21:55, James Kuyper wrote: > On 2/9/25 12:53, Janis Papanagnou wrote: >> On 09.02.2025 18:46, Janis Papanagnou wrote: >>> [...] my test-wise - deliberately >>> wrong! - assignment to 'a[99]' produced also no compiler complaints, >> >> BTW, it produced also no core dump or any other runtime error. >> (But it is obviously severely wrong code anyway.) > > Accessing an array beyond it's bounds has undefined behavior, whether or > not it is a VLA. "undefined behavior" means that the C standard imposes > no requirements on the behavior. In particular, it does not require a > diagnostic message, nor a core dump, nor any other kind of runtime error. Yes. (There's reasons why I prefer programming languages with checks.) > [...] > In this particular case. however, it would be trivial to detect the > violation, but none of the compilers I've tested do so. Yes, because it's a special case with a constant defined. But the value may come from "outside", undetectable by the compiler and only detectable during runtime if there's additional information maintained (stored and checked). > When such code is accepted by an implementation, what is most likely to > happen is that the compiler will generate code that creates an array > containing "foobar", and which attempts to write a pointer to the first > element of that array to the location where a[99] should be, if a had at > least 100 elements. Depending upon what that piece of memory is being > used for, the results could be catastrophic, or completely innocuous, or > somewhere in-between. Yes. That matches exactly my fears or expectations here [with "C"]. Janis