Deutsch English Français Italiano |
<666a2a30$0$952$882e4bbb@reader.netnews.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news-out.netnews.com!postmaster.netnews.com!us14.netnews.com!not-for-mail X-Trace: DXC=O5KK\o<gK40XbTXBL>DDB=HWonT5<]0T=Q;nb^V>PUf6=AnO\FUBY[0nF54O@^\1?4KTo[:hc[gi=Wk5\ZZ8Yg00nk9`jfQW@>=dDioXmM8L11OXeKkS2`?j9 X-Complaints-To: support@blocknews.net Date: Wed, 12 Jun 2024 19:07:29 -0400 MIME-Version: 1.0 User-Agent: Betterbird (Windows) Subject: Re: "undefined behavior"? Newsgroups: comp.lang.c References: <666a095a$0$952$882e4bbb@reader.netnews.com> <8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com> <666a18de$0$958$882e4bbb@reader.netnews.com> <87y1796bfn.fsf@nosuchdomain.example.com> Content-Language: en-US From: DFS <nospam@dfs.com> In-Reply-To: <87y1796bfn.fsf@nosuchdomain.example.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 122 Message-ID: <666a2a30$0$952$882e4bbb@reader.netnews.com> NNTP-Posting-Host: 127.0.0.1 X-Trace: 1718233648 reader.netnews.com 952 127.0.0.1:54681 Bytes: 4851 On 6/12/2024 6:30 PM, Keith Thompson wrote: > DFS <nospam@dfs.com> writes: >> On 6/12/2024 5:30 PM, Barry Schwarz wrote: >>> On Wed, 12 Jun 2024 16:47:23 -0400, DFS <nospam@dfs.com> wrote: >>> >>>> Wrote a C program to mimic the stats shown on: >>>> >>>> https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php >>>> >>>> My code compiles and works fine - every stat matches - except for one >>>> anomaly: when using a dataset of consecutive numbers 1 to N, all values >>>>> 40 are flagged as outliers. Up to 40, no problem. Random numbers >>>> dataset of any size: no problem. >>>> >>>> And values 41+ definitely don't meet the conditions for outliers (using >>>> the IQR * 1.5 rule). >>>> >>>> Very strange. >>>> >>>> Edit: I just noticed I didn't initialize a char: >>>> before: char outliers[100]; >>>> after : char outliers[100] = ""; >>>> >>>> And the problem went away. Reset it to before and problem came back. >>>> >>>> Makes no sense. What could cause the program to go FUBAR at data point >>>> 41+ only when the dataset is consecutive numbers? > >>>> Also, why doesn't gcc just do you a solid and initialize to "" for you? >>> Makes perfect sense. The first rule of undefined behavior is >>> "Whatever happens is exactly correct." You are not entitled to any >>> expectations and none of the behavior (or perhaps all of the behavior) >>> can be called unexpected. >> >> I HATE bogus answers like this. >> >> Aren't you embarrassed to say things like that? > > He has nothing to be embarrassed about. What he wrote is correct. No it's not. "Whatever happens is exactly correct." is nonsense. "You are not entitled to any expectations" is nonsense. > The C standard's definition of "undefined behavior" is "behavior, upon > use of a nonportable or erroneous program construct or of erroneous > data, for which this International Standard imposes no requirements". > > If you don't like the way C deals with undefined behavior, that's > perfectly valid, and a lot of people are likely to agree with you. Thanks for feeling my pain! It's frustrating. By now I spent a half-hour dealing with it. gcc could've just filled the char[] variable with 0s by default. I bet that would save a LOT of people time and headaches. > But I advise against lashing out at people who are correctly explaining > what the C standard says. The C standard really says "Whatever happens is exactly correct."? > DFS, since you've been posting in comp.lang.c for at least ten years, Time flies. How do you know I've posted here that long? > I'm surprised you're having difficulties with this. I'm surprised at some of the wonkiness of gcc and C. * warns relentlessly when the printf specifier doesn't match the var type, but gives no warning when you use an int with memset (instead of the size_t specified in the function prototype). * a missing bracket } throws 50 nonsensical compiler errors. * warns of unused vars but not uninitialized ones * one uninitialized var makes your program do crazy things. Worse than crazy is it's identically crazy each time. ../prog 40 -c outliers: none ../prog 41 -c outliers: 41 ../prog 42 -c outliers: 41 42 ../prog 43 -c outliers: 41 42 43 ../prog 44 -c outliers: 41 42 43 44 etc. And none were outliers - not even close. At least if it showed nonsense data it would be easier to track down. Maybe. The thing is, none of those values (40+) were ever in that char[] prior to running the code for a set of 50 consecutive values. And I edited/compiled the code many times, but still got the identical error. I doubt my environment (gcc 11.4 on Windows Subsys for Linux on Ubuntu) has anything to do with it.