Deutsch   English   Français   Italiano  
<v5ecf5$bqc$1@rasp.pasdenom.info>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news-out.netnews.com!postmaster.netnews.com!us14.netnews.com!not-for-mail
X-Trace: DXC=j5Y>[]f@?\EhQRfiUUWA<IHWonT5<]0TMQ;nb^V>PUfF=AnO\FUBY[@nF54O@^\1?DESf2LZ4kKlILQ^D7JJN18AankHQ>:kCVBOT>gi^X=X_ACGm4YP8f00J
X-Complaints-To: support@blocknews.net
Date: Thu, 13 Jun 2024 10:38:12 -0400
MIME-Version: 1.0
User-Agent: Betterbird (Windows)
Subject: Re: "undefined behavior"?
Newsgroups: comp.lang.c
References: <666a095a$0$952$882e4bbb@reader.netnews.com>
 <v4d4hm$1rjc5$1@dont-email.me> <8734ph7qe5.fsf@nosuchdomain.example.com>
 <666a226d$0$951$882e4bbb@reader.netnews.com> <v4erpi$29e2g$2@dont-email.me>
Content-Language: en-US
From: DFS <nospam@dfs.com>
In-Reply-To: <v4erpi$29e2g$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 81
Message-ID: <666b0451$0$953$882e4bbb@reader.netnews.com>
NNTP-Posting-Host: 127.0.0.1
X-Trace: 1718289489 reader.netnews.com 953 127.0.0.1:56283
Bytes: 4362

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.

I like C for it's blazing speed.  Very addicting.  And it's much more 
challenging/frustrating than python.

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.

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.