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

  • SEARCH
  • Home
  • 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 1037989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:55:06+00:00 2026-05-16T14:55:06+00:00

Is it OK to return a reference to a local variable from a function?

  • 0

Is it OK to return a reference to a local variable from a function? By local I mean that the variable would be created(on the stack i.e. without using new) within the function and its scope is within that function only. I got contradictory answers when I searched about this. 1) says that kind of usage is correct but 2) contradicts it.

1) http://functionx.com/cpp/examples/returnreference.htm
2) http://www.cprogramming.com/tutorial/references.html (under References & Safety section)

Which of them is right?

Another question I had is, if 1) is right then are the following serve same purpose.

i) int& a = func();
ii) int a = func(); where func() returns a reference to an int (local variable in that function).

In both the above cases, there is no copying of return value involved isn’t it. I would like to prevent copying of return values since the return value could be large.

Thank you in advance.

Raghava.

  • 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-16T14:55:06+00:00Added an answer on May 16, 2026 at 2:55 pm

    As everybody else is saying, don’t do that. Returning a reference or pointer to a local variable is always wrong, because the act of returning gets rid of the local variable and hence the reference or pointer is automatically invalid.

    Copying may not be an issue. C++ compilers are allowed to skip copy constructors when returning from functions (the “return value optimization”), so a sufficiently smart compiler might be able to build the value in place. Therefore, you may well be able to return a large value without any copying. Try it and see; you can temporarily put output statements in the copy constructor (if you’ve written one and aren’t using the automatically generated one) to see if it’s actually called.

    So, without running and trying, you don’t know whether there is actual copying going on, and if so how much of a problem it is. As always, time and profile a run to see if there is a problem and, if so, where. Doing anything risky and/or confusing to speed up performance is almost never worth doing before timing and profiling.

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

Sidebar

Related Questions

Why is it that I cannot/shouldn't return the reference to a local variable to
If you return a value (not a reference) from the function, then bind it
I have some questions on returning a reference to a local variable from a
Let's say that some function returns object reference. How one should properly spell: this
I have a function defined a local variable typed in CStringW, is it safe
There's an advice in C++: Never Return a Reference to a Local Object, as
I'm writing some functions that manipulate strings in C and return extracts from the
I'm trying to return a reference to an object, not pass it by value.
should a getter of a private property return a reference to the property or
If I have a function which returns a reference cursor for a query, how

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.