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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:44:00+00:00 2026-05-19T00:44:00+00:00

I have a problem using malloc. I have a function called jacobi_gpu wich is

  • 0

I have a problem using malloc.

I have a function called jacobi_gpu wich is called many times :

int main(int argc, char* argv[]){

    /* ... */

    int totalrot=0;
    while(nrot>0){
        iter++;
        nrot=jacobi_gpu(a,q, tol, dimmat);
        totalrot+=nrot;

        printf("iter =%3d  nrot=%3d\n",iter, nrot);
    }

    /* ... */
}

The parameters a,q,tol and dimmat are correctly initialized.
A and Q are 2 square matrices and dimmat is their dimension.

Here is my code :

int jacobi_gpu(double A[], double Q[], double tol, long int dim){
    int nrot, p, q, k, tid;
    double c, s;
    double *mc, *vc;

    printf("jacobi begins \n");

    mc   = (double *)malloc(2 * dim * sizeof(double));
    vc   = (double *)malloc(2 * dim * sizeof(double));

    if( mc == NULL || vc == NULL){
        fprintf(stderr, "pb allocation matricre\n");
        exit(1);
    }

    nrot = 0;

    for(k = 0; k < dim - 1; k++){
        eye(mc, dim);
        eye(vc, dim);

        for(tid = 0; tid < floor(dim /2); tid++){
            p = (tid + k)%(dim - 1);
            if(tid != 0)
                q = (dim - tid + k - 1)%(dim - 1);
            else
                q = dim - 1;

            //printf("p = %d | q = %d\n", p, q);
            if(fabs(A[p + q*dim]) > tol){

                nrot++;
                symschur2(A, dim, p, q, &c, &s);

                mc[2*tid] = p;        vc[2 * tid] = c;
                mc[2*tid + 1] = q;    vc[2*tid + 1] = -s;

                mc[2*tid + 2*(dim - 2*tid) - 2] = p;
                vc[2*tid + 2*(dim - 2*tid)   - 2 ] = s;

                mc[2*tid + 2*(dim - 2*tid) - 1] = q;
                vc[2 * tid + 2*(dim - 2*tid) - 1 ] = c;     
            }
        }

        affiche(mc,dim,2,"Matrice creuse");
        affiche(vc,dim,2,"Valeur creuse");

    }
    printf("end\n");
    free(mc);
    free(vc);
    return nrot;
}

My problem is in the malloc call on the mc variable :

*** glibc detected *** ./jacobi_gpu: double free or corruption (!prev): 0x00000000022944a0 ***
    *** glibc detected *** ./jacobi_gpu: malloc(): memory corruption: 0x0000000002294580 ***

Any advice?

[EDIT]

  • The function eye initializes an identity matrix
  • The function affiche displays the matrix with lines and columns. The first parameter is the matrix, the second is the number of lines and the third one is the number of column.

More explanation

The purpose of the matrix mc is to store the variables p and q. Those variables contains column indices.
The purpose of the matrix vc is to store the values contained in those column.
For instance, if the first line of the matrix mc is 0 and 5 ( p = 0, q = 5), that means that the values in the matrix vc will be in the column 0 and 5.
If the matrix fifth line in the matrix mc is 2 3 ( p = 2, q = 3), that means that the values in the fifth line in vc will be in column 2 and 3.

Hope this time, i am more clear.

Thanks for your help

  • 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-05-19T00:44:00+00:00Added an answer on May 19, 2026 at 12:44 am

    Identity matrices are always square, but mc is not. When you call eye(mc, dim) I suspect that eye treats mc like it is a dim by dim matrix when it is in fact a 2 by dim matrix, and writes to unallocated memory.

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

Sidebar

Related Questions

I have a problem using the Java search function in Eclipse on a particular
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I am having a problem using the Unity Application Block, I have created a
I have an odd problem...I'm using a documentation generator which generates a lot of
I have a problem with the following Linq query using Entity Framework: from o
I have a problem with binding data using BindingSource, typed dataset and DataGridView. My
I have a problem. I am coding using VS2008. I am calling webservices from
I have a problem with a memory leak in a .NET CF application. Using

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.