I'm trying to implement an n x n multidimensional array of ints in Objective-C, and because using NSMutableArray seems to be too much of an overhead I decided to implement it using only C's malloc and free. My question is, is the code below correct (no memory leaks) under ARC Objective-C?
我正在尝试在Objective-C中实现一个n x n多维数组的int,因为使用NSMutableArray似乎是一个太大的开销我决定只使用C的malloc和free来实现它。我的问题是,ARC Objective-C下的代码是否正确(没有内存泄漏)?
@interface TwoDimensionalArray
{
int **array;
int size;
}
@end
@implementation TwoDimensionalArray
- (id)initWithSize: (int)s
{
if(self = [super init])
{
size = s;
array = malloc(sizeof(int*) * size);
for(int i = 0; i < size; i++)
{
array[i] = malloc(sizeof(int) * size);
for (int j = 0; j < size; j++)
{
array[i][j] = 0;
}
}
}
return self
}
- (id)init
{
return [self initWithSize:1];
}
- (void)dealloc
{
for(int i = 0; i < size; i++)
{
free(array[i]);
array[i] = nil;
}
free(array);
array = nil;
}
@end
If this is not correct, or if you think there is a definitely better way to do it in Objective-C without doing mallocs, please tell me. Thanks.
如果这不正确,或者你认为在没有做mallocs的情况下在Objective-C中有更好的方法,请告诉我。谢谢。
1 个解决方案
#1
6
ARC implements automatic memory management for Objective-C objects and blocks, but does not automate malloc/free. (References: Clang/ARC documentation: General, ARC Release Notes: FAQ.)
ARC为Objective-C对象和块实现自动内存管理,但不自动化malloc / free。 (参考文献:Clang / ARC文档:General,ARC发行说明:FAQ。)
So your question is unrelated to ARC: Everything you malloc()
must be free()
'd (and dealloc
is good place to do so). From the first look your code looks OK.
所以你的问题与ARC无关:你malloc()的所有内容都必须是free()'d(并且dealloc是这样做的好地方)。从第一眼看,您的代码看起来没问题。
(Small remarks: Zeroing the array pointers in dealloc
is not necessary, but NULL
would be the appropriate pointer value to assign, nil
is meant for Objective-C objects.)
(小备注:无需在dealloc中清零数组指针,但NULL将是要分配的适当指针值,nil用于Objective-C对象。)
I do not know how much overhead using Objective-C collections such as NSMutableArray
would cause, that should be tested (as already said in the comments) by profiling.
我不知道使用诸如NSMutableArray之类的Objective-C集合会产生多少开销,应该通过分析来测试(如评论中已经说过的)。
#1
6
ARC implements automatic memory management for Objective-C objects and blocks, but does not automate malloc/free. (References: Clang/ARC documentation: General, ARC Release Notes: FAQ.)
ARC为Objective-C对象和块实现自动内存管理,但不自动化malloc / free。 (参考文献:Clang / ARC文档:General,ARC发行说明:FAQ。)
So your question is unrelated to ARC: Everything you malloc()
must be free()
'd (and dealloc
is good place to do so). From the first look your code looks OK.
所以你的问题与ARC无关:你malloc()的所有内容都必须是free()'d(并且dealloc是这样做的好地方)。从第一眼看,您的代码看起来没问题。
(Small remarks: Zeroing the array pointers in dealloc
is not necessary, but NULL
would be the appropriate pointer value to assign, nil
is meant for Objective-C objects.)
(小备注:无需在dealloc中清零数组指针,但NULL将是要分配的适当指针值,nil用于Objective-C对象。)
I do not know how much overhead using Objective-C collections such as NSMutableArray
would cause, that should be tested (as already said in the comments) by profiling.
我不知道使用诸如NSMutableArray之类的Objective-C集合会产生多少开销,应该通过分析来测试(如评论中已经说过的)。