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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:28:32+00:00 2026-05-12T05:28:32+00:00

A C++ compiler that I will not name lets you take the address of

  • 0

A C++ compiler that I will not name lets you take the address of a literal, int *p = &42;

Clearly 42 is an r-value and most compilers refuse to do so.

Why would a compiler allow this? What could you do with this other than shoot yourself in the foot?

  • 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-12T05:28:33+00:00Added an answer on May 12, 2026 at 5:28 am

    What if you needed a pointer to an integer with the value of 42? 🙂

    C++ references are much like automatically dereferenced pointers. One can create a constant reference to a literal, like this:

    const int &x = 42;
    

    It effectively requires the compiler to initialize a pointer with the address of an integer with the value 42, as you might subsequently do this:

    const int *y = &x;
    

    Combine that with the fact that compilers need to have logic to distinguish between a value which has not had its address taken, and one which has, so it knows to store it in memory. The first need not have a memory location, as it can be entirely temporary and stored in a register, or it may be eliminated by optimization. Taking the address of the value potentially introduces an alias the compiler can’t track and inhibits optimization. So, applying the & operator may force the value, whatever it is, into memory.

    So, it’s possible you found a bug that combined these two effects.

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

Sidebar

Related Questions

I know that if you write void function_name(int& a) , then the function will
I have some code that compiles and runs on MSVC++ but will not compile
(1). When using C++ template, is it correct that the compiler (e.g. g++) will
How can I tell closure compiler that an anonymous function should not be removed
I have a textbox that will collect the ip address from the user. I
First, I read that: array &array &array[0] will all be the same as long
I'm tasked with choosing a name that will in effect be the internal name
I'm setting up a Cakefile that will compile and minify my CoffeeScript and minify
We are writing an app that will use T4 to generate Flex/Actionscript to compile
What windows C IDE can I use that will use gcc to compile and

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.