0
#include <iostream>
#include <string>
using namespace std;
class Check
{
public:
    int a{};
    string b{};
    float c{};
    void print()
    {
        cout << "What!";
    }
    Check()
    {
        cout << "Constructor has called";
    }
    ~Check()
    {
        cout << "Destructor has called ";
    }
};
int main()
{
    Check* ptr{};
    ptr->print(); /*How and why is it working without any error or undefined behaviour even though i did not store an address of object in it */
    return 0;
}

I am using Visual Studio 2019 updated version

According to my knowledge, we can access the members of the class through pointers by making pointer to the class and then store the address of an object of that class type, and then we can access the members through '->' using that pointer

Pro Boy
  • 5
  • 4
  • 2
    Why do you think it's not undefined behaviour? It's only the worst case of UB, where it appears to work. – Lukas-T Jan 01 '21 at 10:35
  • Technically what you do is UB. Since `print()` doesn't do anything with the members of the class it is reasonable to expect that it'll just do the printing even if the object is garbage. This is meaning of UB that there are no guarantees what will happen - a somewhat correct behaviour might occur as well. – ALX23z Jan 01 '21 at 10:39
  • Undefined behaviour means the behaviour is undefined, it does not mean that it will not 'work', or that it must be an error. Defined here means *defined by the C++ standard*. When your program has undefined behaviour then **anything** can happen and your compiler hasn't broken any rules of the C++ standard. – john Jan 01 '21 at 10:40

2 Answers2

1

According to my knowledge, we can access the members of the class through pointers by making pointer to the class and then store the address of an object of that class type, and then we can access the members through '->' using that pointer

That is correct. Though, unless you need to, you would not use a pointer, but just the object itself:

Check ptr;
ptr.print();

How and why is it working without any error ...

You do not get an error, because dereferencing a null pointer is undefined behavior. Compilers may emit a warning in cases they can detect the problem, but they are not required to issue an error (and in general they cannot).

... or undefined behaviour even though i did not store an address of object in it

You code does have undefined behavior. Undefined behavior means that you get no guarantee that your code will do the right thing. You do not get a guarantee that your code will do something wrong.

Behind the scenes the this pointer is passed as implicit parameter to member functions. As print is not actually using any members of the class, the pointer isn't used and your code appears to work. However, this is just by chance. By the rules of the language ptr->print(); is already undefined behavior (no matter if print does use members or not).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Most C++ ABIs place the this (the pointer you are dereferencing) as an implicit first parameter. In your print method call, you never refer to this (your function makes no reference to object members) that is why your program does not crash. If reference an object member or make print virtual your method will crash is this is null.

doron
  • 27,972
  • 12
  • 65
  • 103