gpt4 book ai didi

objective-c - Objective-C 中的动态二维 C 数组崩溃

转载 作者:行者123 更新时间:2023-12-02 16:03:36 25 4
gpt4 key购买 nike

我正在使用二维数组形式的矩阵制作曲线拟合程序,但编译器随机抛出 BAD_ACCESS 错误,例如线程 1:EXC_BAD_ACCESS(代码= 1,地址= 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];
}

最佳答案

您没有为矩阵分配足够的内存。此行设置了整个数据区域,但您只分配了 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]。您在构造初始矩阵时也可以这样做,但实际上我已将其更改为符合您的分配。

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

关于objective-c - Objective-C 中的动态二维 C 数组崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13057703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com