运行这段代码应该会导致程序中断增加大约malloc_counts * _SC_PAGESIZE
而不是我每次都得到固定的程序中断,所以这是为什么。malloc 应该调用brk
或sbrk
将其本身向上舍入传递到下一页(需要一些额外的工作)。那么发生了什么?
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
int main(){
const long malloc_counts = 10;
printf("PAGE SIZE: %ld\n", sysconf(_SC_PAGESIZE));
void* allocated_pool[malloc_counts];
for(int counter=0; counter < malloc_counts; counter++)
{
printf("program brk: %p\n",sbrk(0));
allocated_pool[counter] = malloc(127*4096);
}
}