Path: utzoo!yunexus!maccs!cs3b3aj From: cs3b3aj@maccs.McMaster.CA (Stephen M. Dunn) Newsgroups: comp.lang.c Subject: Re: What if I don't free() all my dynamically allocated memory? Summary: it might even be o.k. on a PC Keywords: so it may be portable Message-ID: <2367@maccs.McMaster.CA> Date: 8 Apr 89 02:44:50 GMT Article-I.D.: maccs.2367 References: <2580@ssc-vax.UUCP> Reply-To: cs3b3aj@maccs.UUCP (Stephen M. Dunn) Organization: McMaster U., Hamilton, Ont., Can. Lines: 42 In article <2580@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: > Anyway, I'm wondering if it's ok for me to just leave the freeing > out altogether. Unix will free all of my resources for me anyway > right? Is there any problem with doing this? > > Of course, I realize this would cause serious problems if ever > ported to a lowly PC, but I don't care ;-) Actually, this may be ok on a PC, too. I don't know how the typical C compiler on a PC handles memory allocation, but I would suspect it's similar to Turbo Pascal: when the program starts, it grabs all available memory and then dishes it out to the program using its own memory manager. Why? Because MS-DOS will only allow you to allocate memory in 16-byte chunks, while your program may try to allocate (say) three bytes at a time, leading to a colossal waste of memory. By grabbing all memory and handling allocation itself, an executable can use memory much more space-efficiently (although this approach is incompatible with multi-tasking operating systems). When the program terminates, the memory-management routines built into your executable by the compiler simply return all the memory to DOS, so your program _must_ free its allocated memory only if it will run out of memory if it doesn't. Neat, huh? Oh, and to overcome the problem with multi-tasking, the compiler will most likely allow you to set an option limiting the maximum amount of memory it grabs. For more complicated programs, however, the analysis required to estimate a realistic figure for this becomes astronomical. As I said, I've never looked to see if compilers other than Turbo Pascal (3 and 4, and probably 5, too) do this, I would think it likely that they do. Anyway, just thought you might like to know the "lowly" PC might even be able to handle your code. Regards -- ====================================================================== ! Stephen M. Dunn, cs3b3aj@maccs.McMaster.CA ! DISCLAIMER: ! ! This space left unintentionally blank - vi ! I'm only an undergrad ! ======================================================================