james-collinss-macbook-prorog17 jamescollins$ ./prog11.1a
1/3 + 2/5 = 11/15
1/3 - 2/5 = -1/15
1/3 * 2/5 = 2/15
1/3 / 2/5 = 5/6
Segmentation fault
got the above output when i ran a program from a book on Objective-C by Stephen G. Kochan, the program compiled without any error or warning messages.
here is my test program which i called prog11.1.m
one thing that i would like to mention is that the program compiled and ran ok with
the following line emitted
but when i added the above line i got a segmentation fault when i went to run the program, in the Kochan book a lot of the programs that i looked at in the later chapters like chapter 17 had pool release added?
do i need this line?
any help would be appreciated.
1/3 + 2/5 = 11/15
1/3 - 2/5 = -1/15
1/3 * 2/5 = 2/15
1/3 / 2/5 = 5/6
Segmentation fault
got the above output when i ran a program from a book on Objective-C by Stephen G. Kochan, the program compiled without any error or warning messages.
here is my test program which i called prog11.1.m
Code:
#import "Fraction.h"
@interface Fraction (MathOps)
-(Fraction *) add: (Fraction *) f;
-(Fraction *) mul: (Fraction *) f;
-(Fraction *) sub: (Fraction *) f;
-(Fraction *) div: (Fraction *) f;
@end
@implementation Fraction (MathOps);
-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b * c)) / (b * d)
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = (numerator * [f denominator]) +
(denominator * [f numerator]);
resultDenom = denominator * [f denominator];
[result setTo: resultNum over: resultDenom];
[result reduce];
return [result autorelease];
}
-(Fraction *) sub: (Fraction *) f
{
// To sub two fractions:
// a/b - c/d = ((a*d) - (b*c)) / (b * d)
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;
resultNum = (numerator * [f denominator])-
(denominator * [f numerator]);
resultDenom = denominator * [f denominator];
[result setTo: resultNum over: resultDenom];
[result reduce];
return [result autorelease];
}
-(Fraction *) mul: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
[result setTo: numerator * [f numerator]
over: denominator * [f denominator]];
[result reduce];
return [result autorelease];
}
-(Fraction *) div: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
[result setTo: numerator * [f denominator]
over: denominator * [f numerator]];
[result reduce];
return [result autorelease];
}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];
Fraction *result;
[a setTo: 1 over: 3];
[b setTo: 2 over: 5];
[a print]; printf (" + "); [b print]; printf (" = ");
result = [a add: b];
[result print];
printf ("\n");
[result release];
[a print]; printf (" - "); [b print]; printf (" = ");
result = [a sub: b];
[result print];
printf ("\n");
[result release];
[a print]; printf (" * "); [b print]; printf (" = ");
result = [a mul: b];
[result print];
printf ("\n");
[result release];
[a print]; printf (" / "); [b print]; printf (" = ");
result = [a div: b];
[result print];
printf ("\n");
[result release];
[a release];
[b release];
[pool release];
return 0;
}
one thing that i would like to mention is that the program compiled and ran ok with
the following line emitted
Code:
[pool release];
but when i added the above line i got a segmentation fault when i went to run the program, in the Kochan book a lot of the programs that i looked at in the later chapters like chapter 17 had pool release added?
do i need this line?
any help would be appreciated.