如果我跑:
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
问题是为什么?堆腐败对吗?