0

我正在使用二维数组形式的矩阵制作曲线拟合程序,但编译器会随机抛出 BAD_ACCESS 错误,例如 Thread 1:EXC_BAD_ACCESS (code=1, address=0x13f800000)。该程序有时会工作,有时会崩溃。任何想法,将不胜感激。谢谢。

- (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];
}
4

2 回答 2

1

您没有为矩阵分配足够的内存。此行设置了整个数据区域,但您只分配matrixSize+1了元素,而不是matrixSize*(matrixSize+1)

temp = (float*)calloc(matrixSize+1, sizeof(float));

因此,维护matrixSize+1列和matrixSize行:

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));
}

当你以后使用它时,要小心。你说错了:

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);
        }
    }
}

请注意,i转到matrixSize+1但您将其用作行索引(只有matrixSize行)。我认为您的意思是使用matrix[j][i]而不是matrix[i][j]. 您在构造初始矩阵时也这样做,但我实际上已将其更改为符合您的分配。

所以我看到你的程序中有两点缓冲区溢出。

于 2012-10-24T21:10:03.303 回答
0

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.

于 2012-10-24T21:07:25.420 回答