Deutsch English Français Italiano |
<v4oinp$gpg0$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 07:48:39 +0200 Organization: A noiseless patient Spider Lines: 57 Message-ID: <v4oinp$gpg0$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> MIME-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 17 Jun 2024 07:48:42 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ce947dcd9ea98bb3504b70234e0c429c"; logging-data="550400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196JRbcPW7SxBYdn4CGtFjX" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:BlUvCd7846ReLqrz5kQsh/l+EmU= X-Enigmail-Draft-Status: N1110 In-Reply-To: <87ed8w3025.fsf@nosuchdomain.example.com> Bytes: 3163 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? Janis