Results 1 to 5 of 5

Thread: Dereferencing NULL pointer in C++

  1. #1
    Join Date
    Jun 2011
    Posts
    65

    Dereferencing NULL pointer in C++

    I have attended too many different interviews after my studies and I found that most of the question are frequently ask upon C++ only, thus I would say I want to know about that exactly, see my question is like with no change in the class ABC, how can we call its method ?

    Code:
    class Abc
    {
    protected:
       Abc() {}
    public:
       int summation(int j, int k) 
    {
    	return j + k;
    }
    }

  2. #2
    Join Date
    May 2009
    Posts
    539

    Re: Dereferencing NULL pointer in C++

    In this case I would say obviously you have mentioned, this is the example of , de-referencing a null pointer which gives indeterminate actions in common, though on the most implementation this source code would work as predicted. And according to this program I would say the code will return the actual value being calculated through the operator. In this a compiler severely treats member function call as normal function call.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Dereferencing NULL pointer in C++

    I would say the simplest answer to this is like this,

    Code:
    A* p = NULL;
      int s = p->sum(666, 777);
    but I think there is some problem in the question only, summation have to return integer value only but off course, in this example there is absent ‘;’ following class definition. I would say in the good manner you should always declare such a integer as a static. as it doesn't utilize any example class member.

  4. #4
    Join Date
    Jun 2011
    Posts
    65

    Re: Dereferencing NULL pointer in C++

    hey thank for the reply but actually the problem is like in fact I don’t know whether it would work or not, I though there is some mistakes in the declaration only. But as you have mentioned that the standard assures that code to work ? and if yes then I would like to know why ? and I think the better answer to the previous code would be like

    Code:
    struct { char _[ sizeof(ABC) ]; } pseudoABC;
    
    A*  p = reinterpret_cast<A*>( &pseudoABC );
    int summation = p->sum(10, 20);
    isn’t it ?

  5. #5
    Join Date
    May 2009
    Posts
    511

    Re: Dereferencing NULL pointer in C++

    Yeah the mentioned code is right but in that also I would say you have made mistake off course in the declaration because as no standard not even the AFAIK standard gives assurance to work this. So I would say you have to make some changes in the coding as I am mentioning. According to the holy standard I would be,

    Code:
    int s = (*(p)).sum(10, 20);

Similar Threads

  1. Replies: 3
    Last Post: 04-02-2012, 01:58 AM
  2. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  3. Null pointer error in java
    By Elizabeth Allen in forum Software Development
    Replies: 5
    Last Post: 19-03-2010, 02:26 PM
  4. Nokia 6300 gives "Null Pointer" error message
    By Nadiaa in forum Portable Devices
    Replies: 4
    Last Post: 08-02-2010, 10:49 PM
  5. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,615,896.45667 seconds with 17 queries