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

  • SEARCH
  • Home
  • 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 7738353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:14:32+00:00 2026-06-01T08:14:32+00:00

I get a segmentation fault when running this code. Anyone know why? Thanks. #include

  • 0

I get a segmentation fault when running this code. Anyone know why? Thanks.

#include <stdio.h>

int main()
{
    double **m1, **m2, **mr;
    int m1_rows, m1_cols, m2_rows, m2_cols, mr_rows, mr_cols;
    int i, j, k;

    printf("Enter number of rows for matrix 1: ");
    scanf("%d", &m1_rows);

    printf("Enter number of columns for matrix 1: ");
    scanf("%d", &m1_cols);

    printf("Enter number of rows for matrix 2: ");
    scanf("%d", &m2_rows);

    printf("Enter number of columns for matrix 2: ");
    scanf("%d", &m2_cols);

    //allocate memory for matrix 1 m1
    m1 = (double **) calloc(m1_rows, sizeof(double *));
    for (i = 0; i < m1_rows; i++) {
        m1[i] = (double *) calloc(m1_cols, sizeof(double));
    }

    //allocate memory for matrix 2 m2
    m2 = (double **) calloc(m2_rows, sizeof(double *));
    for (i = 0; i < m2_rows; i++) {
        m2[i] = (double *) calloc(m2_cols, sizeof(double));
    }

    //allocate memory for sum matrix mr
    mr = (double **) calloc(mr_rows, sizeof(double *));
    for (i = 0; i < mr_rows; i++) {
        mr[i] = (double *) calloc(mr_cols, sizeof(double));
    }

    //assign  mr_rows and mr_cols
    mr_rows = m1_rows;
    mr_cols = m2_cols;

    //initialize product matrix
    for (i = 0; i < m1_rows; i++) {
        for (j = 0; j < m2_cols; j++) {
            mr[i][j] = 0;
        }
    }

    //perform matrix multiplication
    for (i = 0; i < m1_rows; i++) {
        for (j = 0; j < m2_cols; j++) {
            mr[i][j] = 0;
            for (k = 0; k < m1_cols; k++) {
                mr[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }

    //print result
    for (i = 0; i < mr_rows; i++) {
        for (j = 0; j < mr_cols; j++) {
            printf("%f\t", mr[i][j]);
        }
    }

    //free memory m1
    for (i = 0; i < m1_rows; i++); {
        free(m1[i]);
    }
    free(m1);

    //free memory m2
    for (i = 0; i < m2_rows; i++); {
        free(m2[i]);
    }
    free(m2);

    //free memory mr
    for (i = 0; i < mr_rows; i++); {
        free(mr[i]);
    }
    free(mr);

    return 0;
}

I ran using valgrind valgrind --tool=memcheck a.out for more info on the segmentation fault, but the result was over 30000 errors so it didn’t print them out.

  • 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-01T08:14:34+00:00Added an answer on June 1, 2026 at 8:14 am

    You are not assigning mr_rows and mr_cols. They need to be set like this:

    mr_rows = m1_rows;
    mr_cols = m2_cols;
    

    This line is no good:

    mr[i][j] += m1[i][k] * m2[k][j];
    

    That will be accessing elements out of bounds, not least because k is not initialized. You need that line of code inside three nested for loops. Indeed, you may as well roll the zeroising code into this too.

    for(i=0; i<m1_rows; i++){
        for(j=0; j<m2_cols; j++){
            mr[i][j] = 0;
            for(k=0; k<m1_cols; k++){
                mr[i][j] += m1[i][k]*m2[k][j];
            }
        }
    }
    

    Also, all your memory freeing loops are wrong. Instead of

    for(i=0; i<m1_rows; i++);{
        free(m1[i]);
    }
    free(m1);
    

    it should read

    for(i=0; i<m1_rows; i++){
        free(m1[i]);
    }
    free(m1);
    

    That stray semicolon was killing you.

    You also need to perform a check that the number of columns in m1 equals the number of rows in m2, i.e. test that m1_cols == m2_rows.

    One final point. You duplicate your code horribly here. Don’t have three identical for loops to allocate a matrix and three identical for loops to deallocate. Extract those operations into helper functions which can be called from main.

    That’s all that I can find!

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

Sidebar

Related Questions

Why do I get segmentation fault in this function: #include <stdio.h> #include <stdlib.h> #include
When I run the following code: #include <stdio.h> int main(int argc, char *argv[]) {
Here is my code, #include<signal.h> #include<stdio.h> int main(int argc,char ** argv) { char *p=NULL;
I have a confusion related to this program. #include <stdio.h> int main(void) { int
In the following code I get a segmentation fault: Set *getpar() {...} char function(...)
Please help check the following code: running it on linux encountered segmentation fault. #deinf
On running the following script, I get segmentation fault. The output consists of here
We have code running JNI in a separate thread. We occasionally get segmentation faults
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Upon compiling and running this small program to reverse a string, I get a

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.