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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:59:23+00:00 2026-06-03T05:59:23+00:00

This happens when compiling the following code in Visual Studio 2010. My question is:

  • 0

This happens when compiling the following code in Visual Studio 2010. My question is: the C++ compiler will warn if a function return the address of a local variable, but why doesn’t it warn when returning a local reference to a local variable?

Is it still wrong (returning a local reference to a local variable), but just that the compiler couldn’t detect it? Examining the addresses of ‘num’ and ‘r’ reveal that they share the same memory location.

#include <iostream>  
using namespace std;

int & intReference() {
  int num = 5;
  int &r = num;
  cout << "\nAddress of num: " << &num;

  //return num; // Compiler warning: C4172: returning address of local variable or temporary
  return r; // No warning?
}

void main() {
  int &k = intReference();
  cout << "\nk = " << k;  // 5
  cout << "\nAddress of k: " << &k; // same address as num

  char c;
  cin.get(c);
}
  • 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-03T05:59:25+00:00Added an answer on June 3, 2026 at 5:59 am

    Yes it’s still wrong.

    The compiler cannot detect all cases where you’re doing dangerous (or illegal) things. It warns when it does find them, but it can’t identify all cases (and doesn’t have to).

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

Sidebar

Related Questions

How will the Delphi compiler compiles the following code ; uses a_big_unit; procedure TForm1.Button1Click(Sender:
When compiling a win32 console project with GLFW in Visual Studio 2010 SP1, I
Sometimes users mistakenly redirected to ?Process=ViewImages&PAGEID=. When this happens, they get the following error.
This morning I stumbled over a weird issue in Visual Studio's Windows Forms designer.
This happens after I hit Build and Go to run my app on my
This happens in IDLE and Windows 7 RC1 (if that helps). Here is the
I am authenticating users in ldap, but this happens only once, when user is
I need to catch segmentation fault in third party library cleanup operations. This happens
MacPorts installed libiconv @1.14_0+universal as a dependency on my system. This happens to be
This only happens in some IE's. Here: http://animactions.ca/Animactions/volet_entreprise.php You may notice that when you

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.