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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:31:46+00:00 2026-05-23T21:31:46+00:00

int& fun() { int * temp = NULL; return *temp; } In the above

  • 0
int&  fun()
{
    int * temp = NULL;
    return *temp;
}

In the above method, I am trying to do the dereferencing of a NULL pointer. When I call this function it does not give exception. I found when return type is by reference it does not give exception if it is by value then it does. Even when dereferencing of NULL pointer is assinged to reference (like the below line) then also it does not give.

int* temp = NULL:
int& temp1 = *temp;

Here my question is that does not compiler do the dereferencing in case of reference?

  • 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-23T21:31:47+00:00Added an answer on May 23, 2026 at 9:31 pm

    Dereferencing a null pointer is Undefined Behavior.

    An Undefined Behavior means anything can happen, So it is not possible to define a behavior for this.

    Admittedly, I am going to add this C++ standard quote for the nth time, but seems it needs to be.

    Regarding Undefined Behavior,

    C++ Standard section 1.3.24 states:

    Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

    NOTE:
    Also, just to bring it to your notice:
    Using a returned reference or pointer to a local variable inside a function is also an Undefined Behavior. You should be allocating the pointer on freestore(heap) using new and then returning a reference/pointer to it.

    EDIT:
    As @James McNellis, appropriately points out in the comments,
    If the returned pointer or reference is not used, the behavior is well defined.

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

Sidebar

Related Questions

Possible Duplicate: return type in c++ #include<iostream> int& fun(); int main() { int p=fun();
template <class T> bool BST<T>::search(const T& x, int& len) const { return search(BT<T>::root, x);
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
void foo(void **Pointer); int main () { int *IntPtr; foo(&((void*)IntPtr)); } Why do I
I have a simple class like this: class A { public: void f(const int&
I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a
Can anyone parse this following expression for me #define Mask(x) (*((int *)&(x))) I applied
Suppose you have a function: void fun(int *a, int *b); Which way you would
#include<iostream> int & fun(); int main() { int p = fun(); std::cout << p;
The number is bigger than int & long but can be accomodated in Decimal

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.