I’m building a header with a bunch of functions to do some astronomy math and so far so good, except these two functions crash the debugger with a Debugging terminated. message and nothing else. The NSLog statements print to the console just fine with the correct answers, but they crash. Any ideas?
float calcTimeJulianCent(float julianDate) {
NSLog(@" -calcTimeJulianCent");
float tCentury = (julianDate - 2451545.0) / 36525.0;
NSLog(@" -calculation complete, tCentury = %1.4f", tCentury);
return tCentury;
}
float calcJDFromJulianCent(float tCentury) {
NSLog(@" -calcJDFromJulianCent");
float julianDate = tCentury * 36525.0 + 2451545.0;
NSLog(@" -calculation complete, tCentury = %1.4f", julianDate);
return julianDate;
}
And here’s the method that calls the functions:
- (IBAction)doMath {
NSLog(@"-%@:%s called", [self class], _cmd);
// other calls that work
float julianCentury = calcTimeJulianCent(julianDay);
NSLog(@" -calcTimeJulianCent called: %@", julianCentury);
float backToJulianDate = calcJDFromJulianCent(0.1092);
NSLog(@" -calcJDFromJulianCent called: %@", backToJulianDate);
// more calls that work
}
EDIT: Answer – silly mistake. The functions return floats and I used %@ in the NSLog which is for strings. You’d think Xcode could do more than just crash over that.
I saw your edit. The reason is crashes is because
NSLogexpects to find a pointer to an object — that is, a memory location — when it finds the%@format specifier. Obviously a floating-point number cannot be a valid memory address, so the program crashes. Xcode doesn’t fix it becauseNSLogtakes a string and a variable list of arguments. However, in C, you cannot specify types for varargs, so there’s no way to tell what type of argumentsNSLogshould take, in the general case.(gcc does provide some format-specifier checking when you call
printf, but the same doesn’t happen withNSLog.)