KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q68943: calloc() Can Return a Pointer to a Zero Length Block of Memory

Article: Q68943
Product(s): See article
Version(s): 6.00 6.00a | 6.00 6.00a
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | docerr | mspl13_c
Last Modified: 6-FEB-1991

There is a documentation error in the description of the calloc()
function in the "Microsoft C Run-Time Library Reference" manual and in
the online help that shipped with Microsoft C versions 6.00 and 6.00a.

Page 136 of the "Microsoft C Run-Time Library Reference" states, "The
_fcalloc and _ncalloc functions return NULL if there is insufficient
memory available or if num or size is 0." Actually, they will return
NULL only if there is insufficient memory for the request. If one of
the arguments is of size zero, calloc(), _ncalloc(), and _fcalloc()
will return a pointer to a block of size 0 bytes.

Sample Code
-----------

#include <stdio.h>
#include <malloc.h>

void _near *foo;
void _far  *goo;

void main(void)
{
   foo = _ncalloc(0,1);
   goo = _fcalloc(0,1);

   if (NULL == foo) printf("Foo is null.\n");

   else printf("Foo points to a block %d bytes long.\n",_nmsize(foo));

   if (NULL == goo) printf("Goo is null.\n");

   else printf("Goo points to a block %d bytes long.\n",_fmsize(goo));
}

When this program is executed, the output is as follows:

   Foo points to a block 0 bytes long.
   Goo points to a block 0 bytes long.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.