Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:38+00:00 2026-06-13T08:05:38+00:00

I am making a curve fitting program using matrices in the form of a

  • 0

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];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:05:39+00:00Added an answer on June 13, 2026 at 8:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am making a program in opengl to animate a solid-sphere over a curve
I relatively new to Umbraco but using a somewhat contrived pet project I'm making
Making a new site but something is happening to it in IE8. The social
Making a new site but something is happening to it in IE. I've purchased
Making a simple application, so when the user logs out of Windows, it of
I am experimenting with making Blackberry widgets but having a little trouble. My first
I am making an application that does some custom image processing. The program will
I'm trying to create a canvas script that visually draws a cubic bezier-curve, but
I am using ASP.NET-MVC and nHibernate for the first time. Great tools, but big
I am making a custom View where I am plotting a curve. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.