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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:54:00+00:00 2026-05-25T19:54:00+00:00

I am solving a quantum-mech problem which requires me to find some eigenvalues by

  • 0

I am solving a quantum-mech problem which requires me to find some eigenvalues by manipulating some matrices. The specifics of this problem is not relevant, I just need help with the c++ problem, I am new to this language and after a couple of hours I figured any more attempts at solving it myself would be futile and so I turn to you for help.

I have this problem where glibc detects an error at the end of my program and I cannot deallocate properly, it is far too big to copypaste here so I will just replicate the part that actually gives the error.

void hamiltonian(int, double **&);

int i,j;

int main()
{
int N = 1000; double **A;

hamiltonian(N, A);

//Physics here
.
.
.
.
.
//Delete
for(i=0; i<N; i++){delete []A[i];}
delete []A;

return 0;
}

void hamiltonian(int N, double **&A)
{
A = new double *[N];
for(i=0; i<N; i++)
{
A[i] = new double[N];
for(j=0; j<N; j++)
{
if(i==j)A[i][j] = 2;
if(i==j+1 || i==j-1){A[i][j] = 1;}
}
}
}

According to my professor I have to deallocate in the same function as I allocate but I didn’t even think about deallocation after being nearly done with my project and so I have to rewrite a lot of code, the problem is that I cannot deallocate A inside the hamiltonian function as I need it in other functions (inside //Physics).

Surely there must be a way around this? Might sound a bit ignorant of me but this sounds like a less efficient design if I have to deallocate in the same function as I allocate.

  • 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-25T19:54:00+00:00Added an answer on May 25, 2026 at 7:54 pm

    According to my professor I have to deallocate in the same function as I allocate

    That is pure silliness. Sometimes (almost always) you need to use the allocated struct outside the function. Definitely false for objects, since constructors and destructors are different functions.

    Any way, you can get away without using classes, if you make a Matrix struct and associated newMatrix and deleteMatrix functions 🙂

    #include <cstddef>
    #include <iostream>
    
    using namespace std;
    
    struct Matrix
    {
        int n;
        int m;
        double** v;
    };
    
    Matrix newMatrix (int n, int m)
    {
        Matrix A;
        A.n = n;
        A.m = m;
        A.v = new double*[n];
        for( int i = 0; i < n; i++ ){
            A.v[i] = new double[m];
        }
        return A;
    }
    
    Matrix newHamiltonianMatrix (int n, int m)
    {
        Matrix A = newMatrix(n, m);
        for( int i = 0; i < A.n; i++ ){
            for( int j = 0; j < A.m; j++ ){
                A.v[i][j] = 0.0;
                if( i == j ){
                    A.v[i][j] = 2.0;
                }
                if( i == j + 1 or i == j - 1 ){
                    A.v[i][j] = 1.0;
                }
            }
        }
        return A;
    }
    
    void deleteMatrix (Matrix A)
    {
        for( int i = 0; i < A.n; i++ ){
            delete [] A.v[i];
        }
        delete [] A.v;
        A.v = NULL;
    }
    
    int main ()
    {
        Matrix A = newHamiltonianMatrix(10, 20);
        for( int i = 0; i < A.n; i++ ){
            for( int j = 0; j < A.m; j++ ){
                cout << A.v[i][j] << " ";
            }
            cout << endl;
        }
        deleteMatrix(A);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm solving some problem that involves Rabin–Karp string search algorithm. This algorithm requires rolling
I am in the middle of solving a problem which requires me to do
I need some help in solving this problem. We have a large amount of
I'm having trouble solving this problem. I have to find all simple paths starting
I tried solving the following problem in haskell: Find the smallest number b with
I have tried solving this problem by posting other related questions here that focused
Any advice on solving this problem?
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
I'm solving problem of storing files in my web app. I've already done some
Continue solving this problem , i've found couple of 'org.hibernate.impl.SessionFactoryImpl' memory leaks using MAT

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.