Re: "undefined behavior"?

Liste des GroupesRevenir à cl c 
Sujet : Re: "undefined behavior"?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 12. Jun 2024, 23:38:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4d4h5$1rc9e$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 12/06/2024 22:47, DFS 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?
 
It is /really/ difficult to know exactly what your problem is without seeing your C code!  There may be other problems that you haven't seen yet.
Non-static local variables without initialisers have "indeterminate" value if there is no initialiser.  Trying to use these "indeterminate" values is undefined behaviour - you have absolutely no control over what might happen.  Any particular behaviour you see is done to luck from the rest of the code and what happened to be in memory at the time.
There is no automatic initialisation of non-static local variables, because that would often be inefficient.  The best way to avoid errors like yours, IMHO, is not to declare such variables until you have data to put in them - thus you always have a sensible initialiser of real data.  Occasionally that is not practical, but it works in most cases.
For a data array, zero initialisation is common.  Typically you do this with :
int xs[100] = { 0 };
That puts the explicit 0 in the first element of xs, and then the rest of the array is cleared with zeros.
I recommend never using "char" as a type unless you really mean a character, limited to 7-bit ASCII.  So if your "outliers" array really is an array of such characters, "char" is fine.  If it is intended to be numbers and for some reason you specifically want 8-bit values, use "uint8_t" or "int8_t", and initialise with { 0 }.
A major lesson here is to learn how to use your tools.  C is not a forgiving language.  Make use of all the help your tools can give you - enable warnings here.  "gcc -Wall" enables a range of common warnings with few false positives in normal well-written code, including ones that check for attempts to read uninitialised data.  "-Wextra" enables a slew of extra warnings.  Some of these will annoy people and trigger on code they find reasonable, while most are good choices for a lot of code - but personal preference varies significantly.  And remember to enable optimisation, since it makes the static checking more powerful.
If you /really/ want gcc to zero out such local data automatically, use "-ftrivial-auto-var-init=zero".  But it is much better to use warnings and write correct code - options like that one are an addition to well-checked code for paranoid software in security-critical contexts.

Date Sujet#  Auteur
12 Jun 24 * "undefined behavior"?77DFS
12 Jun 24 +* Re: "undefined behavior"?39Barry Schwarz
12 Jun 24 i`* Re: "undefined behavior"?38DFS
13 Jun 24 i `* Re: "undefined behavior"?37Keith Thompson
13 Jun 24 i  `* Re: "undefined behavior"?36DFS
13 Jun 24 i   `* Re: "undefined behavior"?35Keith Thompson
13 Jun 24 i    `* Re: "undefined behavior"?34Malcolm McLean
13 Jun 24 i     +- Re: "undefined behavior"?1Ben Bacarisse
13 Jun 24 i     +* Re: "undefined behavior"?29bart
13 Jun 24 i     i+* Re: "undefined behavior"?22Malcolm McLean
13 Jun 24 i     ii+* Re: "undefined behavior"?2Chris M. Thomasson
14 Jun 24 i     iii`- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii`* Re: "undefined behavior"?19Ben Bacarisse
14 Jun 24 i     ii `* Re: "undefined behavior"?18Malcolm McLean
14 Jun 24 i     ii  `* Re: "undefined behavior"?17Ben Bacarisse
14 Jun 24 i     ii   +* Re: "undefined behavior"?13Malcolm McLean
14 Jun 24 i     ii   i+* Re: "undefined behavior"?4Richard Harnden
14 Jun 24 i     ii   ii`* Re: "undefined behavior"?3Malcolm McLean
14 Jun 24 i     ii   ii `* Re: "undefined behavior"?2bart
14 Jun 24 i     ii   ii  `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   i`* Re: "undefined behavior"?8Ben Bacarisse
15 Jun 24 i     ii   i `* Re: "undefined behavior"?7Malcolm McLean
15 Jun 24 i     ii   i  +- Re: "undefined behavior"?1Ben Bacarisse
15 Jun 24 i     ii   i  `* Re: "undefined behavior"?5David Brown
15 Jun 24 i     ii   i   `* Re: "undefined behavior"?4Richard Harnden
16 Jun 24 i     ii   i    +- Re: "undefined behavior"?1Ben Bacarisse
16 Jun 24 i     ii   i    `* Re: "undefined behavior"?2David Brown
16 Jun 24 i     ii   i     `- Re: "undefined behavior"?1Malcolm McLean
14 Jun 24 i     ii   `* Re: "undefined behavior"?3Chris M. Thomasson
14 Jun 24 i     ii    `* Re: "undefined behavior"?2Ben Bacarisse
15 Jun 24 i     ii     `- Re: "undefined behavior"?1Chris M. Thomasson
14 Jun 24 i     i`* Re: "undefined behavior"?6Keith Thompson
14 Jun 24 i     i +- Re: "undefined behavior"?1bart
14 Jun 24 i     i +* Re: "undefined behavior"?3David Brown
14 Jun 24 i     i i`* Re: "undefined behavior"?2Keith Thompson
15 Jun 24 i     i i `- Re: "undefined behavior"?1David Brown
14 Jun 24 i     i `- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 i     `* Re: "undefined behavior"?3Keith Thompson
14 Jun 24 i      `* Re: "undefined behavior"?2Malcolm McLean
14 Jun 24 i       `- Re: "undefined behavior"?1Keith Thompson
12 Jun 24 +* Re: "undefined behavior"?15David Brown
13 Jun 24 i+* Re: "undefined behavior"?6Keith Thompson
13 Jun 24 ii+* Re: "undefined behavior"?2David Brown
14 Jun 24 iii`- Re: "undefined behavior"?1Keith Thompson
19 Jun 24 ii`* Re: "undefined behavior"?3Tim Rentsch
19 Jun 24 ii `* Re: "undefined behavior"?2Keith Thompson
22 Jun 24 ii  `- Re: "undefined behavior"?1Tim Rentsch
13 Jun 24 i`* Re: "undefined behavior"?8DFS
13 Jun 24 i +* Re: "undefined behavior"?4Ike Naar
13 Jun 24 i i`* Re: "undefined behavior"?3DFS
13 Jun 24 i i `* Re: "undefined behavior"?2Lew Pitcher
13 Jun 24 i i  `- Re: "undefined behavior"?1DFS
13 Jun 24 i `* Re: "undefined behavior"?3David Brown
14 Jun 24 i  `* Re: "undefined behavior"?2Keith Thompson
14 Jun 24 i   `- Re: "undefined behavior"?1David Brown
12 Jun 24 +* Re: "undefined behavior"?19Janis Papanagnou
13 Jun 24 i`* Re: "undefined behavior"?18Keith Thompson
13 Jun 24 i +* Re: "undefined behavior"?2Janis Papanagnou
13 Jun 24 i i`- Re: "undefined behavior"?1David Brown
13 Jun 24 i `* Re: "undefined behavior"?15David Brown
13 Jun 24 i  `* Re: "undefined behavior"?14DFS
14 Jun 24 i   `* Re: "undefined behavior"?13David Brown
15 Jun 24 i    +* Re: "undefined behavior"?11DFS
15 Jun 24 i    i`* Re: "undefined behavior"?10Keith Thompson
15 Jun 24 i    i `* Re: "undefined behavior"?9DFS
15 Jun 24 i    i  `* Re: "undefined behavior"?8Keith Thompson
15 Jun 24 i    i   `* Re: "undefined behavior"?7DFS
15 Jun 24 i    i    +* Re: "undefined behavior"?2Janis Papanagnou
15 Jun 24 i    i    i`- Re: "undefined behavior"?1DFS
15 Jun 24 i    i    +- Re: "undefined behavior"?1James Kuyper
15 Jun 24 i    i    +- Re: "undefined behavior"?1Keith Thompson
15 Jun 24 i    i    +- Re: "undefined behavior"?1bart
15 Jun 24 i    i    `- Re: "undefined behavior"?1David Brown
15 Jun 24 i    `- Re: "undefined behavior"?1David Brown
12 Jun 24 +- Re: "undefined behavior"?1Keith Thompson
13 Jun 24 +- Re: "undefined behavior"?1bart
13 Jun 24 `- Re: "undefined behavior"?1Bonita Montero

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal