Memory allocation problem

bearophile bearophileHUGS at lycos.com
Sun Aug 9 05:49:25 PDT 2009


Frank Benoit:

>Is it the malloc that fails (returning null) or the handling of the block?<

It's the filling of the memory block. malloc by itself doesn't crash.

---------------------

Robert Fraser:
> Have you tried with DMC?<

I have done a test with MDC too now.

// D code
import std.c.stdlib: malloc;
void main() {
    int i;
    int n = (1800 * 1000 * 1000) / double.sizeof;
    double* p = cast(double*)malloc(n * double.sizeof);
    for (i = 0; i < n; i++)
        p[i] = 1.0;
}


// C code
#include "stdlib.h"
int main() {
    int i;
    int n = (1800 * 1000 * 1000) / sizeof(double);
    double *p = (double*)malloc(n * sizeof(double));
    for (i = 0; i < n; i++)
        p[i] = 1.0;
    return 0;
}

With DMD that D code produces the:
Error: Access Violation

That C code compiled with GCC (just gcc test.c -o test), works and just has to move some memory on disk.

That C code compiled with DMC works at first and then crashes badly at runtime after a moment.

Bye,
bearophile



More information about the Digitalmars-d mailing list