内存相关崩溃:Cocos2d游戏中的三维数组
Memory Related Crash : 3 Dimensional Array in Cocos2d Game
这是代码:
.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;
}
相关文章:
- 三维数组中的C/C++DWORD到BYTE和BYTE到DWORD的转换
- std::将三维数组复制到三维向量中
- 显示结构的三维数组
- 使用unique_ptr来管理三维数组
- 无法访问三维数组中的特定位置
- 内存相关崩溃:Cocos2d游戏中的三维数组
- 如何在三维数组中使用std::map来存储/检索具有特定值的键
- 使用参数在C++中实例化三维数组
- 三维数组作为数组的向量
- 用C++将三维数组转换为二维数组
- C/C++ 三维数组如何在内存中存储,以及遍历它的最快方法是什么
- 三维数组c++中的访问冲突(使用malloc)
- C++中三维数组的解定位
- 使用指针访问三维数组的元素时
- 如何在opencv中为三维数组赋值
- C++中的三维数组
- 删除三维数组时出现0xfeeefee2错误
- C 复制三维数组
- 循环访问 CUDA 中的三维数组以对它们的元素求和
- 使用单个连续内存块为三维数组编制索引