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 6782299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:43:30+00:00 2026-05-26T16:43:30+00:00

so this is part of my homework assignment. need some help with it. my

  • 0

so this is part of my homework assignment. need some help with it. my task is to swap rows and columns so that the numbers on the main diagonal of a matrix are in descending order. the number of rows and columns is the same. also I need to allocate memory dynamically.

here is an example:
input:

    1 2       1<4 so we swap rows and columns and the final result is 4 3 
    3 4                                                               2 1

THE PROBLEM is that when I insert a 3×3 array or even larger I get some kind of a segmentation fault. here is my code, please help!

    int **t, n, i, j, aux;
    printf("Insert the size of the matrix: ");
    scanf("%d",&n);

    t = malloc(n * sizeof(int *));

    for(i=1; i <= n; i++){
            t[i]=malloc(n * sizeof(int));
    }
    for(i=1; i<=n; i++){
            for(j=1; j<=n; j++){
                    printf("Element [%d][%d] : ", i, j);
                    scanf("%d", &t[i][j]);
            }
    }

    for(i=2; i<=n; i++){
            if(t[i][i] > t[i-1][i-1]){
                    j=i-1;
                    for(i=1; i<=n; i++){
                            aux=t[i][j];
                            t[i][j]=t[i][j+1];
                            t[i][j+1]=aux;
                    }
                    i=j;
                    for(j=1; j<=n; j++){
                            aux=t[i][j];
                            t[i][j]=t[i+1][j];
                            t[i+1][j]=aux;
                    }
                    i=1;
            }
    i++;
    }
    for(i=1; i<= n; i++){
            free(t[i]);
    }
    free(t);

Now the error I get is the following:

    The matrix inserted:
     1  2  3
     4  5  6
     7  8  9
    The matrix after swapping:
     5  4  6
     2  1  3
     8  7  9
    *** glibc detected *** ./6: double free or corruption (out): 0x08e42018 ***
    ======= Backtrace: =========
    /lib/i686/cmov/libc.so.6[0xb7649764]
    /lib/i686/cmov/libc.so.6(cfree+0x96)[0xb764b966]
    ./6[0x80488fb]
    ....

Thank you in advance!!

  • 1 1 Answer
  • 2 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-05-26T16:43:30+00:00Added an answer on May 26, 2026 at 4:43 pm

    Array indexes start from 0 to n-1, if it’s size is n. With that said, you have quite a few places to take care of it. Play safe with array indexes. For example –

    for(i=1; i <= n; i++){  // Should be i=0; i < n; i++ 
            t[i]=malloc(n * sizeof(int));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is part of a homework assignment. I've got several questions asking find the
(this is indirectly a part of a much larger homework assignment) I have something
I need little help on a homework assignment. I have to create a 10
Before I go any further, this IS apart of my homework.The part that I
Some classmates and I are working on a homework assignment for Java that requires
This is part of a homework assignment. What we have to do is write
Full disclosure, this is part of a homework assignment (though a small snippet, the
I have a graph like this: As part of a homework assignment I want
I want to preface this by saying that this is a homework assignment. I
This is part of a homework assignment so my goal is to understand why

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.