Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual function and segmentation fault

Reply
Thread Tools

virtual function and segmentation fault

 
 
asit
Guest
Posts: n/a
 
      09-25-2011
Look at the following code snippet..


#include <iostream>

using namespace std;

class ex
{
int i;
public:
ex(int ii=0):i(ii) {}

~ex() { cout<<"dest"<<endl; }

virtual void show()
{
cout<<"show fun called"<<endl;
}

};

int main()
{
ex *ob = NULL;
ob->show();
return 0;
}


The output here is segmentation fault.

If I make show non-virtual, there is no segmentation fault.

Can somebody explain me the reason ??
 
Reply With Quote
 
 
 
 
asit
Guest
Posts: n/a
 
      09-25-2011
Is only one virtual table created for a class or different tables are created for different classes ??
 
Reply With Quote
 
 
 
 
Richard Damon
Guest
Posts: n/a
 
      09-25-2011
On 9/25/11 10:58 AM, asit wrote:
> Look at the following code snippet..
>
>
> #include<iostream>
>
> using namespace std;
>
> class ex
> {
> int i;
> public:
> ex(int ii=0):i(ii) {}
>
> ~ex() { cout<<"dest"<<endl; }
>
> virtual void show()
> {
> cout<<"show fun called"<<endl;
> }
>
> };
>
> int main()
> {
> ex *ob = NULL;
> ob->show();
> return 0;
> }
>
>
> The output here is segmentation fault.
>
> If I make show non-virtual, there is no segmentation fault.
>
> Can somebody explain me the reason ??


When show() is virtual, in order to locate the function to call, the
code needs to access the "vtable" to find the address of the function,
and since the object pointer is NULL, this will fault.

When show() isn't virtual, the compiler generally doesn't need to look
at the object to call the function, and since your implementation of
show doesn't refer to any member variables, it MIGHT be able to run
without problems (but it still has invoked "Undefined behavior" (which
sometimes does what you want).
 
Reply With Quote
 
Richard Damon
Guest
Posts: n/a
 
      09-25-2011
On 9/25/11 2:04 PM, Leigh Johnston wrote:
> Do you think that explaining the behaviour of particular undefined
> behaviour invocations is useful? The advice should be to avoid undefined
> behaviour, period. Explaining how things work (such as dynamic dispatch)
> can be done without resorting to explaining it in terms of undefined
> behaviour.
>
> /Leigh


Yes I do. The OP obviously has tried both options and is puzzled by the
difference in behavior (not noticing that both were undefined behavior).
By pointing out how some undefined behavior typically operates, it does
a couple of thing.

One, it points out that just because something "works" doesn't mean you
haven't committed undefined behavior (note that my description of the
non-virtual case doesn't say it WILL work, just why it might).

Two, understand why some behaviors are defined as "undefined" helps give
reality to the concept. Things are not undefined behavior just because
the writers of the standard didn't want to define it, but there is
almost always a real reason why it is impractical to define the behavior
on all implementations. Knowing this, it makes it easier to remember
what is undefined behavior, as well as to understand code that might
invoke what is undefined behavior in the C++ standard but may be defined
by another standard or the implementation.

For example, the code:

int i;
i = 30000 + 30000;

has possibly invoked undefined behavior by possible overflowing signed
integer arithmetic (if ints are only 16 bits). Knowledge that you are on
a 32 bit machine, or that (as is common), the behavior of in integer
arithmetic is reasonably well defined on this platform, allows one to be
less paranoid in the checks that need to be made to avoid/detect problems.
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      09-25-2011
Leigh Johnston <> writes:
> Do you think that explaining the behaviour of particular undefined
> behaviour invocations is useful?


Sure; concrete examples are often a very useful tool for understanding.

-miles

--
Carefully crafted initial estimates reward you not only with
reduced computational effort, but also with understanding and
increased self-esteem. -- Numerical methods in C,
Chapter 9. "Root Finding and Nonlinear Sets of Equations"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implement my own lib function fprintf --- Segmentation Fault eliweiq001 C Programming 12 10-05-2010 11:46 PM
Segmentation Fault using the srand function jtagpgmr C Programming 11 06-27-2006 06:59 PM
Function pointer results in segmentation fault. Chris Van Extergem C Programming 5 05-02-2006 08:28 PM
calling a function (passed as a pointer) causing segmentation fault sandwich_eater@hotmail.com C++ 1 07-22-2005 07:58 PM
Segmentation fault when function ends... Stefan Strasser C++ 14 03-30-2005 07:48 PM



Advertisments