Program Crash - probably a memory retain misuse - objective-c

I have an array of Objective-C objects that is to be sorted on a field of the object. I use a simple selection sort since the array is small. The sort works and the correct data is in the array, but after adding about 3 or 4 objects to the array and resorting each time it crashes.
Any help would be appreciated. The error is EXC_BAD_ACCESS Thanks in advance. The code follows:
TopTenDataClass *temp1 = [[TopTenDataClass alloc] init];
TopTenDataClass *temp2 = [[TopTenDataClass alloc] init];
for (int i = 0; i < [topTenDataArray count]; i++)
{
int minIndex = i;
for (int j = i; j < [topTenDataArray count]; j++)
{
temp1 = [topTenDataArray objectAtIndex:minIndex];
temp2 = [topTenDataArray objectAtIndex:j];
if ( temp2.timeInSeconds < temp1.timeInSeconds)
minIndex = j;
}
[topTenDataArray exchangeObjectAtIndex:minIndex withObjectAtIndex:i];
}
[temp2 release]; [temp1 release];

The issue is that inside of your loop you are changing the values of temp1 and temp2. When you release them after the loop completes, you are not releasing the objects that you created before the loop started. Instead you are attempting to release some other object that you did not alloc/init (in this part of the code). Probably that is what is causing your crash.
I'd suggest trying something like:
TopTenDataClass *temp1 = nil;
TopTenDataClass *temp2 = nil;
for (int i = 0; i < [topTenDataArray count]; i++)
{
int minIndex = i;
for (int j = i; j < [topTenDataArray count]; j++)
{
temp1 = [topTenDataArray objectAtIndex:minIndex];
temp2 = [topTenDataArray objectAtIndex:j];
if ( temp2.timeInSeconds < temp1.timeInSeconds)
minIndex = j;
}
[topTenDataArray exchangeObjectAtIndex:minIndex withObjectAtIndex:i];
}
You don't need to assign a new object instance to temp1 and temp2 before the loop, because you just overwrite their values inside your loop.
Do also note that if topTenDataArray has only a single element in it your code will call [topTenDataArray exchangeObjectAtIndex:0 withObjectAtIndex:0], which may also be a problem (the API docs don't say that you can't exchange an object with itself, but they don't say that you can, either).

Related

Error with using local variable to access array index

For some reason this occurs every time i try to access a specific index from the array by specifying a variable int in the index in a forloop. When i do i get a Thread 1 Error, but if i use a variable that has not be declared in the forloop itself than it seem to work fine.
Code:
for(int i =0 ; i<= [array count]; i++) {
NSNumber *convert = [array objectAtIndex:i]; //results in error
NSLog(#"%i", [convert intValue]);
}
Problem is you are trying to access array beyond its capacity. Array starts with 0 index and goes upto array.count - 1. That said, please try with below code and you should be good:
for (int i = 0 ; i <= array.count - 1 ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}
Another variation could be:
for (int i = 0 ; i < array.count ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}

Array - find how many times an object repeats consecutively

My array objects are as follows:
10,10,10
20,23,14
10,10,10
10,10,10
10,10,10
32,23,42
32,23,42
10,10,10
32,23,23
32,23,23
How can I go through this array and find out how many times the same object repeats sequentially, then add a , and the number of times it repeats?
Then save a new array with objects like:
10,10,10,1
20,23,14,1
10,10,10,3
32,23,42,2
10,10,10,1
32,23,23,2
Any help would be appreciated.
Thanks!
Try this:
NSMutableArray *outArray = [[NSMutableArray alloc] init];
for (NSUInteger j = 0; j < [theArray count]; j++) {
id object = [theArray objectAtIndex:j];
NSUInteger repeats = 1;
while (j + 1 < [theArray count] && [[theArray objectAtIndex:j + 1] isEqual:object]) {
j++;
repeats++;
}
[outArray addObject:object];
[outArray addObject:[NSNumber numberWithUnsignedInteger:repeats]];
}
return outArray;
This can also be done in place if the input array is mutable. I leave that as an exercise for the reader.
Break up every three integers into its own array (make sure they are strings).
Then iterate through each one of those arrays, and input into an NSMutableDictionary, the key is the string (your number), the value is a counter (if seen once, add 1, etc...)
Keep a pointer to the highest key (if newCount > highestCountPointer, then highestCountPointer=newCount)
At the end of that iteration, add the number that the highestCountPoints to to the end of the array.
I'm not an Objective C programmer, so please pardon any language gaffes. Something like the following should do the job:
NSMutableArray *result = [[NSMutableArray alloc] init];
id pending = nil;
NSUInteger count = 0;
for (NSUInteger i = 0; i < [theArray count]; i++) {
id object = [theArray objectAtIndex:i];
if ([object isEqual:pending]) {
count++;
} else {
if (pending != nil) {
[result addObject:[NSString stringWithFormat:#"%#,%d", pending, count]];
}
pending = object;
count = 1;
}
}
if (pending != nil) {
[result addObject:[NSString stringWithFormat:#"%#,%d", pending, count]];
}
Just run "uniq -c" from command line :)

Converting Objective C to C Matrix Manipulation

Okay, so I had working code written in all objective c (yes, I know that objc is technically just C. But i mean I had it written with messages and stuff. I only have a java background and do not know much about plain old C) but it ran incredibly slow. So I wrote out (what i thought) was the same code, but now this set of loops produces different values (for only some of the numbers) and I cannot, for the life of me, figure out whats different. What I am doing is looping 10 times and doing 1 multiplication and 1 addition between matricies. I'm hoping someone with more background with the two languages can pick out the part of code that I transcribed incorrectly. I did not change anything beforehand for any of the arrays (those were hardcoded in and uneffected) so A1, A2, etc have the same values in both parts of code.
Current code in C:
for (int m = 0; m < 10; m++) {
//Do matrix multiplication between A1 and A2. Store in temporary B1
for( int i = 0; i < 13; i++ )
for( int j = 0; j < 43; j++ ) {
double tempTotal = 0;
for( int k = 0; k < 43; k++){
tempTotal = tempTotal + A1[i][k] * A2[k][j];
}
B1[i][j] = tempTotal;
}
//Assign B1 data back into A1 after the multiplication is finished
for(int i = 0; i < 13; i++)
for(int j = 0; j<43; j++)
A1[i][j] = B1[i][j];
//Add C1 and A1. Store into C1.
for (int l = 0; l < 13; l++)
for (int n = 0; n < 43; n++)
C1[l][n] = C1[l][n] + A1[l][n];
}//end m for loop
This was the old Obj c code:
for (int m = 0; m < 10; m++) {
//multiply A1 and A2. Store into A1
A1 = [LCA_Computation multiply:A1 withArray:A2]; //LCA_Computation is the name of the .m class file in which this all happens.
//Add C1 and A1. Store into C1
for (int i = 0; i < 13; i++)
for (int j = 0; j < 43; j++)
[[C1 objectAtIndex:i] replaceObjectAtIndex:j withObject:[NSNumber numberWithDouble: [[[C1 objectAtIndex: i] objectAtIndex: j] doubleValue] + [[[A1 objectAtIndex: i] objectAtIndex: j] doubleValue]]];
}//end m for loop
//multiply method
+ (NSMutableArray*)multiply:(NSMutableArray*)a1 withArray:(NSMutableArray*)a2
{
int a1_rowNum = [a1 count];
int a2_rowNum = [a2 count];
int a2_colNum = [[a2 objectAtIndex:0] count];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:a1_rowNum];
for (int i = 0; i < a1_rowNum; i++) {
NSMutableArray *tempRow = [NSMutableArray arrayWithCapacity:a2_colNum];
for (int j = 0; j < a2_colNum; j++) {
double tempTotal = 0;
for (int k = 0; k < a2_rowNum; k++) {
double temp1 = [[[a1 objectAtIndex:i] objectAtIndex:k] doubleValue];
double temp2 = [[[a2 objectAtIndex:k] objectAtIndex:j] doubleValue];
tempTotal += temp1 * temp2;
}
//the String format is intentional. I convert them all to strings later. I just put it in the method here where as it is done later in the C code
[tempRow addObject:[NSString stringWithFormat:#"%f",tempTotal]];
}
[result addObject:tempRow];
}
return result;
}
This issue had to do with prior memory management issues causing 0's to be used in some calculations.

Any way to add a property to an object obtained from [array objectAtIndex:]?

I apologize in advance if my questions seems unclear.
Effectively, I wanted to set a tag value to an array of objects using a for loop.
for (int j = 0; j < [array count]; j++) {
[array objectAtIndex:j].tag = j;
}
Is there something similar i can do?
Another way of doing it:
int counter = 0;
for (UIView* view in array)
view.tag = counter++;
Assuming all the objects in array is a subclass of UIView:
for (int j = 0; j < [array count]; j++] {
UIView *tmpView = (UIView *)[array objectAtIndex:j];
tmpView.tag = j;
}
objectAtIndex returns a pointer to the object, not a new copy of the object. So anything changed via the returned pointer will be reflected in original array object.

Randomly select x amount of items in a "list"

I would like to select x amount of items randomly from a "list" in objective C store them in an other "list" (each item can only be selected one) , I'm talking about lists because I'm coming from Python. What would be the best way to store a list of strings in Objective C ?
cheers,
You should use NSMutableArray class for changeable arrays or NSArray for non-changeable ones.
UPDATE: a piece of code for selecting a number of items from an array randomly:
NSMutableArray *sourceArray = [NSMutableArray array];
NSMutableArray *newArray = [NSMutableArray array];
int sourceCount = 10;
//fill sourceArray with some elements
for(int i = 0; i < sourceCount; i++) {
[sourceArray addObject:[NSString stringWithFormat:#"Element %d", i+1]];
}
//and the magic begins here :)
int newArrayCount = 5;
NSMutableIndexSet *randomIndexes = [NSMutableIndexSet indexSet]; //to trace new random indexes
for (int i = 0; i < newArrayCount; i++) {
int newRandomIndex = arc4random() % sourceCount;
int j = 0; //use j in order to not rich infinite cycle
//tracing that all new indeces are unique
while ([randomIndexes containsIndex:newRandomIndex] || j >= newArrayCount) {
newRandomIndex = arc4random() % sourceCount;
j++;
}
if (j >= newArrayCount) {
break;
}
[randomIndexes addIndex:newRandomIndex];
[newArray addObject:[sourceArray objectAtIndex:newRandomIndex]];
}
NSLog(#"OLD: %#", sourceArray);
NSLog(#"NEW: %#", newArray);