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 : 14. Jun 2024, 19:18:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4hu1b$2ve93$3@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 13/06/2024 16:38, DFS wrote:
On 6/13/2024 9:21 AM, David Brown wrote:
On 13/06/2024 00:34, DFS wrote:
On 6/12/2024 6:22 PM, Keith Thompson wrote:
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
On 12.06.2024 22:47, DFS wrote:
[...]
before: char outliers[100];
after : char outliers[100] = "";
[...]
Seriously; why do you expect [in C] a declaration to initialize that
stack object? (There are other languages that do initializations as
the language defines it, but C doesn't; it may help to learn before
programming in any language?) And why do you think that "" would be
an appropriate initialization (i.e. a single '\0' character) and not
all 100 elements set to '\0'? (Someone else might want to access the
element 'answer[99]'.) And should we pay for initializing 1000000000
characters in case one declares an appropriate huge array?
>
This:
     char outliers[100] = "";
initializes all 100 elements to zero.  So does this:
     char outliers[100] = { '\0' };
Any elements or members not specified in an initializer are set to zero.
>
Yes.  It's good to point that out, since people might assume that using a string literal here only initialises the bit covered by that string literal.
>
(In C23 you can also write "char outliers[100] = {};" to get all zeros.)
>
>
If you want to set an array's 0th element to 0 and not waste time
initializing the rest, you can assign it separately:
     char outliers[100];
     outliers[0] = '\0';
or
     char outliers[100];
     strcpy(outliers, "");
though the overhead of the function call is likely to outweigh the
cost of initializing the array.
>
A good compiler will generate the same code for both cases - strcpy() is often inlined for such uses.
>
>
Thanks.  I'll have to remember these things.  I like to use char arrays.
>
The problem is I don't use C very often, so I don't develop muscle memory.
>
>
What programming language do you usually use?  And why are you writing in C instead?  (Or do you simply not do much programming?)
 I write a little code every few days.  Mostly python.
Certainly if I wanted to calculate some statistics from small data sets, I'd go for Python - it would not consider C unless it was for an embedded system.

 I like C for it's blazing speed.  Very addicting.  And it's much more challenging/frustrating than python.
With small data sets, Python has blazing speed - /every/ language has blazing speed.  And for large data sets, use numpy on Python and you /still/ have blazing speeds - a lot faster than anything you would write in C (because numpy's underlying code is written in C by people who are much better at writing fast numeric code than you or I).
The only reason to use C for something like is is for the challenge and fun, which is fair enough.

 I coded a subset (8 stat measures) of this C program 3.5 years ago, and recently decided to finish duplicating all 23 stats shown at:
 https://www.calculatorsoup.com/calculators/statistics/descriptivestatistics.php
 Working on the outliers code, I decided to add an option to generate data with consecutive numbers.  That's when I ran $./dfs 50 -c and noticed every value above 40 was considered an outlier.  And this didn't change over a bunch of code edits/file saves/compiles.
 Understanding how an uninitialized variable caused that persistent issue is beyond my pay grade.
Understanding that you should not read from a variable that has never been given a value is well within the pay grade of every programmer. And it's something that every C programmer should understand.  (And now you understand it too!)

 That's when I whined to clc.  Before I even posted, though, I spotted the uninitialized var (outliers).  Later I spotted another one (mode).
 One led to 'undefined behavior', the other to 'stack smashing'.  Both only occurred when using consecutive numbers.
 But with y'all's help I believe I found and fixed ALL issues.  I can dream anyway.
 

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