INFO: printf() and 512-Byte I/O Buffer

Last reviewed: September 30, 1997
Article ID: Q44725
The information in this article applies to:
  • The C Run-time (CRT) included with: - Microsoft Visual C++ for Windows, 16-bit edition, version 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0,

         4.1, 4.2, 5.0
    

SUMMARY

The function printf() takes up 512 bytes from the default heap in 16-bit applications and 4096 bytes in 32-bit applications.

MORE INFORMATION

The standard I/O buffer gets allocated only when needed. This gives the program an extra 512 bytes of near-heap space in small and medium memory models or an extra 512 bytes of far-heap space in compact and large models. Therefore, when the printf() function is used for the first time, a 512-byte buffer is reserved for I/O, and the subsequent calls to printf() use that buffer.

The sample program below demonstrates this behavior. When compiled and executed with current 16-bit versions, the number printed after printf() executes is 512 bytes more than the number printed before the printf() executes.

When this sample program is compiled for release under 32-bit versions and executed in Windows NT, the output displays 4096 bytes. For debug compile, the number of bytes shown is 4144, the extra 48 bytes are used to debug related information. Note that this program displays 0 bytes for 32-bit versions on Windows 95 because _heapwalk is not supported in Windows 95.

Sample Code

   /* Compile options needed: none
   */

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


   unsigned long heapdump (void)
   {
      struct _heapinfo hinfo;
      int heapstatus;
      unsigned long memUsed = 0;

      hinfo._pentry = NULL;
      while ((heapstatus = _heapwalk(&hinfo)) == _HEAPOK)
      {
         if (hinfo._useflag == _USEDENTRY)
            memUsed += hinfo._size;
      }
      switch (heapstatus)
      {
        case _HEAPEMPTY:
            printf("OK - empty heap\n");
            break;
        case _HEAPEND:
            printf("OK - end of heap\n");
            break;
        case _HEAPBADPTR:
            printf("ERROR - bad pointer\n");
            break;
        case _HEAPBADBEGIN:
            printf("ERROR - bad start\n");
            break;
        case _HEAPBADNODE:
            printf("ERROR - bad node\n");
            break;
      }

      return memUsed;
   }


   void main (void)
   {
      unsigned long memUsed, memTemp;

      memTemp = memUsed = heapdump();

      printf("Pre-printf memory used was %lu bytes.\n", memUsed);
      memUsed = heapdump();
      printf("Post-printf memory used is %lu bytes.\n", memUsed);
      printf(
        "The difference in memory used between printf's is %lu bytes.\n",
        memUsed - memTemp);
   }


Additional query words: heapwalk printf
Keywords : CRTIss
Version : WINDOWS: 1.52; WINDOWS NT: 2.0, 2.1, 2.2 , 4.0, 4.1, 4.2, 5.0
Platform : NT WINDOWS
Issue type : kbinfo


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.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.