Path: utzoo!attcan!uunet!ginosko!rex!ukma!rutgers!dptg!att!cbnewsl!dfp From: dfp@cbnewsl.ATT.COM (david.f.prosser) Newsgroups: comp.std.c Subject: Re: Shrinking with realloc Message-ID: <1431@cbnewsl.ATT.COM> Date: 10 Aug 89 15:35:35 GMT References: <26328@shemp.CS.UCLA.EDU> Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser) Organization: AT&T Bell Laboratories Lines: 21 In article <26328@shemp.CS.UCLA.EDU> signup@CS.UCLA.EDU writes: >If realloc is used to shrink a chunk of allocated storage, is it guaranteed >by the standard that the storage won't be moved? I couldn't find any positive >or negative statement about the matter. > >I.e., after > char *p, *q; > q = p = (char *) malloc(10); > (void) realloc(p,5); >is it valid to dereference q? Short answer: "no". Long answer: Having such a requirement on realloc would overly constrain its implementation, quite possibly causing wasted memory. This is because the fancier malloc/realloc/free implementations have special pools for certain sized (typically small) blocks, and crossing such a size boundary would necessitate copying the data. Also, copying of a small amount of data is not too costly when the overall space usage "wins". Dave Prosser ...not an official X3J11 answer...