0

The following program:

/* gcc -O1 -g3 -std=c99 -Wall -Wextra -m32 t.c -o t.exe */

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

int main(int argc, char* argv[]) {

    static const unsigned int MALLOC_SIZE = 0x80000000;
    int ret = -1;

    unsigned char * ptr = malloc(MALLOC_SIZE);
    if(ptr) {
        printf("Allocation succeeded\n");
        printf("%02X … %02X\n", ptr[0], ptr[MALLOC_SIZE - 1]);
        free(ptr);
        ret = 0;
    } else {
        printf("Allocation succeeded\n");
        ret = 1;
    }
    return ret;
}

$ lipo -info t.exe
Non-fat file: t.exe is architecture: i386

Produces the following output:

$ ./t.exe
t.exe(19449) malloc: *** mmap(size=2147483648) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Allocation succeeded

If the allocation failed, why did I get back a non-null pointer?

4

1 回答 1

4

You are printing "allocation succeded" in either case, which could be a bit misleading.

于 2013-07-01T02:55:22.893 回答