#import "YSArray.h" #include #include @implementation YSArray -(id) init { self = [super init]; array = NULL; return self; } -(void) addObject: (id) theObject { //printf("%s\n", "YSArray: addObject"); if (array == NULL) { //printf("%s\n", "before malloc"); array = malloc(sizeof(id)); if (array == NULL) { //printf("%s\n", "addObject: malloc failed."); return; } //printf("%s\n", "after malloc"); } else { //printf("%s\n", "before realloc"); array = realloc(array, count * sizeof(id)); if (array == NULL) { //printf("%s\n", "addObject: realloc failed."); return; } } array[count++] = theObject; //printf("%s\n", "YSArray: object added."); } -(void) removeObject: (unsigned int) index { if (count == 0 || count == 1) { array[0] = NULL; return; } if (index >= count) { return; } if (index == count - 1) { array[count - 1] = NULL; } //hope count > index; memcpy(&array[index], &array[index + 1], (count - index) * sizeof(id)); } @end