Maintaining some code from an iOS application, I came upon the following:
CLLocationCoordinate2D inputArray[size]; // CLLocationCoordinate2D is a struct containing two doubles
for (int i = 0; i < size; i++) {
inputArray[i] = ... ; // Fill the array
}
CLLocationCoordinate2D outputArray[size];
functionThatConvertsInputToOutput(inputArray, outputArray, size);
Here we are doing allocation of two struct arrays of dynamic size (cannot determine size at compile time). So called "Variable-length array", based on that SO question ( Declare Dynamic Array ).
I'm well aware that this does not even compile in C/C++ and when looking after similar questions, the answer is often "Use malloc" or "Use NS(Mutable)Array".
But I haven't really found the answer to the question:
What happens in Objective C when declaring int array[size]; ?
The reason I'm wondering is that the piece of code I have reproduced above crashes when using VLA with reasonably large sizes (36000) and does not crash when using malloc:
CLLocationCoordinate2D *inputArray = malloc(sizeof(CLLocationCoordinate2D) * size);
CLLocationCoordinate2D *ouputArray = malloc(sizeof(CLLocationCoordinate2D) * size);
EDIT #1: What wikipedia says about VLA http://en.wikipedia.org/wiki/Variable-length_array
EDIT #2: Crashes are EXC_BAC_ACCESS at odd places in functionThatConvertsInputToOutput or on the line calling functionThatConvertsInputToOutput.
It’s very likely it’s sticking the memory for the array on the stack, which is why you’re crashing when you blow up the stack by 36,000 * sizeof(CLLocationCoordinate2D).
Related
I am developing an app in XCode and have to write a bit of C for an algorithm. Here is a part of the C code:
double dataTag[M][N];
// dataTag initialized to values.....
double w[N]; // This is outside for loop at the top level of the method
for (int i = 0; i < N; i++) {
w[i] = pow(10.0, dataTag[2][i] / 10.0 / b);
}
//This is inside for loop.....
double disErr[N];
// disErr set and values confirmed with printArray...
double transedEstSetDrv[N][M];
// transedEstSetDrv set and values confirmed with printArray...
double stepGrad[M] = {0, 0, 0};
for (int j = 0; j < M; j++) {
double dotProductResult[M];
dotProductOfArrays(w, disErr, dotProductResult, N);
stepGrad[j] = sumOfArrayMultiplication(transedEstSetDrv[j], dotProductResult, M);
}
// Print array to console to confirm values
NSLog(#"%f %f %f", stepGrad[0], stepGrad[1], stepGrad[2]); <-- if this is present algorithm gives different results.
//Continue calculations......
So this is a part of algorithm in C which is inside for loop. The weird part is the NSLog that prints stepGrad array. Depending if i comment the call to the NSLog or not - the algorithm as a whole gives different results.
It would be great if someone gave some debugging suggestions.
Thanks!
UPDATE 1:
Simplified example which has the same issue and gave more code to support the issue.
UPDATE 2:
Removed the length_of_array function and just replaced it with a known number for simplicity.
So i will answer my own question.
Thanks to the comment from #Klas Lindbäck, i fixed the issue which was related to not initializing a C static array in for loop. So i went over all arrays before and after the code that had issue and did a
memset(a_c_array, 0, sizeof(a_c_array));
after declaration of each array. That is now working fine. Thank you for all your help!
I have a crash with some optimisation code. What I'm trying to do is to remove some points from the input array when the previous and the next point are close enough. The method works well in almost all case but crash with some specific data.
An example of input data that crash:
Value of coords : (51.55188, -0.17591), (51.55208, -0.17516), (51.55231, -0.17444)
Value of altitudes : 10000, 10000, 10000
Value of count : 3
If I skip the optimisation code and use directly the input value, then everything works correctly. It also works correctly if I simply memcpy the input values in the temp arrays.
I got a EXC_BAD_ACCESS EXC_I386_GPFLT after using this method with the input data posted. The crash doesn't happen directly in this method but after when I use the object created at the end of the method. I've already tried NSZombie and Profiling for zombies. Everything works correctly with almost all the data but crash 100% with this specific input data (At least it is easier for me to debug!).
The code of my method:
+ (instancetype) optimizedPolylineWithCoordinates:(CLLocationCoordinate2D*) coords altitudes:(RLMKAltitude*) altitudes count:(NSUInteger) count
{
CGFloat minimumDistanceBetweenPoints = [self minimumOptimizedDistanceBetweenPoints];
CLLocationCoordinate2D* tempCoords = malloc(sizeof(CLLocationCoordinate2D) * count);
RLMKAltitude* tempAltitudes = malloc(sizeof(RLMKAltitude) * count);
NSUInteger tempCoordsCount = 0;
// Always keep first point
tempCoords[0] = coords[0];
tempAltitudes[0] = altitudes[0];
++tempCoordsCount;
for (NSUInteger i = 1; i < (count - 1); i++)
{
MKMapPoint prevPoint = MKMapPointForCoordinate(coords[i - 1]);
MKMapPoint nextPoint = MKMapPointForCoordinate(coords[i + 1]);
// Get the distance between the next point and the previous point.
CLLocationDistance distance = MKMetersBetweenMapPoints(nextPoint, prevPoint);
// Keep the current point if the distance is greater than the minimum
if (distance > minimumDistanceBetweenPoints)
{
tempCoords[tempCoordsCount] = coords[i];
tempAltitudes[tempCoordsCount] = altitudes[i];
++tempCoordsCount;
}
}
// Always keep last point
tempCoords[tempCoordsCount] = coords[(count - 1)];
tempAltitudes[tempCoordsCount] = altitudes[(count - 1)];
++tempCoordsCount;
RLMKMapWay* object = [self polylineWithCoordinates:tempCoords altitudes:tempAltitudes count:tempCoordsCount];
free(tempCoords);
free(tempAltitudes);
return object;
}
Note that the polylineWithCoordinates method called with the temp data take care of making copy of all the data so the problem is likely not related with the free located after the call (I've already tried to comment both lines and the crash still happen)
When count == 1, you are writing outside the allocated memory.
Using the following code, I am attempting to convert three float values into a single NSData object, which I can then transmit over a serial port.
float kP = [[self.kPTextField stringValue] floatValue];
float kI = [[self.kITextField stringValue] floatValue];
float kD = [[self.kDTextField stringValue] floatValue];
float combined[] = {kP, kI, kD};
NSData *dataPackage = [NSData dataWithBytes:&combined length:sizeof(combined)];
[self.serialPort sendData:dataPackage];
The problem is that it doesn't seem to work very well. Whenever I use the "sizeof()" C function, it tells me that the "dataPackage" is only 8 bytes, even though 3 float values should total 12 bytes. I am receiving the data with an Arduino. It sees the bytes coming in, but they aren't legible at all. I don't think it's a problem on the Arduino side of things (but who knows?).
Any help would be appreciated! I'm not a CS major, just a bio major, and I've never learned this stuff in a formal way so I am sorry if my question is ridiculous. I've spent several hours searching the net about this problem and haven't found anything that helped.
EDIT: It turns out this code was completely correct. I made a simple mistake on the arduino side of things by using a struct instead of a union to take the bytes and convert them back into floats.
For others who may be in a similar predicament, a successful way to convert floats from bytes coming out of the serial port is the following:
(at top of implementation file)
union {
float pidVals[3];
byte bytes[12];
} pidUnion;
(inside loop)
if (Serial.available() > 11) {
for (int i = 0; i < 12; i++) {
pidUnion.bytes[i] = Serial.read();
}
}
//Now, you can get access to all three floats of data using pidUnion.pidVals[0], pidUnion.pidVals[1], etc.
This probably isn't the best or most reliable way to transmit data. There is no error-correcting mechanism or packet structure. But it does work in a pinch. I imagine you would probably want to find a way to create a packet of data along with a hash byte to make sure all of the data is correct on the other side, this code doesn't have any of that though.
There are multiple problems with your code.
First, you don't want to use stringValue on a text field. You want the text property, which is a string.
So the first line should read like this:
float kP = [self.kPTextField.text floatValue];
Second, in C, an array of things is a pointer. The data type of
float combined[]
and
float *combined
is identical. Both are "pointer to float".
So this code:
NSData *dataPackage = [NSData dataWithBytes:&combined
length: sizeof(combined)];
Should not have an ampersand in front of combined. It should read:
NSData *dataPackage = [NSData dataWithBytes:combined
length: sizeof(combined)];
Third, what matters is sizeof(combined), not sizeof(dataPackage).
The expression sizeof(dataPackage) will tell you the size of the variable dataPackage, which is a pointer to an NSData object. You must be running on a 64 bit device, where pointers are 8 bytes.
To test the length of the data in your NSData object, you want to ask it with the length property:
NSLog(#"sizeof(combined) = %d", sizeof(combined)";
NSData *dataPackage = [NSData dataWithBytes:&combined
length: sizeof(combined)];
NSLog(#"dataPackage.length = %d", dataPackage.length";
Both log statements should display values of 12.
I'm currently parsing NSString values to NSNumbers and then adding them into a NSMutableArray called operands in an object called "data" like so:
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:*operandString];
[data.operands addObject:myNumber];
I then retrieve those numbers, perform some math on them, then update the array:
double x = [[data.operands objectAtIndex: i]doubleValue];
double y = [[data.operands objectAtIndex: i + 1]doubleValue];
double answer = x * y;
[data.operands replaceObjectAtIndex:(i) withObject:[NSNumber numberWithDouble:answer]];
When I get the answer, everything looks fine eg: ( 3.33 * 5 = 16.65)
BUT, when I look in the debugger I'm seeing some crazy values for x and answer, such as:
x = 3.3300000000000001
answer = 16.649999999999999
Why is this happening? Am I loosing some precision with parsing these back and fourth? Is it how I've used the NSNumberFormatter to parse the string?
The reason I'm in trouble with this is because I'm trying to ensure there's no double overflow errors so I'm using this simple test to check the integrity:
if (answer / y != x){
//THROW OVERFLOW ERROR
}
With the above crazy numbers this is always inconsistent. When I NSLog the answer it comes out fine:
NSLog (#"%g", [[data.operands objectAtIndex:i]doubleValue]]);
Same for
NSLog (#"%f", [[data.operands objectAtIndex:i]doubleValue]]);
You are not losing any precision that you need to worry about. Those are the correct values. There are only about 2^60 different double numbers, that finite set has to try to approximately cover the infinite 'number of numbers' in the range that doubles cover.
In other words, there are no exact answers in computer land and your
if (answer / y != x){
//THROW OVERFLOW ERROR
}
Will not work. Or it may work much of the time, but fail if you push it. Instead you need to acknowledge the limited precision (which is pretty high precision) of doubles:
//Don't waste time worrying like this...
if (fabs(answer / y - x) > 1e-12*fabs(answer)){
//Not correct or useful thing to check don't use this - i did not check
}
// let the math package handle it:
if (isnan(answer)){
// we gots problems
}
if (!isnormal(answer)){
// we gots some other problems
}
Also don't forget that 10^300 is a very large number, doubles work pretty well. To use 32 bit floats you need to pay much more attention to order of execution, etc.
NSLog is likely outputting with fewer decimals of precision, and rounds to the nearest thing, so the answers look better.
It seems I cannot make C++/CLI structures be aligned with less than 8 bytes. I have a struct of two Int32, allocate a million of them, and voilà: 16 MB memory according to ".NET Memory Profiler" (plus the list data). I set the compiler option to /Zp4 (also tried /Zp1), to Minimize Size (/O1) and Small Code (/Os), just to make sure, I additionally put a "#pragma pack(1)" into my code, to no avail. My struct is still taking up 16 Bytes. I changed it to class, still the same.
Why that?
How to change?
Ciao,
Eike
using namespace System;
#pragma pack(1)
ref struct myStruct
{
Int32 a;
Int32 b;
};
int main(array<System::String ^> ^args)
{
System::Collections::Generic::List<myStruct^> list;
for (int i = 0; i < 1000000; i++)
{
list.Add(gcnew myStruct());
}
// avoid optimization
Console::WriteLine(list[333333]->a);
return 0;
}
You need to use value types to be able to specify alignment. Beyond that I'm not sure this is the best way to measure this. Reference types also have some small built in overhead. Try value struct/value class instead.