Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: The return value of realloc(p,0) Date: Fri, 01 Sep 2023 00:14:49 -0700 Organization: A noiseless patient Spider Lines: 36 Message-ID: <86wmxas6ty.fsf@linuxsc.com> References: <86edjju668.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="e13a49a448cc252f76540c1a5198a3b9"; logging-data="3881751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/4P3BVM+AepN3c8jdRIo1lkuz5xJ/xtf4=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:NLQeEX2ldIPinbtq1LvwcE3M8H8= sha1:64VsRHK8T0w0gSvPLpPX/NldY6E= Xref: csiph.com comp.lang.c:173498 Vir Campestris writes: > On 31/08/2023 06:33, Tim Rentsch wrote: > >> There are reasons for realloc() to zize 0 to be a special case. >> Specifically, because the pointer value returned doesn't have to >> be dereferencable, it can point to parts of the address space >> that are otherwise unusable. The overhead for such "zero-byte >> blocks" is very low, just a smidge over one bit. The usuable >> memory previously allocated can be reclaimed and used in its >> entirely for subsequent allocations. If running in a 64-bit >> address space, vast amounts of otherwise unusable areas can >> be used for these "special objects", having very little impact >> on memory usable for actual objects. > > Tim, I'm confused by a little bit of that: > > "just a smidge over one bit" > > I can see how you might point them all to a single place that can't be > read or written, and have safety in case anyone tries to use them. > > I can see that you might have a section of memory with a series of > bytes, each one reserved for these empty allocations. It might be > inaccessible, but will still use address space. > > But how do you get anything smaller than a byte, but non-zero? I've > only ever used one CPU with addressing smaller than a byte. I was tempted to make a joke here, but I couldn't see how to turn out something funny. What I meant was just over one bit /per zero-byte block/. So if there were 20,000 zero-byte blocks, they could be managed with just a little more than 2500 bytes of allocated memory. Sorry for the confusion.