Deutsch   English   Français   Italiano  
<8734pc2iba.fsf@nosuchdomain.example.com>

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

Path: ...!feeds.phibee-telecom.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Whaddaya think?
Date: Sun, 16 Jun 2024 23:29:29 -0700
Organization: None to speak of
Lines: 79
Message-ID: <8734pc2iba.fsf@nosuchdomain.example.com>
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>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Mon, 17 Jun 2024 08:29:29 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a34467dd4a85130cbe55ab07dc34e4ee";
	logging-data="553297"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18jgVjQPy0PndgasezK8krk"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:HAHwNOrCGsWlK5yjdcYxhAzGaug=
	sha1:Bs8kQwuCbm2pbUBBdWqOyZ6YVqs=
Bytes: 3993

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 17.06.2024 02:06, Keith Thompson wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> [...]
>>> #include <stdlib.h>
>>> #include <stdio.h>
>>>
>>> void main (int argc, char * argv[])
>>> {
>>>     int chunk = 10;
>>>     int bufsize = chunk+1;
>>>     char * buf = malloc(bufsize);
>>>     char * anchor = buf;
>>>     while (fgets(buf, chunk+1, stdin) != NULL)
>>>         if (realloc(anchor, bufsize += chunk) != NULL)
>>>             buf += chunk;
>>>     puts(anchor);
>>> }
>> 
>> realloc() can return the pointer you pass to it if there's enough room
>> in the existing location.  (Or it can relocate the buffer even if there
>> is enough room.)
>> 
>> But if realloc() moves the buffer (copying the existing data to it), it
>> returns a pointer to the new location and invalidates the old one.  You
>> discard the new pointer, only comparing it to NULL.
>> 
>> Perhaps you assumed that realloc() always expands the buffer in place.
>> It doesn't.
>
> No, I didn't assume that. I just missed that 'anchor' will get lost.
> Thanks!
>
>> 
>> If the above program worked for you, I suspect that either realloc()
>> never relocated the buffer, or you continued using the original buffer
>> (and beyond) after realloc() invalidated it. [...]
>
> Yes, that was certainly the case. (I did no thorough test with large
> data sets, just a simple ad hoc test.)
>
>
> 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.

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.

```
$ ( echo one ; echo two ) | ./janis
one

$
```

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */