From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lucio De Re To: 9fans@cse.psu.edu Subject: Re: [9fans] manual suggestions and upas/fs bug Message-ID: <20020917111727.U6063@cackle.proxima.alt.za> References: <3D8642ED.6BE57EE6@null.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii In-Reply-To: <3D8642ED.6BE57EE6@null.net>; from Douglas A. Gwyn on Tue, Sep 17, 2002 at 08:47:45AM +0000 Date: Tue, 17 Sep 2002 11:17:27 +0200 Topicbox-Message-UUID: ed04c43a-eaca-11e9-9e20-41e7f4b1d025 On Tue, Sep 17, 2002 at 08:47:45AM +0000, Douglas A. Gwyn wrote: > > > if(n==0){ > > > free(p); > > > p = malloc(n); > > > } else > > > p = realloc(p, n); > > p = realloc(p,n) is almost always a mistake. If the allocation > fails, the previous pointer value in p gets replaced by a null > pointer value, so you lose the handle to the data that did not > get extended (or, rarely, shrunk). In other words, if (n==0) panic ("zero size"); p = realloc(p, n); is closer to the intent here. Better still char *p0 = realloc(p, n); if (p0 == 0) panic ("realloc failed"); p = p0; would be a lot saner. I forget the semantics, but realloc(p, n) need not return p even if the reallocation succeeds. What I don't know is what happens to the space pointed to by p, I assume it's released. ++L