内存相关崩溃:Cocos2d游戏中的三维数组

Memory Related Crash : 3 Dimensional Array in Cocos2d Game

本文关键字:三维 数组 游戏 Cocos2d 崩溃 内存      更新时间:2023-10-16

这是代码:

.h 声明

@interface LevelManager : NSObject{
}
@property int ***construtorDeMundo;

初始化和 malloc

-(id)init {
    self = [super init];
    if(self != nil){
        construtorDeMundo = (int***) malloc ( NUMFASES * sizeof(int *));
        for (int i = 0; i < NUMFASES ; i++) {
            construtorDeMundo[i] = (int**) malloc (MAX_PONTOS_CRITICOS * sizeof(int));
        }
        for (int i = 0; i < NUMFASES; i++)
            for (int j = 0; j < MAX_PONTOS_CRITICOS; j++) {
                construtorDeMundo[i][j] = (int*) malloc (PROPRIEDADES * sizeof(int));
                for (int k = 0; k < PROPRIEDADES ; k++)
                    construtorDeMundo[i][j][k] = 0;
            }
        [self pegaInformacoes];
    }
    return self;
}

访问代码:

 for (int j = 1; j < [elements count]; j++) {
            if(j <= PROPRIEDADES+1){
                NSString *valor = (NSString *)[elements objectAtIndex:j];
                construtorDeMundo[fase][i][j-1] = [((NSNumber*)valor) intValue];
            }
        }

游戏在最后一个函数中随机崩溃不同的索引。与马洛克有关的东西...如何解决?如果你知道,请帮助我。

对不起,这个游戏代码不是英文的...不是我写的。

提前谢谢。

终于修复了问题。在上面的代码中,为 sizeof(( 提供了错误的值。

以下是更新的代码:

-(id)init {
    self = [super init];
    if(self != nil){
        construtorDeMundo = (int***) malloc ( (NUMFASES) * sizeof(*construtorDeMundo));
        for (int i=0; i < NUMFASES; ++i)
            construtorDeMundo[i] = NULL;
        for (int i = 0; i < NUMFASES ; ++i) {
            construtorDeMundo[i] = (int**) malloc (MAX_PONTOS_CRITICOS * sizeof(*construtorDeMundo[i]));
        }
        for (int i=0; i < NUMFASES; ++i)
            for (int j=0; j < MAX_PONTOS_CRITICOS; ++j)
                construtorDeMundo[i][j] = NULL;
        for (int i = 0; i < NUMFASES; ++i)
            for (int j = 0; j < MAX_PONTOS_CRITICOS; ++j) {
                construtorDeMundo[i][j] = (int*) malloc ((PROPRIEDADES) * sizeof(*construtorDeMundo[i][j]));
                for (int k = 0; k < PROPRIEDADES ; k++)
                    construtorDeMundo[i][j][k] = 0;
            }

        [self pegaInformacoes];
    }
    return self;
}