#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<pthread.h>
#define BLOCKSIZE 1024*1024
// #define BLOCKSIZE 4096
int main (int argc, char *argv[])
{
    void *myblock = NULL;
    int count = 0;
    while (1)
    {
        myblock =  malloc(BLOCKSIZE);
        if (!myblock){
            puts("error"); break;
        }
        memset(myblock,1, BLOCKSIZE);
        count++;
    }
    printf("Currently allocated %d \n",count);
    printf("end");
    exit(0);
}
当 BLOCKSIZE 为 1024*1024 时。一切正常。malloc 返回 NULL,循环中断。程序打印文本并退出。
当 BLOCKSIZE 为 4096 时,Malloc 永远不会返回 NULL 程序崩溃。=> 内存不足,被内核杀死。为什么?