bph@buengc.BU.EDU (Blair P. Houghton) (05/09/90)
In article <19461@duke.cs.duke.edu> drh@cs.duke.edu writes: >3. If you are always freeing and mallocing a chunk of memory the same > size, then why not just allocate a static array and not mess with free() > and malloc() at all. Such will accomplish exactly the same thing > as free() followed by malloc(), but with considerably fewer machine > cycles. Example: > > char *squirl() > { > static char test[100]; > return test; > } Using this function the array test[] will be allocated exactly once during the execution of the program, and the same value will be returned no matter how many times squirl() is called. If you want two blocks of 100 char's simultaneously, this won't work. Its only redeeming feature is that it hides the allocation and address-taking of the array test[] from the caller, and doesn't make one suspect that it's an array type. That is, if you consider that a redeeming feature, which it might be if the allocation is couched in #ifdef's and other implementation-dependencies. I prefer to think of it as an obfuscation. Use malloc() and free(). --Blair "Use Texaco System3. Just five tanks is all we ask. Just five measly 11-18 gallon tanks. Just five tanks of $1.20-$1.40 gasoline. Just $108-252 over the next 5-8 weeks..."
john@stat.tamu.edu (John S. Price) (05/09/90)
In article <5804@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: >In article <19461@duke.cs.duke.edu> drh@cs.duke.edu writes: >>[stuff deleted] >> char *squirl() >> { >> static char test[100]; >> return test; >> } > >Using this function the array test[] will be allocated >exactly once during the execution of the program, and the >same value will be returned no matter how many times >squirl() is called. If you want two blocks of 100 char's >simultaneously, this won't work. >[stuff deleted] > > --Blair The original won't work if you want two blocks of 100 char's simultaneously, for it frees the old block before it allocates the new block. So, the solution that was suggested is identical to the first to the caller. A pointer is returned in both cases, a pointer to 100 chars. I am still curious as to why someone would want to allocate 100 chars with malloc, and free that space before you allocate some more. If all you want is space for 100 chars, just use a static array like in the above example. I am assuming that this space is re-assigned each time the function is called... -------------------------------------------------------------------------- John Price | It infuriates me to be wrong john@stat.tamu.edu | when I know I'm right.... --------------------------------------------------------------------------
peter@ficc.ferranti.com (Peter da Silva) (05/10/90)
In article <5804@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: > Using this function the array test[] will be allocated > exactly once during the execution of the program, and the > same value will be returned no matter how many times > squirl() is called. If you want two blocks of 100 char's > simultaneously, this won't work. Unfortunately, neither will the original proposal: char *foo() { static char *buffer = NULL; free(buffer); ... buffer = malloc(...); return buffer; } Unless you *like* dereferencing freed pointers, that is. What it *will* do is let you deal with large objects without chewing up lots of BSS. -- `-_-' Peter da Silva. +1 713 274 5180. <peter@ficc.ferranti.com> 'U` Have you hugged your wolf today? <peter@sugar.hackercorp.com> @FIN Commercial solicitation *is* accepted by email to this address.