Why it works a dereference to a null object?

Good day!
Why runs this code? trash is a pointer that points to garbage - the idea is when you call trash->f() must fall. But f() is successfully called. There is speculation that global and contact classes only through this.
struct A
{
 void f() { std::cout << "Hello, world!" << std::endl };
};

void main()
{
 A * trash;
trash->f();
}
April 3rd 20 at 17:37
2 answers
April 3rd 20 at 17:39
Solution
A pointer to an object actually contains the trash.
But turning therefore to the function, you would call a normal function, which is passed in the parameters of the this pointer (which contains garbage). But since this function does not use, and the problems do not arise.
Try to add a member of structure and print it in f() - all will fall.
April 3rd 20 at 17:41
A * trash = nullptr;
Accustom to initialize the variables with the young :)
It is the task with the interview. - clay commented on April 3rd 20 at 17:44
@clay, The more! In itself, the name of the variable suggests. In such situations they say: "first of all initialize the pointer to null, and now let's look further..." - Greyson17 commented on April 3rd 20 at 17:47

Find more questions by tags C++