我試圖做一個簡單的二進制添加應用程序。以下是我使用的兩種主要方法。你可以假設任何事情我不代碼聲明在頭文件中已經聲明:奇怪的「EXC_BAD_ACCESS」錯誤
-(IBAction)valuesChanged
{
if ((![input1.text isEqualToString:@""]) && (![input2.text isEqualToString:@""]))
{
if (bitRange.selectedSegmentIndex == 0) {flowLimit = 8;}
else if (bitRange.selectedSegmentIndex == 1) {flowLimit = 16;}
else {flowLimit = 32;}
NSMutableArray* bin1 = [[NSMutableArray alloc] initWithCapacity:32];
NSMutableArray* bin2 = [[NSMutableArray alloc] initWithCapacity:32];
NSMutableArray* resBin = [[NSMutableArray alloc] initWithCapacity:32];
input1Decimal = [input1.text intValue];
input2Decimal = [input2.text intValue];
int decimalDummy = input1Decimal;
while (decimalDummy > 0)
{
if (decimalDummy == 1)
{
[bin1 addObject:[NSNumber numberWithInt:1]];
decimalDummy--;
}
else
{
[bin1 addObject:[NSNumber numberWithInt:(decimalDummy % 2)]];
decimalDummy = decimalDummy/2;
}
}
decimalDummy = input2Decimal;
while (decimalDummy > 0)
{
if (decimalDummy == 1)
{
[bin2 addObject:[NSNumber numberWithInt:1]];
decimalDummy--;
}
else
{
[bin2 addObject:[NSNumber numberWithInt:(decimalDummy % 2)]];
decimalDummy = decimalDummy/2;
}
}
while ([bin1 count] < flowLimit) {[bin1 addObject:[NSNumber numberWithInt:0]];}
while ([bin2 count] < flowLimit) {[bin2 addObject:[NSNumber numberWithInt:0]];}
resBin = [self addBinary:bin1 toBinary:bin2];
NSString* string1 = @"";
NSString* string2 = @"";
NSString* string3 = @"";
for (int i = 0; i < flowLimit; i++)
{
string1 = [[NSString stringWithFormat:@"%@",[bin1 objectAtIndex:i]] stringByAppendingString:string1];
string2 = [[NSString stringWithFormat:@"%@",[bin2 objectAtIndex:i]] stringByAppendingString:string2];
string3 = [[NSString stringWithFormat:@"%@",[resBin objectAtIndex:i]] stringByAppendingString:string3];
}
[output1 setText:string1];
[output2 setText:string2];
[binResult setText:string3];
[bin1 release];
[bin2 release];
[resBin release];
}
}
-(NSMutableArray*)addBinary:(NSMutableArray*)binary1 toBinary:(NSMutableArray*)binary2
{
BOOL carry = NO;
NSMutableArray* result = [NSMutableArray arrayWithCapacity:32];
for (int i = 0; i < flowLimit; i++)
{
if (([[binary1 objectAtIndex:i] intValue] == 0) && ([[binary2 objectAtIndex:i] intValue] == 0))
{
if (carry)
{
[result addObject:[NSNumber numberWithInt:1]];
}
else {[result addObject:[NSNumber numberWithInt:0]];}
}
else if (([[binary1 objectAtIndex:i] intValue] == 1) && ([[binary2 objectAtIndex:i] intValue] == 0))
{
if (carry)
{
[result addObject:[NSNumber numberWithInt:0]];
carry = YES;
}
else {[result addObject:[NSNumber numberWithInt:1]];}
}
else if (([[binary1 objectAtIndex:i] intValue] == 0) && ([[binary2 objectAtIndex:i] intValue] == 1))
{
if (carry)
{
[result addObject:[NSNumber numberWithInt:0]];
carry = YES;
}
else {[result addObject:[NSNumber numberWithInt:1]];}
}
else
{
if (carry)
{
[result addObject:[NSNumber numberWithInt:1]];
carry = YES;
}
else
{
[result addObject:[NSNumber numberWithInt:0]];
carry = YES;
}
}
carry = NO;
}
return result;
}
代碼運行在調試正常,但運行這些方法後的地方,我得到一個「EXC_BAD_ACCESS」錯誤。任何人都知道這是爲什麼發生?
我承認,這是一個比我更好的主意。 –
+1這是正確的答案。你已經在你的方法開始時分配了resBin,但隨後通過調用'[self addBinary:toBinary]'將它重新分配給一個自動釋放的可變數組,因此釋放resBin導致崩潰(並導致它崩潰)。 – Rog
實際上,使用'NSMutableArray * bin1 = [NSMutableArray array];'使用'withCapacity'版本可以獲得很少或沒有什麼東西。 – zaph