我正在尝试将 C 样式的 Vertex3D 结构数组保存到 NSData 对象,并在重新加载应用程序时将它们取回:
NSData *vert = [NSData dataWithBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)];
然后保存此数据并尝试将其读回我的 c 数组中:
vertices = malloc(sizeof(Vertex3D) * NUM_OF_VERTICES);
[vert getBytes:&vertices length:(sizeof(Vertex3D) * NUM_OF_VERTICES)];
上面的结果是“EXC_BAD_ACCESS”,然后是:
malloc: *** error for object 0x48423c0: pointer being freed was not allocated
我对编程很陌生,所以可能有一些我不知道的基本内存管理原则。我已经验证加载的 NSData 与保存的相同,但很明显从 c-array 到 NSData(和返回)的转换不是我想要的。