[EMAIL PROTECTED] on 05/02/2000 05:51:57 PM
>On Tue, May 02, 2000 at 05:10:40PM -0400, Noel L Yap wrote:
>> Code that checks for NULL as an error condition is, IMHO, incorrect (unless,
of
>> course, the code never expects malloc to be called with a zero argument).
>> Correct code would look like:
>> p = malloc(n);
>> if((p == NULL) ^ (n == 0))
>> {
>> /* error */
>> }
>>
>> Also, allocating 1-byte chunks could potentially fragment memory and slow
down
>> the process.
>
>Probably not. Most malloc()'s today will see that you only want
>one byte and up the request to the closest power of two that
>is word aligned with the machine that we are on.
It doesn't really matter exactly how much memory was allocated or where it's
aligned. The fact is that any allocation causes memory fragmentation.
Allocating memory when none was asked for causes more fragmentation than is
necessary.
>>
>> Currently, most (if not all) calls to free() check to see if the argument is
>> NULL anyway (ie if(p != NULL) free(p)). The only thing that's gained by not
>> wrapping free() is one less function call for each free(). If you're worried
>> about that extra function call, the wrapper could be made a macro.
>
>would this be a good thing(tm) for someone to go and do?
I agree with Larry. It'll be a lot of work to change the code with very little
gain. However, future code should use such a function.
Noel
This communication is for informational purposes only. It is not intended as
an offer or solicitation for the purchase or sale of any financial instrument
or as an official confirmation of any transaction, unless specifically agreed
otherwise. All market prices, data and other information are not warranted as
to completeness or accuracy and is subject to change without notice. Any
comments or statements made herein do not necessarily reflect those of
J.P. Morgan & Co. Incorporated, its subsidiaries and affiliates.