了解堆检查,堆损坏

时间:2014-03-05 15:03:25

标签: c windows visual-studio-2010 visual-c++ heap-memory

如果我跑:

char *buffer;
buffer = (char *)malloc( 8 );
strcpy(buffer,"abcdefghijklm");
int  heapstatus = _heapchk();
switch( heapstatus )
{
case _HEAPOK:
  printf(" OK - heap is fine\n" );
  break;
 case _HEAPEMPTY:
  printf(" OK - heap is empty\n" );
  break;
  case _HEAPBADBEGIN:
  printf( "ERROR - bad start of heap\n" );
  break;
  case _HEAPBADNODE:
  printf( "ERROR - bad node in heap\n" );
  break;
 }

它显示:

 ERROR - bad node in heap

好。 但如果我跑:

char *a;
for (int i=0;i<1000000;i++) a = (char*)malloc(1); // Line added
char *buffer;
buffer = (char *)malloc( 8 );
strcpy(buffer,"abcdefghijklm");
int  heapstatus = _heapchk();
switch( heapstatus )
{
case _HEAPOK:
  printf(" OK - heap is fine\n" );
  break;
 case _HEAPEMPTY:
  printf(" OK - heap is empty\n" );
  break;
  case _HEAPBADBEGIN:
  printf( "ERROR - bad start of heap\n" );
  break;
  case _HEAPBADNODE:
  printf( "ERROR - bad node in heap\n" );
  break;
 }

它显示:

OK - heap is fine

问题是为什么?堆腐败对吗?

0 个答案:

没有答案