我有下面的代码在我的应用程序malloc内存到一个struct对象。这个方法在代码中被调用了几次,每次都为一个新的Vector3D对象错配内存。
我想做的是捕获指向malloced ShapeClass对象(C结构)的指针,以便我可以稍后在dealloc方法中释放每个对象。
谁能告诉我如何将指针添加到数组中?
谢谢。
vectorCount = [floatArray.array count] / 3;
ShapeClass *vectorArray = malloc(sizeof(Vector3D) * vectorCount);
for (int i=0; i<vectorCount; i++) {
vectorArray[i].x = [[floatArray.array objectAtIndex:(i*3)] floatValue];
vectorArray[i].y = [[floatArray.array objectAtIndex:(i*3)+1] floatValue];
vectorArray[i].z = [[floatArray.array objectAtIndex:(i*3)+2] floatValue];
}
return vectorArray;
// I WANT TO INSERT SOMETHING HERE TO CAPTURE THE POINTER TO vectorArray - E.G ADD IT TO AN ARRAY ?
}
}
return NULL;
}
使用[NSValue valueWithPointer:]
存储在容器类中
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
SomeObject *object = [[SomeObject alloc] init];
//Store the pointer to object
[dictionary setValue:[NSValue valueWithPointer:object] forKey:@"object"];