Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 04 Aug 2012 19:48:59 -0500 Message-ID: <501DC2F9.22FD@mindspring.com> Date: Sat, 04 Aug 2012 20:48:57 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Trying to figure out how to dynamically re-allocate strings while concatenating References: <501D50E6.532@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 106 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.223.89 X-Trace: sv3-zWAnnu0J/XKVVBznEfZesrhi1RURXP+bhbi2zPcpfUkEuvFbS04jJZL71sSbsI8/JJek27w0U9JJEU7!A68RwyhfH96rI8BheJ3sn7gwViLewBK/GvDDg5ZYEWLJCkmq2Ii6ZkKqCrfipWVd0i2jtnTo5zi8!P2MHS/0C9w== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4296 Xref: csiph.com comp.lang.c:24233 Keith Thompson wrote: > > pete writes: > [...] > > One thing that nobody else has mentioned, is that it is good > > programming practice to assign the return value of realloc to a > > temporary pointer first, > > and only to assign that value to your working > > pointer if the temporay pointer has been tested > > and found to be not a null pointer. > > > > Otherwise you lose the value of the pointer to the allocated memory, > > which can cause a memory leak. > [...] > > That is, of course, very good advice. > > On the other hand, whether it matters depends on what you intend to do > once you've detected an allocation failure. > > char *buf = malloc(100); > if (buf == NULL) { > /* malloc failed */ > } > > char *temp = realloc(buf, 200); > if (temp == NULL) { > /* realloc failed */ > } > > So how can you recover from a failure to allocate memory? You could > just try again, but it will probably just fail again. You could fall > back to a less memory-intensive, but perhaps slower, algorithm, but > I don't think I've ever seen code in the real world that does that. I managed to write some code which does something else in that event, but in real life I don't think that that part of the code will ever be used. I have (like so many of us do) a get_line function. It reallocates when the buffer isn't big enough to hold two bytes more than the number of bytes which have been read from a line in a text file. The two bytes extra, is so that if reallocation fails, then there is enough buffer to hold all of the characters which have been read so far, plus space for a null byte to make a string. The first number of bytes that I try to allocate is the lowest number which is greater than or equal to how many I need and which is also one less than a power of two. If the first reallocation fails, then I keep using binary search to reallocate. If the reallocation fails completely, then it gets difficult to describe in English. (min) is how much memory I need. (*n) is how much memory I have. (p) is a temporary pointer. (lineptr) is my working pointer. The reallocation code looks something like this: if (++min > *n) { void *p; size_t max; size_t next; for (next = min ^ min - 1; min > next; ++next) { next *= 2; } do { max = next; next = (max - min) / 2 + min; p = realloc(*lineptr, max); } while (p == NULL && max != min); if (p == NULL) { /* ** Handle it. */ } else { *lineptr = p; *n = max; } } The function is here: http://www.mindspring.com/~pfilandr/C/lists_and_files/file_lib.c The description of the function is here: http://www.mindspring.com/~pfilandr/C/lists_and_files/file_lib.h -- pete