TC> I strongly recommend against "mass
TC> allocation/deallocation" because IMHO, you
TC> can run into lots of trouble with memory leaks and
TC> holes. I like to deallocate
Either:
a) You've learned alot in your absence from the C_ECHO there, or
b) You've learned alot since you got back to the C_ECHO, or
c) You're a natural. ;-)
TC> the memory right after I no longer need it. Yes, it is
TC> best to use a free()
TC> for each malloc(). That is about the only* way I know
TC> how to do it right now
TC> (because I'm still not that great at C to try anything else). I also
don't
Only way I know how, too...
TC> like to have a lot of allocated memory "laying around"
TC> unused when the program
TC> is running. I think it could slow the program down
TC> some, and/or decrease the
TC> amount of space you have ready for other things.
Well, there are a lot of considerations here. Extraneously allocated memory
in 32-bit systems (Win32, OS/2, DOS32) can be swapped out of memory to disk.
This disk swapping (and the swapping back in if you merely free it) can slow
down the system. Or, if you are no longer using a small block of memory in a
4k page, it can slow things down whether free'd or not. There are a lot of
things that have to be taken into account when you worry about micro
optimizations like this - and this is one of those things that a compiler
cannot help you with automatically! At this point, I would refer you to the
design documentation to your compiler, OS, and hardware.
However, if you have a 32-byte chunk of memory you no longer need, and need
to allocate <32 bytes, you're best off to free the original chunk and
allocate the new chunk to "save space." However, no one guarantees that
malloc will use the just-freed memory... (I think DOS compilers usually do,
mostly because of the 640k limit) An interesting tidbit is how virtual
memory systems (Windows, OS/2, etc.) can make more memory available easily
(in page-sized chunks) if you've recently freed up a previous page. However,
when a page is 4k, and OS/2 has 512 meg per process, and Win32 has at least
2GB per process, that's a minor issue. :-)
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|