Microsoft KB Archive/44725

= INFO: printf and 512-Byte I/O Buffer =

Article ID: 44725

Article Last Modified on 12/11/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul>

 Microsoft Visual C++ 6.0 Service Pack 5</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q44725

<div class="summary_section">

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

<div class="moreinformation_section">

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, Windows 98, or Windows Millennium Edition (Me) because _heapwalk is not supported in Windows 95, Windows 98, and Windows Me.

Sample Code
/* Compile options needed: none


 * 1) include <stdio.h>
 * 2) 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(&quot;OK - empty heap\n&quot;); break; case _HEAPEND: printf(&quot;OK - end of heap\n&quot;); break; case _HEAPBADPTR: printf(&quot;ERROR - bad pointer\n&quot;); break; case _HEAPBADBEGIN: printf(&quot;ERROR - bad start\n&quot;); break; case _HEAPBADNODE: printf(&quot;ERROR - bad node\n&quot;); break; }

return memUsed; }

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

memTemp = memUsed = heapdump;

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

Additional query words: heapwalk printf

Keywords: kbinfo kbcrt KB44725

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.