From: Damian Yerrick Newsgroups: comp.os.msdos.djgpp Subject: Re: Unstandard mallocation needed Organization: Pin Eight Software http://pineight.8m.com/ Message-ID: References: <384735753 DOT 961926689108 DOT JavaMail DOT root AT web443-mc DOT mail DOT com> X-Newsreader: Forte Agent 1.7/32.534 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 56 X-Trace: +4QiDE1ser1OhVmzw/yhBGwMWR59zr/Q7x9r8QIJ8sQcpQC0xYhljdCotcMF5sAeFrUj2pvo78Fe!GwX/bcBVfuPCbMTxz8SaBpAVQO3f7831B9zSc10NAfe5XdRyWdIZ5sV3CUY9oph3yFUg8vAZWghn!9K2y X-Complaints-To: abuse AT gte DOT net X-Abuse-Info: Please be sure to forward a copy of ALL headers X-Abuse-Info: Otherwise we will be unable to process your complaint properly NNTP-Posting-Date: Sun, 25 Jun 2000 15:50:01 GMT Distribution: world Date: Sun, 25 Jun 2000 15:50:01 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Sun, 25 Jun 2000 13:24:51 +0300 (IDT), Eli Zaretskii wrote: > >On Sun, 25 Jun 2000, Martin Nilsson wrote: > >> Q: So can I query the malloc package to find out how big >> an allocated block is? >> >> A: Unfortunately, there is no standard or portable way. >> >> but I would like to do it anyway, with DJGPP. None of the >> memory allocation routines listed in info.exe seem to fit. >> Does somebody know how to do it? It would be extra nice if >> the solution was somwhat portable to the GNU GCC compiler. > >As the C FAQ tells you: there's no portable solution. You can hack the >sources of the DJGPP's implementation of malloc (it's in djlsr203.zip), >if you really need it, but that would be not portable to any other >library. > >Many people want these features because they want to detect memory leaks >in their programs. If that's your reason as well, there are special >packages out there for this purpose; the DJGPP FAQ mentions several of >them in section 22.4. > >> p = realloc(p, 50); /* saving the contents of bytes >> 0..49, freeing bytes 50..99 */ >> >> thus doing a nifty "slicing" operation without having to >> copy anything. It would be nice to also be able to do the "opposite" >> operation, i e freeing the *first* n bytes, >> like this: >> >> byte *p = malloc(100); >> . . . >> >> p = free_the_first_n_bytes(p, 35); >> /* p now is old p + 35; >> bytes 0..34 are freed */ > >You can't, not in the current implementation of malloc, anyway. >It stores internal info about the allocated block in a few bytes >immediately preceding and following the allocated block. I'd just use a memmove() then a realloc() until somebody (hint hint) submits a patch that allows this. However, it probably will be able only to chop off multiples of 8 bytes from the start of a block. -- Damian Yerrick "I refuse to listen to those who refuse to listen to reason." See the whole sig: http://www.rose-hulman.edu/~yerricde/sig.html This is McAfee VirusScan. Add these two lines to your signature to prevent the spread of signature viruses. http://www.mcafee.com/