通过sbrk()动态分配的空间,在被回收后,修改其内容可能不会coredump,但是有风险;通过mmap()动态分配的空间,在被回收后,修改其内容会造成coredump。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
#define P1_SIZE (512)
#define P2_SIZE (512*1024)
void *p1, *p2;
p1 = malloc(P1_SIZE);
memset(p1,P1_SIZE,0);
printf("p1=%x\n",(unsigned)p1);
free(p1);
memset(p1,P1_SIZE,1);
p2 = malloc(P2_SIZE);
memset(p2,P2_SIZE,0);
printf("p2=%x\n",(unsigned)p2);
free(p2);
memset(p2,P2_SIZE,1);
return 0;
}