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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:03:42+00:00 2026-05-14T09:03:42+00:00

Suppose I have a rather large class Matrix , and I’ve overloaded operator== to

  • 0

Suppose I have a rather large class Matrix, and I’ve overloaded operator== to check for equality like so:

bool operator==(Matrix &a, Matrix &b);

Of course I’m passing the Matrix objects by reference because they are so large.

Now i have a method Matrix::inverse() that returns a new Matrix object. Now I want to use the inverse directly in a comparison, like so:

if (a.inverse()==b) { ... }`

The problem is, this means the inverse method needs to return a reference to a Matrix object. Two questions:

  1. Since I’m just using that reference in this once comparison, is this a memory leak?

  2. What happens if the object-to-be-returned in the inverse() method belongs to a boost::shared_ptr? As soon as the method exits, the shared_ptr is destroyed and the object is no longer valid. Is there a way to return a reference to an object that belongs to a shared_ptr?

  • 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-14T09:03:42+00:00Added an answer on May 14, 2026 at 9:03 am

    You do not need to return a reference from the inverse() method. Return the object itself. The compiler will create a temporary reference for passing to the equality operator, and that reference will go out of scope immediately after the operator returns.


    To answer your question whether or not it’s a memory leak.

    Depends on where you’re going to get that object that you’re going to return from inverse(). If you’re returning a reference to an object allocated on the heap, like so:

        Matrix& inverse()
        {
            Matrix* m = new Matrix();
            return *m;
        }
    

    then that’s definitely a leak. Indeed, you’re never going to free that memory, are you?
    If you’re returning a reference to a stack-allocated object, like so:

        Matrix& inverse()
        {
            Matrix m;
            return m;
        }
    

    then I wouldn’t say it’s a leak… Instead, it’s a crash. A General Protection Fault, if you will. Or a memory corruption. Or something else out of a nightmare. Don’t do this. Ever.
    A stack-allocated object like that goes out of scope when the function returns, and that memory is reclaimed. But what’s worse, it’s reclaimed for the purposes of calling other functions, and allocating those functions’ local variables. Therefore, if you retain a reference to a stack-allocated object, then you’re pretty much screwed.

    And finally, you might use some kind of custom storage for that Matrix, like so:

        static Matrix _inversed;
    
        Matrix& inverse()
        {
            _inversed = ...
            return _inversed;
        }
    

    Technically, this wouldn’t constitute a leak or a crash. But you really don’t want to do it either, because it’s not clear from the signature of the inverse() method that it actually returns a reference to shared instance, which will make it all too easy to forget this, and to fiddle with those “inversed” matrices, screwing up your data.

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

Sidebar

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.