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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:54:48+00:00 2026-05-28T06:54:48+00:00

I used to think returning a reference is bad as our returned reference will

  • 0

I used to think returning a reference is bad as our returned reference will refer to some garbage value. But this code works (matrix is a class):

const int max_matrix_temp = 7;
matrix&get_matrix_temp()    
{
   static int nbuf = 0;  
   static matrix buf[max_matrix_temp];  

   if(nbuf == max_matrix_temp)    
      nbuf = 0;  

   return buf[nbuf++];
} 

matrix& operator+(const matrix&arg1, const matrix&arg2)
{  
    matrix& res = get_matrix_temp();
    //...  
    return res;
}

What is buf doing here and how does it save us from having garbage values?

  • 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-28T06:54:49+00:00Added an answer on May 28, 2026 at 6:54 am

    I see no buf declared anywhere, which means it doesn’t go out of scope with function return, so it’s okay. (it actually looks like it’s meant to be matrixbuf which is also fine, because it’s static).

    EDIT: Thanks to R. Martinho Fernandes for the guess. Of course it is matrix buf, so it makes buf static array in which temporary is allocated to make sure it doesn’t get freed when the function returns and therefore the return value is still valid.

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

Sidebar

Related Questions

So... I used to think that when you accessed a file but specified the
I'm not very used to programming with flags, but I think I just found
This is the first time I've used a thread that requires returning values to
I have some code where I'm returning an array of objects. Here's a simplified
Out of curiosity...what happens when we call a method that returns some value but
I think that it used to the be the case that in Liferay 4,
Yesterday I used jQuery UI for the first time and I think I'm going
I'm used to doing Java programming, where you never really have to think about
I used Collections.sort(playersList); to sort a List . So, I think playersList is sorted
I used Term::ShellUI and almost every thing is working as expected but the issue

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.