Possible Duplicate:
A reference can not be NULL or it can be NULL?
How does the below program compile successfully?
int main()
{
int *ptr = NULL;
int &ref = *ptr;
return 0;
}
See it on ideone.
Can we define reference to NULL?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
The code you’ve given will compile, because the compiler doesn’t check the value of the pointer at compile-time. However dereferencing a NULL pointer is undefined behavior.
This is a problem that I ran into once in the past and was burned into my memory. My further thoughts can be found here: https://stackoverflow.com/a/57656/5987