Deutsch   English   Français   Italiano  
<v4oovd$hu7p$1@dont-email.me>

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

Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Whaddaya think?
Date: Mon, 17 Jun 2024 09:35:07 +0200
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <v4oovd$hu7p$1@dont-email.me>
References: <666ded36$0$958$882e4bbb@reader.netnews.com>
 <20240616015649.000051a0@yahoo.com> <v4lm16$3s87h$4@dont-email.me>
 <v4lmso$3sl7n$1@dont-email.me> <v4lr0m$3tbpj$1@dont-email.me>
 <8734pd4g3s.fsf@nosuchdomain.example.com> <v4ltuj$3trj2$1@dont-email.me>
 <87ed8w3025.fsf@nosuchdomain.example.com> <v4oinp$gpg0$1@dont-email.me>
 <8734pc2iba.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 17 Jun 2024 09:35:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ce947dcd9ea98bb3504b70234e0c429c";
	logging-data="588025"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+HHl2IOINyM5YMzs1JKRsb"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
 Thunderbird/45.8.0
Cancel-Lock: sha1:FlUsAGpNDcNVl1wIdrTtT7KIVBA=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <8734pc2iba.fsf@nosuchdomain.example.com>
Bytes: 2919

On 17.06.2024 08:29, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>>[...]
>>
>> Elsethread I suggested to merge the malloc() with the realloc() call.
>> The resulting code would be simpler (and might address that problem).
>>
>>     int chunk = 10;
>>     int bufsize = 1;
>>     char *  anchor = NULL;
>>     while ((anchor = realloc (anchor, bufsize += chunk)) != NULL  &&
>>             fgets (anchor+bufsize-chunk-1, chunk+1, stdin) != NULL)
>>         ;
>>     puts (anchor);
>>
>>
>> Do you see the exposed problem (or any other issues) here, too?
> 
> If stdin is empty, you never store anything in the buffer and
> puts(anchor) has undefined behavior because there might be a terminating
> '\0'.  If the first realloc() fails, anchor is a null pointer and again
> puts(anchor) has undefined behavior.
> 
> If nothing goes wrong, puts() adds an extra newline to the output.

Yes, the purpose of puts() was to show whether the function that I
wanted to check works properly on a long line of data.

> 
> That's all that jumped out at me looking at the code, but did you test
> it with multi-line input?  When I tried it it printed only the first
> line of input (followed by that extra newline).
> 
> I'm still not entirely sure what the code is supposed to do.

I just wanted the realloc-append logic codified and verified. (As
a possible building block to create a function for the purpose of
the original task outlined by the OP.)

Janis