Re: More Funny Stuff From Joel

Liste des GroupesRevenir à ol advocacy 
Sujet : Re: More Funny Stuff From Joel
De : bowman (at) *nospam* montana.com (rbowman)
Groupes : comp.os.linux.advocacy
Date : 15. Jun 2024, 04:33:58
Autres entêtes
Message-ID : <ld4cslFnt9iU1@mid.individual.net>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Pan/0.149 (Bellevue; 4c157ba)
On Fri, 14 Jun 2024 14:14:33 -0400, DFS wrote:


Yep.  In the big stats program I posted I messed around with using a
malloced array of 20,000.  It ran a bunch of times just fine, but all of
a sudden I got nonsense results.
 
So I filled it with 0s before using it, and no problem.
 
But calloc saves a line of code, so that's what I used.

Yeah, that's the joy of undefined behavior.  In the original program with
'int mode' not initialized it segfaulted pretty reliably on
'md_count[mode]' if 'mode' happened to be a  garbage number. It *might*
have been 0 for a dew runs.  'double mean' was uninitialized but on a few
runs it was 0.0. Maybe the compiler was feeling nice but I wouldn't bet on
it. If it wasn't 0.0, 'mean += nums[len];' wouldn't blow up but it would
be bogus.

'int md_count[COUNT_CNT];' was another uninitialized one. I printed it out
and some elements were 0, some were bogus which wiped out the mode logic
even with mode initialized.

Luckily, it did have 10 elements although the 0th would never be used. If
it were md_count[COUNT_CNT-1] it could potentially get interesting. It's
on the stack so md_count[9] possibly could corrupt something else on the
stack like 'len'. That's a real crap shoot that depends on how the stack
frame was built and what is adjacent.

 
On geeksforgeeks a calloc() example is:
 
   int* ptr;
   ptr = (int*)calloc(n, sizeof(int));
 
Isn't the 2nd int* redundant?

Most C compilers let it slide but malloc, calloc, and realloc return void
pointers.  C++ doesn't.

$ g++  -o mean mean.c
mean.c: In function ‘int main()’:
mean.c:15:21: error: invalid conversion from ‘void*’ to ‘int*’ [-
fpermissive]
   15 |         ptr = calloc(COUNT_CNT, sizeof(int));
      |               ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
      |                     |
      |                     void*


If you work with both languages and/or different compilers you get into
the habit of explicitly casting the return. A C++ purist would frown on
using calloc or malloc although you certainly can. 'new' probably uses one
or the other under the hood but you don't want to mix and match free and
delete.

Going the other way C++ allowed variable declarations outside of blocks. C
finally caught up, I forget which version, but I would follow the C rule
in C++ although I really prefer declaring a variable close to where you're
going to use it.



 





Date Sujet#  Auteur
8 Jun 24 * More Funny Stuff From The Joke Python131Diego Garcia
8 Jun 24 +* Re: More Funny Stuff From The Joke Python117Joel
8 Jun 24 i+* Re: More Funny Stuff From The Joke Python110rbowman
8 Jun 24 ii`* Re: More Funny Stuff From The Joke Python109rbowman
8 Jun 24 ii +* Languages (was: Re: More Funny Stuff From The Joke Python)7vallor
8 Jun 24 ii i+* Re: Languages2%
8 Jun 24 ii ii`- Re: Languages1vallor
9 Jun 24 ii i`* Re: Languages (was: Re: More Funny Stuff From The Joke Python)4Lawrence D'Oliveiro
24 Jun 24 ii i `* Re: Languages (was: Re: More Funny Stuff From The Joke Python)3Sebastian Wells
24 Jun 24 ii i  +- Re: Languages (was: Re: More Funny Stuff From Python)1Lawrence D'Oliveiro
24 Jun 24 ii i  `- Re: Languages (was: Re: More Funny Stuff From The Joke Python)1rbowman
9 Jun 24 ii +* Re: More Funny Stuff From Joel95DFS
9 Jun 24 ii i+- Re: More Funny Stuff From Joel1Lawrence D'Oliveiro
9 Jun 24 ii i+* Re: More Funny Stuff From Joel2rbowman
9 Jun 24 ii ii`- Re: More Funny Stuff From Joel1Chris Ahlstrom
9 Jun 24 ii i+* Re: More Funny Stuff From Joel34Tyrone
9 Jun 24 ii ii+* Re: More Funny Stuff From Joel6Joel
9 Jun 24 ii iii`* Re: More Funny Stuff From Joel5rbowman
9 Jun 24 ii iii `* Re: More Funny Stuff From Joel4Joel
9 Jun 24 ii iii  `* Re: More Funny Stuff From Joel3rbowman
9 Jun 24 ii iii   `* Re: More Funny Stuff From Joel2Joel
9 Jun 24 ii iii    `- Re: More Funny Stuff From Joel1rbowman
11 Jun 24 ii ii+* Re: More Funny Stuff From Joel14candycanearter07
11 Jun 24 ii iii+* Re: More Funny Stuff From Joel2Chris Ahlstrom
12 Jun 24 ii iiii`- Re: More Funny Stuff From Joel1candycanearter07
14 Jun 24 ii iii`* Re: More Funny Stuff From Joel11Stéphane CARPENTIER
14 Jun 24 ii iii `* Re: More Funny Stuff From Joel10Farley Flud
14 Jun 24 ii iii  +* Re: More Funny Stuff From Joel8Stéphane CARPENTIER
14 Jun 24 ii iii  i`* Re: More Funny Stuff From Joel7Farley Flud
15 Jun 24 ii iii  i +- Re: More Funny Stuff From Joel1DFS
15 Jun 24 ii iii  i `* Re: More Funny Stuff From Joel5Stéphane CARPENTIER
15 Jun 24 ii iii  i  +* Re: More Funny Stuff From Joel2Farley Flud
15 Jun 24 ii iii  i  i`- Re: More Funny Stuff From Joel1Stéphane CARPENTIER
15 Jun 24 ii iii  i  `* Re: More Funny Stuff From Joel2Farley Flud
15 Jun 24 ii iii  i   `- Re: More Funny Stuff From Joel1Stéphane CARPENTIER
14 Jun 24 ii iii  `- Re: More Funny Stuff From Joel1DFS
14 Jun 24 ii ii`* Re: More Funny Stuff From Joel13Stéphane CARPENTIER
14 Jun 24 ii ii `* Re: More Funny Stuff From Joel12Joel
14 Jun 24 ii ii  +* Re: More Funny Stuff From Joel6Stéphane CARPENTIER
15 Jun 24 ii ii  i`* Re: More Funny Stuff From Joel5candycanearter07
15 Jun 24 ii ii  i +* Re: More Funny Stuff From Joel2Joel
16 Jun 24 ii ii  i i`- Re: More Funny Stuff From Joel1candycanearter07
16 Jun 24 ii ii  i `* Re: More Funny Stuff From Joel2Lawrence D'Oliveiro
16 Jun 24 ii ii  i  `- Re: More Funny Stuff From Joel1candycanearter07
16 Jun 24 ii ii  `* Re: More Funny Stuff From Joel5Lawrence D'Oliveiro
16 Jun 24 ii ii   `* Re: More Funny Stuff From Joel4Joel
16 Jun 24 ii ii    +* Re: More Funny Stuff From Joel2Lawrence D'Oliveiro
16 Jun 24 ii ii    i`- Re: More Funny Stuff From Joel1Joel
19 Jun 24 ii ii    `- Re: More Funny Stuff From Joel1Joel
9 Jun 24 ii i+* Re: More Funny Stuff From Joel20DFS
9 Jun 24 ii ii`* Re: More Funny Stuff From Joel19Joel
9 Jun 24 ii ii `* Re: More Funny Stuff From Joel18rbowman
9 Jun 24 ii ii  `* Re: More Funny Stuff From Joel17Joel
9 Jun 24 ii ii   +- Re: More Funny Stuff From Joel1%
9 Jun 24 ii ii   +* Re: More Funny Stuff From Joel5DFS
9 Jun 24 ii ii   i`* "Standard GUI" (was: Re: More Funny Stuff From Joel)4vallor
10 Jun 24 ii ii   i +- Re: "Standard GUI"1rbowman
10 Jun 24 ii ii   i `* Re: "Standard GUI"2Lawrence D'Oliveiro
10 Jun 24 ii ii   i  `- Re: "Standard GUI"1Chris Ahlstrom
9 Jun 24 ii ii   +- Re: More Funny Stuff From Joel1rbowman
11 Jun 24 ii ii   `* Re: More Funny Stuff From Joel9candycanearter07
11 Jun 24 ii ii    +* Re: More Funny Stuff From Joel2DFS
11 Jun 24 ii ii    i`- Re: More Funny Stuff From Joel1Lawrence D'Oliveiro
11 Jun 24 ii ii    `* Re: More Funny Stuff From Joel6Joel
12 Jun 24 ii ii     +* Re: More Funny Stuff From Joel4Lawrence D'Oliveiro
12 Jun 24 ii ii     i+* Re: More Funny Stuff From Joel2Lawrence D'Oliveiro
12 Jun 24 ii ii     ii`- Re: More Funny Stuff From Joel1Chris Ahlstrom
12 Jun 24 ii ii     i`- Re: More Funny Stuff From Joel1candycanearter07
12 Jun 24 ii ii     `- Re: More Funny Stuff From Joel1candycanearter07
11 Jun 24 ii i`* Re: More Funny Stuff From Joel37candycanearter07
11 Jun 24 ii i +- Re: More Funny Stuff From Joel1Lawrence D'Oliveiro
11 Jun 24 ii i +* Re: More Funny Stuff From Joel10rbowman
11 Jun 24 ii i i`* Re: More Funny Stuff From Joel9candycanearter07
11 Jun 24 ii i i `* Re: More Funny Stuff From Joel8rbowman
11 Jun 24 ii i i  +* Re: More Funny Stuff From Joel2Lawrence D'Oliveiro
11 Jun 24 ii i i  i`- Re: More Funny Stuff From Joel1candycanearter07
11 Jun 24 ii i i  `* Re: More Funny Stuff From Joel5Chris Ahlstrom
11 Jun 24 ii i i   `* Re: More Funny Stuff From Joel4rbowman
11 Jun 24 ii i i    +- Re: More Funny Stuff From Joel1Chris Ahlstrom
12 Jun 24 ii i i    `* Re: More Funny Stuff From C2Lawrence D'Oliveiro
12 Jun 24 ii i i     `- Re: More Funny Stuff From C1candycanearter07
11 Jun 24 ii i +* Re: More Funny Stuff From Joel2rbowman
12 Jun 24 ii i i`- Re: More Funny Stuff From Python1Lawrence D'Oliveiro
12 Jun 24 ii i `* Re: More Funny Stuff From Joel23candycanearter07
12 Jun 24 ii i  +* Re: More Funny Stuff From Joel2Lawrence D'Oliveiro
13 Jun 24 ii i  i`- Re: More Funny Stuff From Joel1candycanearter07
13 Jun 24 ii i  `* Re: More Funny Stuff From Joel20candycanearter07
13 Jun 24 ii i   `* Re: More Funny Stuff From Joel19DFS
13 Jun 24 ii i    `* Re: More Funny Stuff From Joel18rbowman
14 Jun 24 ii i     +* Re: More Funny Stuff From Joel16rbowman
14 Jun 24 ii i     i`* Re: More Funny Stuff From Joel15rbowman
15 Jun 24 ii i     i +* Re: More Funny Stuff From Joel13rbowman
17 Jun 24 ii i     i i`* Re: More Funny Stuff From Joel12rbowman
18 Jun 24 ii i     i i `* Re: More Fun Stuff From Lunar Lander11Lawrence D'Oliveiro
18 Jun 24 ii i     i i  `* Re: More Fun Stuff From Lunar Lander10rbowman
18 Jun 24 ii i     i i   +* Re: More Fun Stuff From Lunar Lander2Farley Flud
18 Jun 24 ii i     i i   i`- Re: More Fun Stuff From Lunar Lander1Joel
24 Jun 24 ii i     i i   `* Re: More Fun Stuff From Lunar Lander7Lawrence D'Oliveiro
24 Jun 24 ii i     i i    `* Re: More Fun Stuff From Lunar Lander6rbowman
24 Jun 24 ii i     i i     `* Re: More Fun Stuff From Lunar Lander5Lawrence D'Oliveiro
24 Jun 24 ii i     i i      `* Re: More Fun Stuff From Lunar Lander4rbowman
16 Jun 24 ii i     i `- Re: More Funny Stuff From Python1Lawrence D'Oliveiro
14 Jun 24 ii i     `- Re: More Funny Stuff From Joel1candycanearter07
9 Jun 24 ii +- Re: More Funny Stuff From The Joke Python1rbowman
9 Jun 24 ii +- Re: More Funny Stuff From The Joke Python1Lawrence D'Oliveiro
14 Jun 24 ii `* Re: More Funny Stuff From The Joke Python4Stéphane CARPENTIER
8 Jun 24 i`* Re: More Funny Stuff From The Joke Python6DFS
8 Jun 24 +* Re: More Funny Stuff From The Joke Python6vallor
8 Jun 24 +* Re: More Funny Stuff From Feeb's Life3DFS
8 Jun 24 +* Re: More Funny Stuff From The Joke Python2rbowman
9 Jun 24 +- Re: More Funny Stuff From The Joke Python1Lawrence D'Oliveiro
14 Jun 24 `- Re: More Funny Stuff From The Joke Python1Stéphane CARPENTIER

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal