Dynamic two dimensional C array crashing in Objective-C - objective-c

I am making a curve fitting program using matrices in the form of a two dimensional arrays but the compiler throws out BAD_ACCESS errors at random such as Thread 1:EXC_BAD_ACCESS (code=1, address=0x13f800000). The program works sometimes and other times crashes. Any ideas would be appreciated. Thanks.
- (void)main {
NSLog(#"Enter size");
scanf("%i", &matrixSize);
float* temp;
matrix = (float**)calloc(matrixSize, sizeof(float*));
temp = (float*)calloc(matrixSize+1, sizeof(float));
for (int i = 0; i < matrixSize+1; i++) {
matrix[i] = temp + (i*(matrixSize+1));
}
[self enterPoints];
[self elimination];
[self setNeedsDisplay:YES];
free(matrix);
free(temp);
}
//points entered here
- (void)enterPoints {
CGPoint *points = (CGPoint *)malloc(matrixSize * sizeof(CGPoint));
for (int i = 0; i < matrixSize; i++) {
scanf("%lf", &points[i].x);
scanf("%lf", &points[i].y);
}
for (int j = 0; j < matrixSize; j++) {
for (int i = 0; i < matrixSize+1; i++) {
if (i == (matrixSize)) {
matrix[i][j] = points[j].y;
}
else {
matrix[i][j] = pow(points[j].x, (matrixSize-1)-i);
}
}
}
free(points);
}
//matrix reduction occurs here
- (void)elimination {
for (int j = 0; j < matrixSize; j++) {
double divideValue = matrix[j][j];
for (int i = 0; i < matrixSize+1; i++) {
matrix[i][j] /= divideValue;
}
for (int j1 = 0; j1 < matrixSize; j1++) {
if (j1 == j) {
if (j1 == matrixSize-1) {
break;
}
else {
j1++;
}
}
double subValue = matrix[j][j1];
for (int i = 0; i < matrixSize+1; i++) {
matrix[i][j1] -= matrix[i][j]*subValue;
}
}
}
}
//drawing the polynomial
- (void)drawRect:(NSRect)dirtyRect {
NSGraphicsContext * GraphicsContext = [NSGraphicsContext currentContext];
CGContextRef context = (CGContextRef) [GraphicsContext graphicsPort];
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 3.0);
CGContextMoveToPoint(context, 0, matrix[matrixSize][0]*100 + 100);
[GraphicsContext saveGraphicsState];
CGMutablePathRef path;
path = CGPathCreateMutable();
for (float i = -matrixSize; i < matrixSize; i+=.01) {
float y = 0;
for (int j = 0; j < matrixSize; j++) {
y += matrix[matrixSize][j]*pow(i, j);
}
CGContextAddLineToPoint(context, i*100 + 100, y*100 + 100);
}
CGContextStrokePath(context);
[GraphicsContext restoreGraphicsState];
}

You did not allocate enough memory for your matrix. This line sets up the entire data area, but you have only allocated matrixSize+1 elements, instead of matrixSize*(matrixSize+1):
temp = (float*)calloc(matrixSize+1, sizeof(float));
So, maintaining the matrixSize+1 columns and matrixSize rows:
matrix = (float**)calloc(matrixSize, sizeof(float*));
temp = (float*)calloc(matrixSize * (matrixSize+1), sizeof(float));
for (int i = 0; i < matrixSize; i++) {
matrix[i] = temp + (i*(matrixSize+1));
}
When you use this later, be careful. You are addressing it wrong:
for (int j = 0; j < matrixSize; j++) {
for (int i = 0; i < matrixSize+1; i++) {
if (i == (matrixSize)) {
matrix[i][j] = points[j].y;
}
else {
matrix[i][j] = pow(points[j].x, (matrixSize-1)-i);
}
}
}
Notice that i goes to matrixSize+1 but you are using that as the row index (there are only matrixSize rows). I think you meant to use matrix[j][i] instead of matrix[i][j]. You also do this when you construct the initial matrix, but I've actually changed that to be in line with your allocation.
So there are two points of buffer overrun in your program that I see.

EXC_BAD_ACCESS indicates one of your objects is being over-released (not to be confused with garbage collection) before invoking a method on it. Once you reach the point in your code where you invoke the method on the collected object, the pointer is referencing an invalid memory location.
To find Zombie objects have a look at this: How to Enable NSZombie in XCode
I've used this and it works very well.

Related

Objective C - What causes copying of data to float array to result in continuous memory increase?

When I call this method
- (int)runDetection:(NSArray *)data length:(int)length
{
long count = [data count];
float *dataArray = (float *)malloc(sizeof(float *) * count);
for (int i = 0; i < count; ++i)
{
dataArray[i] = [[data objectAtIndex:i] floatValue];
}
free(dataArray);
return -2;
}
But after commenting this piece of code, the app works fine. No continuous memory increase.
for(int i = 0; i < count; ++i)
{
dataArray[i] = [[data objectAtIndex:i] floatValue];
}
The memory is freed after use so why is this causing memory increase?

Objective-C equal distribution from 5 different NSMutableArrays

This is my code but its still not correct. Currently i can distribute like this: 0,1,2,0,1,2,0,1,2 but i will distribute it 0,1,2,2,1,0,0,1,2,2 (0,1,2 are the groups)
//Create x Groups
for (int z=0; z<numberOfGroups; z++) {
mutableArrayOfSubarrays[z] = [NSMutableArray arrayWithCapacity:countOfElementsForTheGroups];
}
int nextSubarray = 0;
//Distribute the Objects into the groups
for (int i = 0; i < [AllObjectsToDistribute count]; i++)
{
[mutableArrayOfSubarrays[nextSubarray] addObject:[AllObjectsToDistribute objectAtIndex:i]];
nextSubarray = nextSubarray % customGroups;
nextSubarray++;
nextSubarray = nextSubarray % customGroups;
}
Keep track of which way the group index counts in a BOOL.
Example:
NSUInteger groupIndex = 0;
BOOL groupIndexCountsUp = YES;
for (id object in allObjectsToDistribute) {
[groupsArray[groupIndex] addObject:object];
if (groupIndexCountsUp) {
if (groupIndex < numberOfGroups - 1)
groupIndex++;
else
groupIndexCountsUp = NO;
}
else {
if (groupIndex > 0)
groupIndex--;
else
groupIndexCountsUp = YES;
}
}

Detect break statement in function?

I have the function below:
- (void)doStuff {
BOOL allDoneNow = NO;
for (int i = 0; i < [arABFBmatches count]; i++) {
for (int j = 0; j < [arABFBmatches count]; j++) {
if (should_skip_rest) {
allDoneNow = YES;
break;
}
}
if (allDoneNow) break;
}
}
Now, if I call that method with [self doStuff];, how can I detect if the function broke? Or is there a better way to stop and restart the execution of a function?
- (BOOL)doStuff // <- return a boolean value
{
for (int i = 0; i < [arABFBmatches count]; i++)
{
for (int j = 0; j < [arABFBmatches count]; j++)
{
if (should_skip_rest)
{
return NO; // <- NO for break
}
}
}
return YES; // <- YES for completed
}
This beaks the execution of the function. If you want to restart it, simply call it in a while loop.
while( (doStuff()==NO) && thereShouldBeAnotherConditionForStopping )
{
// Do something after each attempt, otherwise it seems to be a little bit silly
}
Or you can use blocks, like this:
- (void)doStuff:(void (^)())breakBlock{
BOOL allDoneNow = NO;
for (int i = 0; i < [arABFBmatches count]; i++) {
for (int j = 0; j < [arABFBmatches count]; j++) {
if (should_skip_rest) {
allDoneNow = YES;
breakBlock();
break;
}
}
if (allDoneNow) breakBlock();
}
}
and call it:
[self doStuff:^{
NSLog(#"ends with break");
}];

creating sprite animation from sprite sheet cocos2d

I'm trying to create an animated sprite from a sprite sheet, to do this I'm running the following:
NSMutableArray *bunsenAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 74; ++i) {
[bunsenAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"15_%i.png", i]]];
}
for (int j = 1; j <= 74; ++j) {
[bunsenAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"15_1.png"]]];
}
for (int k = 75; k <= 148; ++k) {
[bunsenAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"15_%i.png",k]]];
}
for (int l = 1; l <= 74; ++l) {
[bunsenAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"15_1.png"]]];
}
NSLog(#"bunsenAnimFrames is %#", bunsenAnimFrames );
CCAnimation *bunsenAnimationAnimation = [CCAnimation animationWithSpriteFrames:bunsenAnimFrames delay:1/25];
bunsens = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:bunsenAnimationAnimation]];
Then later during populating a stage with a set of tiled sprites I run:
else if (blockValue == 2 || blockValue == 3 || blockValue == 4 || blockValue == 5) {
bunsen = [CCSprite spriteWithSpriteFrameName:#"15_1.png"];
if (blockValue == 2) {
bunsen.rotation = 90;
}
else if (blockValue == 3) {
bunsen.rotation = 180;
}
else if (blockValue == 4) {
bunsen.rotation = 270;
}
float tileY = screenSize.height-((countery*startData4)+startData4/2+77.5)/2;
float tileX = ((counterx*startData4)+startData4/2+5)/2;
bunsen.position = ccp(tileX,tileY);
[bunsenAnimation addChild:bunsen];
[bunsen runAction:bunsens];
}
This works perfectly well in adding the bunsen sprite (declared as a sprite in header file) however on calling [bunsen runAction:bunsens]; nothing happens to the sprite, any reason for this?
The solutions I found for this was not using a fraction in the delay for CCAnimation... strange! as I thought the delay value could be any float!

Defining a matrix as an array of arrays and computation its inverse matrix in C++

Unfortunately I haven't much experience in C++ and I'm trying to progress myself in C++.
Firstly,I defined array of arrays so that I formed an 3x3 matrix:
array< array< double >^ >^ input = gcnew array< array< double >^ >(3);
for (j=0;j<input->Length;j++){
input[j]=gcnew array<double>(3);
Then I assigned matrix elements to input array of arrays:
int value=1;
for(y=0;y<(3);y++){
for(x=0;x<(3);x++)
{input[y][x]=value;
value=value+1;
}
}
Is there a C++ function that compute inverse matrix of this input array of arrays?
Could you help me please?
Best Regards...
Look at Simple 3x3 matrix inverse code (C++).
There are no functions in C++ to make matrix operations, you'll need to find some library to do it or implement your own.
Note that for fixed size arrays, you can use regular C/C++ arrays, like this one:
double arr[3][3];
Wikipedia has a list of numerical libraries in various programming languages. If the functions that you are looking for are not available you could also consider writing a wrapper function around an available Fortran or C or C++ implementation.
There is not built in C++ function to do matrix inversion (or for any other matrix calculations).
There are lots of methods for matrix inversion mentioned HERE.
If you want efficiency and ease of implementation then Guassian Elimination is
probably the best.
Code for inverse of matrix using Elementary row transformation in c++
#include<iostream>
#include<stdio.h>
#include<conio.h>
using
namespace std;
float a[4][4];float b[4][4]={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
int no = 4;
int check(int k) {
float cont = 0, cont2 = 0;
for (int i = k; i < no; i++) {
if (a[i][k] == 1) {
for (int j = 0; j < no; j++) {
cont = a[i][j];
cont2 = b[i][j];
a[i][j] = a[k][j];
b[i][j] = b[k][j];
a[k][j] = cont;
b[k][j] = cont2;
}
} else if (a[i][k] == 0) {
for (int j = 0; j < no; j++) {
cont = a[i][j];
cont2 = b[i][j];
a[i][j] = a[no - 1][j];
b[i][j] = b[no - 1][j];
a[no - 1][j] = cont;
b[no - 1][j] = cont2;
}
}
}
return 0;
}
int divi(int k) {
float particular = a[k][k];
for (int i = 0; i < no; i++) {
a[k][i] = a[k][i] / particular;
b[k][i] = b[k][i] / particular;
if (a[k][i] == (-0)) {
a[k][i] = 0;
}
if (b[k][i] == (-0)) {
b[k][i] = 0;
}
}
return 0;
}
int sub1(int k) {
float particular;
for (int j = k + 1; j < no; j++) {
particular = a[j][k];
for (int i = 0; i < no; i++) {
a[j][i] = a[j][i] - (particular * a[k][i]);
b[j][i] = b[j][i] - (particular * b[k][i]);
if (a[j][i] == (-0)) {
a[j][i] = 0;
}
if (b[j][i] == (-0)) {
b[j][i] = 0;
}
}
}
return 0;
}
int sub2(int k) {
float particular;
for (int j = k - 1; j >= 0; j--) {
particular = a[j][k];
for (int i = no - 1; i >= 0; i--) {
a[j][i] = a[j][i] - (particular * a[k][i]);
b[j][i] = b[j][i] - (particular * b[k][i]);
if (a[j][i] == (-0)) {
a[j][i] = 0;
}
if (b[j][i] == (-0)) {
b[j][i] = 0;
}
}
}
return 0;
}
int disp(){
cout<<endl;
for(int x=0;x<no;x++){
for(int y=0;y<no;y++){
if(a[x][y]==(-0)){a[x][y]=0;}
if(b[x][y]==(-0)){b[x][y]=0;}
printf("%0.1f|||%0.1f ",a[x][y],b[x][y]);
}
cout<<endl;}
}
int main()
{
for(int i=0;i<no;i++){
for(int j=0;j<no;j++){cout<<"Enter a["<<i<<"]["<<j<<"]";cin>>a[i}[j];}
}
for(int i=0;i<no;i++){
for(int j=0;j<no;j++){cout<<a[i][j]<<" ";}
cout<<endl;
}
for(int i=0;i<no;i++){
check(i);
disp();
divi(i);
disp();
sub1(i);
disp();
}
for(int i=no-1;i>=0;i--){
sub2(i);
disp();
cout<<endl;
}
getch();
getch();
getch();
return 0;}
}