TITLE: invoking new with a size of 0 PROBLEM: Bob Archer I am trying to write a replacement for the global new[] operator. In section r.5.3.3 of the C++ Programming Language 2e, Stroustrup writes: "This implies that an operator new() can be called with the argument zero. In this case, a pointer to an object is returned. Repeated such calls return pointers to distinct objects." This implies to me that new int[0] would be equivalent to new int[1] - space for a single object (an int in this case) will be allocated and a pointer returned to it. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 29 Oct 95 Given an array of "size" elements, a valid array index "i" is in the range "0 <= i < size". If size==0, there is no valid index. But you can take the address of the (non-existent) object at index "size". Thus, for example: int* pi = new int[size]; // size>=0 int* q = &pi[size]; // address is OK, but cannot dereference If you wind up requesting a zero-length array of type T, you get back a pointer which you can compare to NULL or to other pointers, but it does not point to a T; you cannot dereference the pointer. Similarly, if you call operator new with a zero size, you get back a pointer to a zero-size object, meaning that you cannot dereference it. Thus, operator new does not need to know the type of the object requested. Its primary obligation is to return storage suitably aligned for any object of the requested size. It also must not return a pointer to an object previously allocated but not yet deleted. It may allocate more storage than was requested, but the requestor cannot assume that extra storage was allocated. The simplest way to handle zero-size requests in operator new is to do this: void* operator new(size_t size) // or operator new[] { if( size == 0 ) size = 1; ... // remainder of function } The requestor of a zero-sized object cannot dereference the returned pointer, so the amount of storage allocated doesn't matter. RESPONSE: khan@xraylith.wisc.edu (Mumit Khan) As I understand it (cf: DWP sec 5.3.4 paragraph 12), new T[N] results in a call to operator new[] (sizeof(T) * N + x) where x is some implementation dependent value that might acct for array overhead (eg., in gcc, x = 8bytes). That means that you're not guaranteed to see 0 in your replacement operator new[] even when the element count is 0. RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 29 Oct 95 And your point is ... what? If a zero-element array is requested, the returned pointer cannot be dereferenced, so it doesn't matter how much storage is allocated. The only concern is to ensure that operator new returns a unique pointer if asked for zero bytes. If it is never asked for zero bytes, there is nothing to be concerned about. So if you ensure that a zero request is changed to a request for 1 byte, all should be well. My unstated assumptions were 1. We want to write a reasonably portable operator new. 2. Requests for zero elements or zero bytes are very rare. 3. We can afford to waste storage for such requests, since the total involved will be entirely negligible. Now, maybe you have the problem that you have many requests for zero- element arrays in your program, and that the allocated data is never deleted so that you are wasting too much memory. In that case, you could check for the zero-element allocation at the point of the new- expression, or you would need a system-specific solution in the event that operator new is passed a non-zero value when asked for zero elements.