Dynamic Storage Allocator Pros and Cons

Jim Patterson jimp at cognos.UUCP
Fri Nov 16 06:43:56 AEST 1990


In article <241 at smds.UUCP> rh at smds.UUCP (Richard Harter) writes:
>Security and Error Checking:
>
>This is the reason for using G/R, if it matters to you.  Specifically
>the features are:
>
>(A)	All invalid size requests (zero, negative, too large) are trapped.
                                   ^^^^

Whether a 0 size request is invalid is a matter of interpretation.
Note that ANSI C specifically allows it; if you disallow it, then
getsp/remsp aren't really equivalent to malloc/free.

There are often times when a 0-byte request is legitimate. Usually this
comes up in logic that looks like this:

    Count the number of (some thing)
    Allocate memory for that many struct's to describe those things

(where it's legitimate for there to be 0 or more things).

As long as you only look at entries which you've counted and know are
there, the code is quite valid since it won't look at the pointer when
the count is 0.

We in fact have a wrapper around malloc/free that does much the same
things as yours, and it too disallows 0 size requests. However, in
just about every case I can recall where it complained of a 0-byte
request, the code was actually not broken, it just hadn't considered 0
to be a special case. So, this check isn't really a "good thing" IMHO.
-- 
Jim Patterson                              Cognos Incorporated
UUCP:uunet!mitel!cunews!cognos!jimp        P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
NOT a Jays fan (not even a fan)            Ottawa, Ont  K1G 3Z4



More information about the Comp.lang.c mailing list