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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:54:29+00:00 2026-05-29T05:54:29+00:00

As we all know there are no null references. So I try to compile

  • 0

As we all know there are no null references.
So I try to compile this line of code

int main{
    int &surprise_reference=*(int*)0;  
    return 0;}  

To my surprise this code works without a single warning .
Then I change my code to

int  main()   {
 int&suprise_reference=*(int*)0;  
 if(&suprise_reference==0)  
 std::cout<<"you don't know references";  
          return  0;
}

Output of program is you don’t know references.
Does it mean that dereferencing a null pointer can lead to null references also?
Please help..

  • 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-29T05:54:30+00:00Added an answer on May 29, 2026 at 5:54 am

    What your experiment shows is that it happens to work on your particular computer. That doesn’t mean it’s “ok”.

    In particular, this line:

    int &surprise_reference=*(int*)0;
    

    is invalid C++ code, because (exactly as you said) a reference is not allowed to point to a NULL pointer. The result of doing something that’s not allowed by the C++ standard is undefined behavior. In this particular case, on your particular computer, the undefined behavior happens to be pretty benign, but that doesn’t make it predictable, or always OK.

    See https://isocpp.org/wiki/faq/references#refs-vs-ptrs for more details. One thing that they point out in particular is that, because the compiler knows that creating a null reference is illegal, it may assume that you haven’t done it — for instance, if you have a function that takes p as a parameter, and you write

    int &q = *(int*)p;
    if (p == NULL)
      ...
    

    it may conclude from the reference creation that p is known to be non-NULL, and optimize away the check. Then, if you pass in a NULL pointer for p, things may go badly wrong.

    (That sort of assumption-and-optimization, by the way, is how a lot of the bad things from undefined code happen.)

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

Sidebar

Related Questions

I know there is a standard behind all C compiler implementations, so there should
First of all: I do know that there are already many questions and answers
Does anybody know if there is a way using java to pass all the
There seems to be too many attributes/parameters in CSS... I want to know all
All I want to know is if there's an easy way to add lines
Are there any open-source libraries that all programmers should know about? I'm thinking something
There are many things that all programmers should know, but I am particularly interested
I know that technically all three ways below are valid, but is there any
I don't know if I should test my @Entity-annotated Pojos. After all, there are
I wish to know all the pros and cons about using these two methods.

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.