Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!amdcad!sun!pitstop!sundc!seismo!uunet!ingr!dan From: dan@ingr.com (Dan Webb) Newsgroups: comp.sys.sgi Subject: Re: -lmalloc Message-ID: <3774@ingr.com> Date: 3 Feb 89 15:53:55 GMT References: <295@jupiter.iotek.UUCP> Organization: Intergraph Corp. Huntsville, Al Lines: 60 in article <295@jupiter.iotek.UUCP>, mike@iotek.UUCP (Mike Thompson) says: > > In article <3724@ingr.com> dan@ingr.com (Dan Webb) says: >>I probably don't have to convince too many people of this, but a request >>for zero bytes is by no means an invalid request. I think -lmalloc should >>be fixed. > Well I'm one of those few who need convincing, I think that > treating a request for 0 memory from a memory managment system as > an error is if not correct is at least not incorrect, to give you Ok, I guess there are a few people that need to be convinced. Philisophically speaking: When you're talking about an amount of things, zero is not a special case. I can have two apples, or I can have zero apples. If I have zero apples, I'd better not try to eat one because I might core dump :-). Asking for zero bytes is just as valid. I have written lots of code that deals with dynamically changing data, such as fonts (yes, these fonts are quite dynamic) and Mac-like scrolling lists. It is quite valid for a font to initially have no characters or a list to have no entries. I must therefore deal with the case of allocating a zero-length amount. Later, I need to realloc or free the data. If malloc(0) returns NULL, I'm forced to special-case it everywhere I allocate/reallocate/free it. My code looks like this in many places: ptr = (Thing *) malloc (count * sizeof (Thing)); ... ptr = (Thing *) realloc (ptr, count * sizeof (Thing)); ... free (ptr); The value of count may very well be zero. The bottom line is that if I want to special-case zero for speed, I will. But I don't want malloc to force me to do it. > ...to give you > a valid pointer would in effect be allocating at least a byte, > after all that address cannot be allocated to any other request > can it? This depends on the implementation. The normal SysV malloc uses a linked list of blocks, so it isn't a problem. The only thing that would exist for the zero-length block is the block header. > On the other hand a program that does not check for > errors on system calls, and does no checking for null pointers is > definitely what I would call not written in the best of styles if > not down right buggy! (especially with the problems of null > de-referencing effecting portability over the past few years) I totally agree. But if I checked for an error an error from -lmalloc, it would tell me that allocating zero entries in my list is invalid -- when it's really not! To solve the problem with -lmalloc, I wrote wrappers around malloc and realloc. They turn any zero request into a request for one byte. - Dan Webb