How this can be possible ?

W

WantToKnowMore

I'm calling a function with a class pointer without assigning to any
class object. Still it executes the function.
And note that with VS 2005 compiler its giving 2 different results on
2 different PCs. In one PC it gives run time error (expected) and in
another PC it executes !

#include <iostream>
using namespace std;

class foo
{
public:
void greet(){cout << "Hello" << endl;}
};
int main()
{
foo* f;
f->greet(); // In one PC this function getting executed with printing
"Hello". How ???

return 0;
}
 
M

Michael Doubez

I'm calling a function with a class pointer without assigning to any
class object. Still it executes the function.


From 5.2.5/3, f->greet() is equivalent to (*f).greet(). You are
deferencing an unitialized pointer, this is UB so everything can
happen.

For your compiler, I guess the expression is equivalent to a free
function call because 'this' is passed as a hidden parameter and not
used in the function.
 
R

Ron

From 5.2.5/3, f->greet() is equivalent to (*f).greet(). You are
deferencing an unitialized pointer, this is UB so everything can
happen.

Yep, one of the insidious modes of undefined behavior is that it
silently
appears to work fine TODAY. Tomorrow or when you least suspect it
may
be a different story.
 
J

Juha Nieminen

WantToKnowMore said:
I'm calling a function with a class pointer without assigning to any
class object.

Member functions are not stored inside the object. They are a bit like
normal functions, just silently taking an additional parameter (the
object in question).

Of course if the member function in question tries to access the
uninitialized object, you will get a crash or worse.
 
J

Joshua Maurice

  Member functions are not stored inside the object. They are a bit like
normal functions, just silently taking an additional parameter (the
object in question).

  Of course if the member function in question tries to access the
uninitialized object, you will get a crash or worse.

Though virtual members are stored inside the object, quote unquote,
for the common implementation, so if "greet" was declared virtual,
then I would expect the OP's program to crash.
 
M

Michael Doubez

Though virtual members are stored inside the object, quote unquote,
for the common implementation, so if "greet" was declared virtual,
then I would expect the OP's program to crash.

I would not: in the example, the compiler knows the underlying type
and therefore is not required to use the virtual call. I expect that
in practice, it doesn't and simply directly call the most derived
member function.
 
J

Joshua Maurice

I would not: in the example, the compiler knows the underlying type
and therefore is not required to use the virtual call. I expect that
in practice, it doesn't and simply directly call the most derived
member function.

I think you greatly over-estimate the effectiveness of current C++
compilers. I would guess that most do not do such program analysis to
inline virtual functions, nor use a virtual lookup cache, etc. At
least, Visual Studios 2008 does not, nor g++ version 4.1.2. Both
produced programs from the following source which crashed / seg
faulted when compiled with the high optimizations (-O3 for g++, and
equivalent options for Visual Studios):
#include <iostream>
using namespace std;
class foo
{
public:
virtual void greet(){cout << "Hello" << endl;}
};
int main()
{ foo* f = 0;
f->greet();
}
 
J

Juha Nieminen

Joshua said:
Though virtual members are stored inside the object

Well, not really. Virtual members (well, pointers to them, to be
exact) are stored in an external table (which is shared by all the
objects of that type) and what is stored in the object is only a pointer
to that table.

The effect is basically the same, though.
 
J

Joshua Maurice

  Well, not really. Virtual members (well, pointers to them, to be
exact) are stored in an external table (which is shared by all the
objects of that type) and what is stored in the object is only a pointer
to that table.

  The effect is basically the same, though.

Though virtual members are stored inside the object, quote unquote,

From http://idioms.thefreedictionary.com/quote,+unquote
quote, unquote (British, American & Australian) also quote, end quote (American)
something that you say when you want to show that you are using someone else's phrase, especially when you do not think that phrase is true

Thank you for attempting to correct me with a great out of context
quote (by snipping off the next two words). I already said what you
were trying to correct, so we're both right, but you made it sound as
though I was wrong, and I take some offense to that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top