As null pointer points to nothing. So, How does the following code works
#include <stdio.h>
class cb
{
public:
void HelloB()
{
printf("Calling cb\n");
};
};

int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}

Dani AI

Generated

Quick expert summary: the code compiles but the behavior is undefined. Calling a non‑static member function through a null pointer does not have a defined result in C++; it might "work" on one compiler/architecture and crash or do something bizarre on another. (en.cppreference.com)

Why it sometimes appears to work: for built‑in pointers a->m is equivalent to (*a).m, and many compilers implement non‑virtual member calls by generating a direct call that simply receives the pointer as a hidden this parameter. If the function never uses this you may see the call succeed—still, the C++ rules say this is undefined behavior and optimizers can exploit that assumption. (en.cppreference.com)

Practical, safe fixes (pick one that fits your design):

  • Make the function static or a free function if it does not need object state.
  • Check the pointer before calling (if (p) p->foo();).
  • Use references (when an object must exist) or smart pointers that encode ownership and intent.

Example (safe alternative):

struct S {
    static void hello() { puts("hello"); }   // call without an instance
    void instance() { puts("instance"); }
};

int main() {
    S::hello();           // safe
    S* p = nullptr;
    if (p) p->instance(); // check before use
}

As correctly noted, the standard classifies this as undefined behavior; ’s reminder that ptr->member is essentially (*ptr).member is a helpful way to see why dereferencing matters; and ’s later clarification matches this: don’t rely on the apparent “works on my machine” result. For the formal wording and more details see the C++ language reference on pointers and undefined behavior. (en.cppreference.com)

Recommended Answers

All 6 Replies

As null pointer points to nothing. So, How does the following code works
#include <stdio.h>
class cb
{
public:
void HelloB()
{
printf("Calling cb\n");
};//This semicolon should not be here...
};

int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}

Whereas crashing of program is concern(it won't..no reason)...it doesn't matter if u initailise it will 0.....member function will be called depending on the type of the pointer

You can even write

cb* pb=(cb*)10;

//although it doesn't make any sense..still the code wil will work

Sunny,
but it is null ptr and null ptr cant be dereferenced . Also, they cant be used for calling functions. A null pointer is known not to point to any object or function.
It is not the initialization, it is saying that ptr is null.
Now look into the problem again. :)

but it is null ptr and null ptr cant be dereferenced .

Neither in your program nor the example which i gave you derefrenced null pointer.

Also, they cant be used for calling functions.

I guess here i don't need to prove anything as it does call the member function...try running it and check it out

A null pointer is known not to point to any object or function.It is not the initialization, it is saying that ptr is null.

It is a pointer of type class cp..which can be passed null pointer or address of any other object.

Example

cb obj;
cb* pb = 0;
pb->somememberfunction();
pb=&obj;
pb->somememberfunction();

Both will work

it will compile, but dereferencing a null pointer like that will often (but maybe not always) crash the program -- it's undefined behavior. In the case of c++ classes, the function will be called with an invalid "this" pointer and any attempt to reference or use class objects will also result in undefined behavior. So "it works" is all relative to this undefined behavior, and won't "work" very long!

Neither in your program nor the example which i gave you derefrenced null pointer.

actually you do dereference the null pointer...

PT* ptr = 0;
ptr->whatever();

is the same as

PT* ptr = 0;

(*ptr).whatever();

Sorry about that...yes it does derefrence the null pointer.....as far as working of code is concerned...what you are doing is wrong

Almost always when a program does something wrong like this, the C++ standard does NOT says that the program must crash, its does NOT say that the program must produce an error message, what it says is that the program has UNDEFINED BEHAVIOUR.

UNDEFINED BEHAIOUR means exactly what it says, anything could happen,
including the program working. If you ran this program on a different
computer, or with a different compiler or even on a different day of the
week you might get different behaviour.

Most compilers will not crash untill you access a member variable
within the function.

In other Words,
Since the member function in this case does not use its "this"
pointer for anything.....therefore it will work most of the time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.