Deutsch English Français Italiano |
<8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!weretis.net!feeder8.news.weretis.net!usenet.goja.nl.eu.org!dotsrc.org!filter.dotsrc.org!news.dotsrc.org!not-for-mail From: Barry Schwarz <schwarzb@delq.com> Newsgroups: comp.lang.c Subject: Re: "undefined behavior"? Date: Wed, 12 Jun 2024 14:30:26 -0700 Message-ID: <8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com> References: <666a095a$0$952$882e4bbb@reader.netnews.com> X-Newsreader: Forte Agent 4.2/32.1118 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 53 Organization: SunSITE.dk - Supporting Open source NNTP-Posting-Host: a3114a48.news.sunsite.dk X-Trace: 1718227830 news.sunsite.dk 714 schwarzb@q.com/71.212.170.240:59896 X-Complaints-To: staff@sunsite.dk Bytes: 3038 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. Since we cannot see your code, I will guess that you use a non-zero value in outliers[i] to indicate that the corresponding value has been identified as an outlier. Since you did not initialize the array outliers, you have no idea what indeterminate value any element of the array contains when your program begins execution. Apparently some of them are non-zero. The fact that the first 40 are zero and the remaining non-zero is merely an artifact of how your system builds this particular program with that particular set of compile and link options. Change anything and you could see completely different behavior, or not. I don't use gcc but, in debug mode, some compilers will put recognizable "garbage values" in uninitialized variables so you can spot the condition more easily. In any case, the C language does not prevent you from shooting yourself in the foot if you choose to. Evaluating an indeterminate value is one fairly common way to do this. -- Remove del for email