Sujet : Re: "undefined behavior"?
De : schwarzb (at) *nospam* delq.com (Barry Schwarz)
Groupes : comp.lang.cDate : 12. Jun 2024, 22:30:26
Autres entêtes
Organisation : SunSITE.dk - Supporting Open source
Message-ID : <8t3k6j5ikf5mvimvksv2t91gbt11ljdfgb@4ax.com>
References : 1
User-Agent : Forte Agent 4.2/32.1118
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