Sujet : Re: "undefined behavior"?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 12. Jun 2024, 22:57:19
Autres entêtes
Organisation : None to speak of
Message-ID : <87bk457rk0.fsf@nosuchdomain.example.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
DFS <
nospam@dfs.com> writes:
Wrote a C program to mimic the stats shown on:
>
https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php
And where is that C program? Do you expect us to help debug it without
seeing your code?
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.
Great, you've found the problem and solved it.
See question 1.30 of the comp.lang.c FAQ, <
https://www.c-faq.com/>.
Makes no sense. What could cause the program to go FUBAR at data
point 41+ only when the dataset is consecutive numbers?
Without seeing your code, we can't really tell what's going on, but
assuming that your `outliers` array has automatic storage duration
(i.e., is defined inside a function definition without `static`), its
initial value is indeterminate. It's not random; it's garbage. It
might be consistent from one run of your program to the next, just
because it gets its initial value from whatever is in memory when the
object is allocated. It might even happen to be all zeros, though
that's apparently not what happened in your case.
Also, why doesn't gcc just do you a solid and initialize to "" for you?
Because you didn't ask it to. The language says that static objects
(ones defined at file scope or with the `static` keyword) are
initialized to zero (that's a bit of an oversimplification; I'm skipping
over what "zero" means in this context). Automatic (local) objects
without initializers start with garbage values.
Initializing a character array with a string literal initializes the
entire array. Characters beyond the length of the string literal are
initialized to 0 ('\0').
Compilers can sometimes warn you when your code depends on indeterminate
values. I'd expect gcc to do so with the right options (try "-Wall").
Initializing automatic objects to all-bits-zero might be useful,
and I think some compilers might offer that as an option. But it
could substantially hurt performance. If you're careful enough to
write code that never depends on the values of uninitialized objects,
zero-initialization is a waste of time. Initializing static objects
to zero is cheap; it's done at program load time.
There are languages that make it difficult, or even impossible, to
read uninitialized objects. C is not such a language. It places a
greater burden on the programmer for the sake of runtime performance.
And yes, C's behavior here is a source of bugs that are often
difficult to diagnose, though compiler warnings can be very helpful
if you enable them.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */