Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!mips!synoptics!unix!husc6!m2c!jjmhome!smds!rh From: rh@smds.UUCP (Richard Harter) Newsgroups: comp.lang.c Subject: Re: Dynamic Storage Allocator Pros and Cons Summary: You like zero size, use it Message-ID: <245@smds.UUCP> Date: 17 Nov 90 07:08:17 GMT References: <241@smds.UUCP> <9052@cognos.UUCP> Organization: SMDS Inc., Concord, MA Lines: 47 In article <9052@cognos.UUCP>, jimp@cognos.UUCP (Jim Patterson) writes: : In article <241@smds.UUCP> rh@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.