Segmentation Fault.

Discussion in 'C++' started by Prasad, May 4, 2006.

  1. Prasad

    Prasad Guest

    I am executing this program but, getting error Segmentation Fault.

    Can any one point out the error in this?

    #include<iostream>

    using namespace std;

    typedef struct __Node
    {
    int data ;
    struct __Node *left, *right ;
    } NODE ;
    NODE *ROOT = NULL ;

    void Insert ( NODE *root, NODE *nNode )
    {
    if ( root )
    {
    //////////////
    }
    else
    {
    *root = nNode ;
    }
    }

    void Insert ( int data )
    {
    NODE *tmp = new NODE ;
    tmp -> left = tmp -> right = NULL ;
    tmp -> data = data ;
    Insert ( ROOT, tmp ) ;
    }

    int main ()
    {
    Insert ( 10 ) ;
    cout << ROOT -> data << endl ;
    return 0 ;
    }

    Thanks in advance.
    Prasad
     
    Prasad, May 4, 2006
    #1
    1. Advertising

  2. "Prasad" <> wrote in message
    news:...
    :I am executing this program but, getting error Segmentation Fault.
    :
    : Can any one point out the error in this?
    :
    : #include<iostream>
    :
    : using namespace std;
    :
    : typedef struct __Node

    BTW: Identifiers starting by two unserscores (or an underscore and
    an uppercase letter) are reserved for use by the implementation.
    Don't use them, or your code will not be standards compliant.

    : {
    : int data ;
    : struct __Node *left, *right ;
    : } NODE ;
    All-uppercase are typically used only for preprocessors definitions
    (althouth it is not illegal to do otherwise).

    : NODE *ROOT = NULL ;

    You are never allocating this root node, therefore dereferencing
    a null pointer.
    Why not declare an actual variable instead of a pointer:
    Node root;
    Use &root when you need a pointer to the root.

    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, May 4, 2006
    #2
    1. Advertising

  3. Prasad

    Sunil Varma Guest

    Prasad wrote:
    > I am executing this program but, getting error Segmentation Fault.
    >
    > Can any one point out the error in this?
    >
    > #include<iostream>
    >
    > using namespace std;
    >
    > typedef struct __Node
    > {
    > int data ;
    > struct __Node *left, *right ;
    > } NODE ;
    > NODE *ROOT = NULL ;
    >
    > void Insert ( NODE *root, NODE *nNode )
    > {
    > if ( root )
    > {
    > //////////////
    > }
    > else
    > {
    > *root = nNode ;
    > }
    > }
    >
    > void Insert ( int data )
    > {
    > NODE *tmp = new NODE ;
    > tmp -> left = tmp -> right = NULL ;
    > tmp -> data = data ;
    > Insert ( ROOT, tmp ) ;
    > }
    >
    > int main ()
    > {
    > Insert ( 10 ) ;
    > cout << ROOT -> data << endl ;
    > return 0 ;
    > }
    >
    > Thanks in advance.
    > Prasad


    I dont which version of gcc you are using.
    But g++ complains error on,
    *root = nNode;

    Here you are trying to fill object pointed to by root with nNode
    pointer.
    Probably what you wanted is

    root = nNode.

    Regards
    Sunil
     
    Sunil Varma, May 4, 2006
    #3
  4. Prasad

    santosh Guest

    > else
    > {
    > *root = nNode ;
    > }


    *root = nNode ;
    You mean root->left or right = nNode ;


    > int main ()
    > {
    > Insert ( 10 ) ;
    > cout << ROOT -> data << endl ;
    > return 0 ;
    > }


    ROOT->data is garbage
     
    santosh, May 4, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    873
  2. Pud
    Replies:
    0
    Views:
    581
  3. Replies:
    0
    Views:
    537
  4. Ivan Vecerina
    Replies:
    0
    Views:
    489
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,627
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page