gpt4 book ai didi

c++ - 内存相关崩溃: 3 Dimensional Array in Cocos2d Game

转载 作者:行者123 更新时间:2023-11-30 16:19:05 24 4
gpt4 key购买 nike

代码如下:

.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];
}
}

游戏因最后一个函数中的不同索引而随机崩溃。与 malloc 有关的问题...如何修复?如果您知道请帮助我。

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

提前致谢。

最佳答案

问题终于解决了。在上面的代码中,为 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;
}

关于c++ - 内存相关崩溃: 3 Dimensional Array in Cocos2d Game,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55711125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com