Deutsch English Français Italiano |
<vob4od$qdjb$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: James Kuyper <jameskuyper@alumni.caltech.edu> Newsgroups: comp.lang.c Subject: Re: Two questions on arrays with size defined by variables Date: Sun, 9 Feb 2025 15:55:41 -0500 Organization: A noiseless patient Spider Lines: 27 Message-ID: <vob4od$qdjb$2@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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 09 Feb 2025 21:55:44 +0100 (CET) Injection-Info: dont-email.me; posting-host="747f909815c9273d0d3564426e08bd23"; logging-data="865899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18U21RwmgGY42nU3PeKzzD62i8a4ajnS14=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:42e23SUSNUpcdceqv/u47kkTB10= Content-Language: en-US In-Reply-To: <voaq2u$ojms$1@dont-email.me> Bytes: 2757 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. The standard says "undefined behavior" when there are some situations where it can be arbitrarily difficult to identify violations of a rule at compile time. That applies to this rule, because if pointers are used, it can be quite difficult to confirm whether or not a given access will violate this rule. In this particular case. however, it would be trivial to detect the violation, but none of the compilers I've tested do so. 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.