Dynamic Storage Allocator Pros and Cons

Richard Harter rh at smds.UUCP
Sat Nov 17 18:08:17 AEST 1990


In article <9052 at cognos.UUCP>, jimp at cognos.UUCP (Jim Patterson) writes:
: In article <241 at smds.UUCP> rh at smds.UUCP (Richard Harter) writes:

: >(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/retsp aren't really equivalent to malloc/free.

	Quite true, but then I never claimed it was.  However if you
	want that behaviour, change the test.

: 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.

I suspect that in this kind of case one is often implicitly
protected by a for loop that executes zero times.  One is also protected
most of the time by the fact that the allocated space (of what size??)
will hold variables of all types except structs.  This protects you
against algorithms that explicitly set location 0 of an array outside
of a loop.

: 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.

Probably differences in coding techniques.  My experience is that 
0 size requests are very rare and, when they happen, there was a real
live bug (usually a typo) in the code.  However I check things like 0
length as a matter of course (ingrained habit, not virtue).  
-- 
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398 
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb.  This sentence short.  This signature done.



More information about the Comp.lang.c mailing list