2011-12-15 2 views
1

아래 코드는 malloc 메모리가 struct 객체에있는 내 응용 프로그램에 있습니다. 이 메소드는 새 Vector3D 객체에 대한 메모리를 mallocing 할 때마다 코드에서 여러 번 호출됩니다.배열에 구조체에 포인터 저장? iPhone

내가 원하는 것은 malloc 된 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; 
} 

답변

1

[NSValue valueWithPointer:]을 사용하여 컨테이너 클래스에 저장하십시오.

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 
SomeObject *object = [[SomeObject alloc] init]; 

//Store the pointer to object 
[dictionary setValue:[NSValue valueWithPointer:object] forKey:@"object"]; 
관련 문제