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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:23:21+00:00 2026-06-12T12:23:21+00:00

I use the following code Matrix operator * (const Matrix & obj) { Matrix

  • 0

I use the following code

Matrix operator * (const Matrix & obj)
{
    Matrix m = Matrix();

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
        {
            m[i][j] = 0;

            for (int k = 0; k < 4; k++)
                m[i][j] += _data[i][k] * obj._data[k][j];
        }

    return m;
}

like this

v1 = m_proj * m_view * m_object * v1;

But I guess it is very unoptimized since I presume the new Matrix I create inside the operator is being copied around like crazy. If I do this though

Matrix & operator * (const Matrix & obj)
{
    Matrix m = Matrix();

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
        {
            m[i][j] = 0;

            for (int k = 0; k < 4; k++)
                m[i][j] += _data[i][k] * obj._data[k][j];
        }

    return m;
}

Change the return type of the operator to a reference to Matrix the whole code stops working altogether (it compiles, just the matrices are not being multiplied as they should).

If I change it to

Matrix & operator * (const Matrix & obj)
{
    Matrix & m = * new Matrix();

    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
        {
            m[i][j] = 0;

            for (int k = 0; k < 4; k++)
                m[i][j] += _data[i][k] * obj._data[k][j];
        }

    return m;
}

Now it works but I have a bad memory leak.

So how do I solve this problem? Is there an elegant solution?

Thanks!

  • 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-12T12:23:22+00:00Added an answer on June 12, 2026 at 12:23 pm

    Your function is creating a new object, and it should return the object by value.

    You cannot return references to a local variable, and returning references to dynamically allocated memory has multiple issues: it solves nothing (does not reduce the number of objects created by the expression, and makes each object more expensive to create due to the dynamic allocation) and adds memory leaks.

    Without the definition of the class Matrix it is not clear whether the memory is handled in an array or it is dynamically allocated. If it is dynamically allocated and you have a C++11 compiler, you can implement move construction and move assignment and the cost of all those copies will go away.

    In C++03 (and also in C++11) you can implement operator*= and manually handle the objects that get created (assuming that you can do this in place efficiently):

    //v1 = m_proj * m_view * m_object * v1;
    Matrix tmp = m_proj;
    tmp *= m_view;
    tmp *= m_object;
    tmp *= v1;
    v1 = tmp;
    

    This will create a single temporary and apply all the multiplications in place, reducing the number of copies.

    At any rate, I would not spend too much time with this, as a 4×4 matrix is not that expensive to copy.

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

Sidebar

Related Questions

I'd like to use the following function to return a transposed pointer to a
I have the following code in the caller function int matrix[3][3] = {{1, 2,
I have several blocks of the following code that each use there own matrix.
I use following code: Create a retry policy, when error, retry after 1 second,
I just use following code to add an image to my project, var paper
I use VS2010, C# to develop Silverlight 4 app, I use following code in
I have an array of objects, and i use following code to get in
I use the following code to crop an image according to a rectangular selection
I use the following code to copy a particular directory and its contents to
I use the following code to fill all empty keys in sub-arrays with ``

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.