Amazon.com Widgets Exercise 10.3 exec_bad_access error
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 03:46:16 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 10 - More on Variables and Data Types
| | | |-+  Exercise 10.3 exec_bad_access error
Pages: [1]   Go Down
Print
Author Topic: Exercise 10.3 exec_bad_access error  (Read 771 times)
TheWatchfulOne
Newbie
*
Posts: 2


Email




« on: July 04, 2010, 05:04:30 PM »

Code: (Objective-C)
//
//  Fraction.h
//  Program 9.2, Exercise 9.3, Exercise 9.4, Exercise 9.5, Program 10.1, Program 10.2
//
//  Created by anon on 5/31/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <objc/Object.h>

@interface Fraction : NSObject
{
int numerator;
int denominator;
}

// initializer methods

-(Fraction *) initWith: (int) n Over: (int) d;

//  setter methods

-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) setTo: (int) n over: (int) d;

//  getter methods

-(int) getNumerator;
-(int) getDenominator;

//  other methods
-(double) convertToNum;
-(id) addValue: (id) f;
-(void) reduce;
-(void) print;

// allocation methods

+(Fraction *) allocF;
-(void) releaseF;
+(int) getCount;
+(int) getAddCount;
+(void) printCount;

@end

Code: (Objective-C)
//
//  Fraction.m
//  Program 9.2, Exercise 9.3, Exercise 9.4, Exercise 9.5, Program 10.1, Program 10.2
//
//  Created by anon on 5/31/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"
#import <stdio.h>

static int gCounter;
static int gAddCount;

@implementation Fraction;

//  initializer methods

-(Fraction *) initWith:(int)n Over:(int)d
{
self = [super init];

if (self)
{
[self setTo: n over: d];
}

return self;
}

//  setter methods

-(void) setNumerator: (int) n
{
numerator = n;
}

-(void) setDenominator: (int) d
{
denominator = d;
}

//  getter methods

-(int) getNumerator
{
return numerator;
}

-(int) getDenominator
{
return denominator;
}

//  other methods

-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return 0.0;
}

-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}



-(id) addValue: (id) f   //add a Fraction to the receiver
{
extern int gAddCount;
++gAddCount;

//to add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b*d)

//result will store the result of the addition

id resultFrac = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * [f getDenominator]) + (denominator * [f getNumerator]);
resultDenom = denominator * [f getDenominator];

[resultFrac setTo: resultNum over: resultDenom];
[resultFrac reduce];

return resultFrac;
}

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while ( v != 0 )
{
temp = u % v;
u = v;
v = temp;
}

numerator /= u;
denominator /= u;
}

-(void) print
{
printf(" %i/%i ", numerator, denominator);
}

// allocation methods

+(Fraction *) allocF
{
extern int gCounter;
++gCounter;

return [Fraction alloc];
}

-(void) releaseF
{
extern int gCounter;
--gCounter;

[self release];
}

+(int) getCount
{
extern int gCounter;

return gCounter;
}

+(int) getAddCount
{
extern int gAddCount;

return gAddCount;
}

+(void) printCount
{
extern int gCounter;

printf("%i Fractions are declared.\n", gCounter);
}

@end

Code: (Objective-C)
//
//  main.m
//  Exercise 10.3
//
//  Created by anon on 7/3/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "Fraction.h"

int main(int argc, char *argv[])
{
Fraction *f1 = [[Fraction allocF] initWith: 1 Over: 8 ];
Fraction *f2 = [[Fraction allocF] initWith: 1 Over: 16 ];
Fraction *resultF = [[Fraction allocF] init];
int cnt;

for(cnt = 1; cnt <= 3; ++cnt)
{
printf("\n");
[f1 print];
printf(" + ");
[f2 print];
printf(" = ");
resultF = [f1 addValue: f2];
[resultF print];

f1 = resultF;

printf("\n%i Fractions have been declared.\n", [Fraction getCount]);
printf("\nThe Fraction addValue method has been called %i times.\n", [Fraction getAddCount]);
}

[f1 releaseF];
[Fraction printCount];
[f2 releaseF];
[Fraction printCount];
[resultF releaseF];
[Fraction printCount];

    return 0;
}

In main.h, whenever I include "f1 = resultF" my code appears to run but then gives me an error that says Exec_Bad_Access.  If I comment that line out, the program terminates normally.  Why is this? Huh
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: July 04, 2010, 05:26:05 PM »

F1 and resultF reference the same Fraction object and so should not be released.  If that's a little unclear, please read this thread:

http://classroomm.com/objective-c/index.php?topic=56.0

Cheers,


Steve Kochan
Logged
TheWatchfulOne
Newbie
*
Posts: 2


Email




« Reply #2 on: July 04, 2010, 06:03:17 PM »

Wow, Steve, thanks for such a quick reply!  I kind of see what you are saying.  This now brings up another question:  If f1 and resultF reference the same Fraction object, then does that mean I didn't have to allocate resultF?  Or maybe a better question would be, what happened to the memory that was used when resultF was allocated?  And how would it ever get released?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #3 on: July 04, 2010, 06:47:22 PM »

Please read through the thread reference I gave you, as it covers your question in detail.

Cheers,


Steve Kochan
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.