0

これを実行すると、数千を超える cnt でメモリを割り当て続けても問題ないようです。理由がわかりません。ある時点で NULL を取得する必要があるのではないですか? ありがとう!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int main(void)
{
    long C = pow(10, 9);
    int cnt = 0;
    int conversion = 8 * 1024 * 1024;
    int *p;
    while (1)
    {
        p = (int *)malloc(C * sizeof(int));
        if (p != NULL)
            cnt++;
        else break;
        if (cnt % 10 == 0)
            printf("number of successful malloc is %d with %ld Mb\n", cnt, cnt * C / conversion);
    }

    return 0;
}
4

2 に答える 2

0

仮想メモリを割り当てています。64 ビット OS では、仮想メモリをほぼ無制限に利用できます。

于 2013-05-17T02:48:36.137 に答える