Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Memory allocation problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Memory allocation problem



Hi,

I'm having some problems with the program that follows. The error I get is

teste(1392) malloc: *** vm_allocate(size=1069056) failed (error code=3)
teste(1392) malloc: *** error: can't allocate region
teste(1392) malloc: *** set a breakpoint in szone_error to debug
Not enough memory

------------<CODE>------------
#include <stdio.h>
#include <malloc/malloc.h>

#define SIZE1 650
#define SIZE2 34

int main()
{
    int i,j,k,l;

    int ****matrix;

    matrix = (int ****) malloc(SIZE1*sizeof(int));
    if (matrix == NULL) {
        printf("Not enough memory\n");
        exit(1);
    }
    printf("First loop\n");

    for(i=0; i < 650; i++) {
        matrix[i] = (int ***) malloc(SIZE2*sizeof(int));
        if (matrix[i] == NULL) {
        printf("Not enough memory\n");
        exit(1);
    }
    }
    printf("Second loop\n");

    for(i=0; i < 650; i++) {
    for (j=0; j < 34; j++) {
        matrix[i][j] = (int **) malloc(SIZE2*sizeof(int));
        if (matrix[i][j] == NULL) {
        printf("Not enough memory\n");
        exit(1);
        }
    }
    }
    printf("Third loop\n");

    for(i=0; i < 650; i++) {
    for (j=0; j < 34; j++) {
        for (k=0; k < 650; k++) {
        matrix[i][j][k] = (int *)malloc(SIZE2*sizeof(int));
        if (matrix[i][j][k] == NULL) {
            printf("Not enough memory\n");
            exit(1);
        }
        }
    }
    }
    printf("Fourth loop\n");

    return 0;
}
------------<END OF CODE>------------

The problem happens in the fourth loop. I'm using an iBook G4 1GHz, 1.12RAM (Mac OS X 10.4.2). The same program runs perfectly on a linux machine (kernel version 2.6.8-2-686). So, I think it is some sort of limitation that I'm not aware of.

I tried to set a breakpoint in function "szone_error", as recommeded, but gdb doesn't stop there (I compiled it with the -g3 option).

Any hints are welcomed! :-)

P.S.: I'm actually a Java programmer trying to port one of my programs to Objective-C. So, I may be doing something very, very stupid! Please, be patient! :-)

P.P.S.: I'm really not sure if my problem should be posted here. If there is a more suitable list, please let me know.

Cheers,
Ernesto.

--
Ernesto Trajano de Lima
<http://www.cin.ufpe.br/~etl>
======================
.:::. Mac OS X Powered .:::.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-userlevel mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.