简体   繁体   English

C没有可用的程序内存:不安全地调用malloc错误

[英]C No memory available to program: unsafe to call malloc error

I am trying to create all of the matrices from size 10x10 to 1000x1000 around 980 I start running out of memory even though the program is clearly not using all 4GB of my system. 我正在尝试创建所有大小从10x10到​​1000x1000的矩阵(大约980),尽管程序显然没有使用系统的全部4GB内存,但我开始用尽内存。 My code looks like: 我的代码如下:

    double **a3;
    double **result;
    a3 = malloc(k * sizeof(double *));
    for(i = 0; i < k; i++)
    {
        a3[i] = malloc(sizeof(double));
    }
    result = malloc(k * sizeof(double *));
    for(i = 0; i < k; i++)
    {
        result[i] = malloc(k * sizeof(double));
    }
    for(i = 0; i < k; i++)
    {
        for( j = 0; j < k; j++)
        {
            a3[i][j] = 0;
            result[i][j] = 0;
        }
    }
    ...
    for(i = 0; i < k; i++)
{
    free(a3[i]);
    free(result[i]);
}
free(a3);
free(result);

I am not sure why this doesn't work (it does work if I use int arrays but I need double arrays) 我不确定为什么这行不通(如果我使用int数组但我需要double数组,它确实可以工作)

I am using Mac OSX 64bit 我正在使用Mac OSX 64位

I'm not sure how 'clear' you actually are about not using all the memory that's available to your program . 我不确定您实际上对不使用程序可用的所有内存的了解程度 If I'm figuring rightly, you end up allocating a total of 333,833,185 doubles. 如果我没弄错的话,您最终总共分配了333,833,185个双打。 At eight bytes each, that's over 2.5GB. 每个八个字节,超过2.5GB。

Not sure how much is available to your C compiler's memory management facilities... why not write a simple program that discovers what the biggest amount of memory you could malloc() is? 不确定C编译器的内存管理工具有多少可用...为什么不编写一个简单的程序来发现malloc()可能占用的最大内存量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM