最近在方法抽取時,使用了結構體來來支持自定義數值傳值,本來一切都順順利利,但在處理特定情況,需要在結構體中定義一個傳遞一個指向數組的指針時,出了問題,指針在用時,拿不到值。
結構體中使用指針
初始使用代碼如下
typedef struct {
int *p;
int count;
} ZLLAverge;
ZLLAverge defaultAverge() {
int a[] = {1, 2, 3};
ZLLAverge averge;
averge.count = sizeof(a) / sizeof(int);
averge.p = a;
return averge;
}
- (void)test1:(ZLLAverge)averge {
for (int i = 0; i < averge.count; i++) {
printf("%i ", averge.p[i]);
printf("------------\n");
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self test1:defaultAverge()];
}
//結果如下
0 -1074336348 3 ------------
后面又嘗試了兩種情況
- (ZLLAverge)ctreateAverge{
int a[] = {4, 5, 6};
ZLLAverge averge;
averge.count = sizeof(a) / sizeof(int);
averge.p = a;
return averge;
}
- (void)viewDidLoad {
[super viewDidLoad];
ZLLAverge averge = [self ctreateAverge];
[self test1:averge];
ZLLAverge averge1;
int a[] = {7, 8, 9};;
averge1.count = sizeof(a) / sizeof(int);
averge1.p = a;
[self test1:averge1];
}
//結果如下
0 -1074639484 3 ------------
7 8 9 ------------
嘗試到這兒,回想起以前學習的東西,串起來就明白了。
每個函數(方法)在執行時會在棧區分配一塊內存空間,在函數內部創建的局部變量就分配在這塊內存空間內,當函數執行完時這塊內存空間會被回收。但結構體是值傳遞,所以不受影響,指針的值還在,而上面的指針指向的內存地址中的數據,在函數返回時已被回收,取出的數據就不是我們想要的了。
正確的使用是使用malloc函數,在堆區開辟一塊內存空間,同時使用memcpy函數將在棧區的指針指向的內存地址中的數據拷貝到堆區,這樣在函數返回時就不會被回收,影響我們的使用了。
ZLLAverge defaultAverge() {
int a[] = {1, 2, 3};
ZLLAverge averge;
averge.count = sizeof(a) / sizeof(int);
size_t memory_size = averge.count * sizeof(int);
int *newA = (int *)malloc(memory_size);
memcpy(newA, a, memory_size);
averge.p = newA;
return averge;
}
//
1 2 3 ------------
PS:注意 使用malloc函數開辟的內存空間,要自己釋放,不然會有內存泄露。
優雅的使用
上面我們已經解決了問題,但如果我們的代碼是提供給別人用的呢,如何給提示信息,注意上面提到的問題呢。或者如果要在別的結構體中使用有指針的結構體呢。有很多方法,但怎樣做才簡單而優雅呢,在定義這種類型的結構體時提供創建和銷毀方法,代碼如下:
//再加一些注釋就更完美了
typedef struct {
int *p;
int count;
} ZLLAverge;
static void avergeRelease(ZLLAverge averge) {
if (averge.p) {
free(averge.p);
}
}
static ZLLAverge avergeCreate(int a[], int count) {
size_t memory_size = count * sizeof(int);
int *newA = (int *)malloc(memory_size);
memcpy(newA, a, memory_size);
ZLLAverge averge;
averge.p = newA;
averge.count = count;
return averge;
}
注意順序,銷毀方法在前,創建方法在后。這種可以最大程度的提醒使用者正確的使用方法。
如果要在別的結構體中使用有指針的結構體
typedef struct {
ZLLAverge averge;
int topMargin;
} ZLLAttiStringValue;
static void attiStringValueRelease(ZLLAttiStringValue stringValue) {
avergeRelease(stringValue.averge);
}
static ZLLAttiStringValue attiStringValueCreate(int a[], int count) {
ZLLAttiStringValue stringValue;
if (a) {
stringValue.averge = avergeCreate(a, count);
}
return stringValue;
}
也要提供創建和銷毀方法。
小結
- 注意不能在創建函數內部求count,只能通過外面傳進來,因為函數調用過程,sizeof(指針)返回的是指針的字節,而不是數組的長度。
- 不能在結構體中使用OC對象類型,OC對象類型本質是個指針,是在堆上開辟內存空間了的,而在及結構體銷毀方法里,我們是沒法調用OC對象的銷毀方法的
我們把oc對象轉為cf對象來看下引用計數
@interface ZLLFont : UIFont
@end
@implementation ZLLFont
- (void)dealloc {
NSLog(@"......");
}
@end
typedef struct {
ZLLAverge averge;
int topMargin;
ZLLFont *font;
} ZLLAttiStringValue;
static void attiStringValueRelease(ZLLAttiStringValue stringValue) {
avergeRelease(stringValue.averge);
if (stringValue.font) {
CFTypeRef objCF = (__bridge_retained CFTypeRef)stringValue.font;
NSLog(@"release --- 引用計數 --- %ld", CFGetRetainCount(objCF));
CFRelease(objCF);
NSLog(@"release --- 引用計數 --- %ld", CFGetRetainCount(objCF));
stringValue.font = nil;
}
}
static ZLLAttiStringValue attiStringValueCreate(int a[], int count) {
ZLLAttiStringValue stringValue;
if (a) {
stringValue.averge = avergeCreate(a, count);
}
stringValue.font = [ZLLFont systemFontOfSize:12];
return stringValue;
}
- (void)viewDidLoad {
[super viewDidLoad];
ZLLAttiStringValue stringValue = attiStringValueCreate(nil, 0);
attiStringValueRelease(stringValue);
}
//
release --- 引用計數 --- 4
release --- 引用計數 --- 3
嘗試了多次調用CFRelease也不行。