【发布时间】:2011-05-17 11:22:13
【问题描述】:
我正在为 iPhone 编写一个应用程序,它使用链表来跟踪一组点。我在其他 C 程序中一次又一次成功地使用了链表,但我从未在 iPhone 上使用过链表,而且我的“freeList”函数崩溃了。
pointNode * temp;
pointTrain = lPoints->next;
while (pointTrain) {
temp = pointTrain->next;
free(pointTrain);
pointTrain = temp;
}
这就是我释放列表的方式,'lPoints' 是头部,并且总是在运行时分配。此代码在添加更多节点之前执行,但即使“pointTrain”为 NULL,它似乎仍会执行,这应该使其跳过 while 循环。可惜循环还在执行,程序在free(pointTrain)上崩溃了
有什么我没看到的吗?
点是在循环中添加的:
pointTrain->x = onPx / trueWidth;
pointTrain->y = onPx % trueWidth;
pointTrain->next = (pointNode*)malloc(sizeof(pointNode));
pointTrain = pointTrain->next;
【问题讨论】:
-
什么是pointNode?是 C-Struct、Objective-C 类还是 C++ 结构/类?
-
为什么不使用 NSMutableArray 之类的?
-
Obj-C 是 C 的严格超集。如果您用 C 编写了链表,它们在 Objective-C 中的工作方式完全相同。
标签: iphone memory-management linked-list