0

Sample code:

#include <iostream>
using namespace std;

class Base
{
public:
    void fun(){
        cout<<"Base fun()\n";
    }
    void display(){
        cout<<"Base display() function\n";
    }
};

int main()
{
    Base *b; //Just a pointer to a base class
    b->fun(); //how we are able to access fun() here?
    b->display(); //how we are able to access display() here?
    return 0;
}

Output:

Base fun()                                                                                                            
Base display() function

Questions:

  1. How we are able to access the member functions of the base class using just a pointer to a base class?
  2. Can we access the members of a class without instantiating using 'new' operator? What happens internally?
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Srihari
  • 46
  • 6
  • 1
    Answer to both questions is "Undefined Behaviour". Anything can happen. – Yksisarvinen Jul 29 '20 at 15:04
  • It is called undefined behavior – Tony Tannous Jul 29 '20 at 15:05
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Jul 29 '20 at 15:09
  • If you make those member functions [`static`](https://en.cppreference.com/w/cpp/language/static), you can call them like so: `Base::fun()` – G. Sliepen Jul 29 '20 at 15:11
  • Uninitialized pointer is much worse than a pointer set to `NULL`. At least a pointer set to `NULL` can be checked for being `NULL`, and, in practice, using it is much more likely to crash, then to silently corrupt your memory. – Dima Jul 29 '20 at 15:12
  • I would not close this question. It is different from the case with having a null pointer, and it is very important to be able to instantly recognize this bug. – Dima Jul 29 '20 at 15:28
  • I encountered this problem when I didn't initialize the Base class pointer with anything and tried to access the member functions of Base class, so it is different from the null pointer usecase. So, how we are getting the desired output with a garbage pointer here? Is it because the compiler actually links the address of member functions during compilation? If yes, how a compiler can actually link the address of a Base class's member function or base class code segment region to Base class pointer? How the code will be copied to the heap to access using the Base class pointer? – Srihari Aug 01 '20 at 19:01

1 Answers1

0

No! What you have there is an uninitialized pointer, which is a very bad bug.

Base *b;

declares a pointer to an object of class Base. A pointer is an address of a memory location. When a pointer is uninitialized, it contains an address, but you have no way of knowing what that address is. So, if you use this pointer, as in your example, the behavior is undefined.

Most likely the pointer contains an address of a part of memory that you are not allowed to access, and running your code will cause a segmentation fault. This is actually the best case scenario.

In the worst case, the pointer points to some valid memory, which will be interpreted as an object of class Base. Then, if you modify that memory, you would be corrupting it, causing errors which are very hard to track down.

Occasionally, this bug may have no ill effects. In your case, your class has no data members, and so the member functions you call do not modify the object or access any of its data. However, if you modify this class to have some data, and add functions that modify it, you are in for a nasty surprise.

When you declare a pointer, you should make it point to something. You can make it point to a new object on the heap by calling new, or you can set to be an address of an existing object using & (address of) operator. Alternately, you can set it to nullptr, so that you can later check it for being ``NULL`.

Better yet, avoid using raw pointers, and use smart pointers instead.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • What makes me curious is that he claims he got the desired output instead of nothing or a segfault. I can't think of a reason why the two cout's would actually run. Is this some sort of optimization happening that causes this code to work somehow? – Eric Jul 29 '20 at 15:15
  • 1
    In this case, the class has no data members to access or modify. So no seg fault. The functions you call are ignoring the `this` pointer which is passed in under the hood. – Dima Jul 29 '20 at 15:17