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 learned that compiler will expand macros while compiling. Templates are also expanded at
I know that the compiler will sometimes initialize memory with certain patterns such as
I'm building a compiler that targets .NET and I've previously generated CIL directly, but
In C and C++ you can tell the compiler that a number is a
Just wondering: When I add restrict to a pointer, I tell the compiler that
Delphi has a $WARN compiler directive that allows one to selectively enable or disable
If you code in C and configure your compiler to insist that all functions
Intuitively, it would seems that a compiler for language Foo cannot itself be written
Compiler error CS0283 indicates that only the basic POD types (as well as strings,
I realize that far is compiler specific, but my expectation is that the placement

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.