Seg Fault Problem

This is a discussion on Seg Fault Problem within the C++ Programming forums, part of the General Programming Boards category; I am having a problem with a segmentation fault in my program. Basically the program is a red black tree ...

  1. #1
    Registered User
    Join Date
    Mar 2002

    Seg Fault Problem

    I am having a problem with a segmentation fault in my program. Basically the program is a red black tree implementation. The seg fault happens at the same point in my code no matter what I insert and it's always after the third insertion, which causes a double red. I am using trinode restructuring to fix the double red.
    Below is the code for insertion.

    // Inserts a new key,element pair
    RedBlackTree::insert( void * key, void * element)
            RedBlackNode * parent=NULL;
            RedBlackNode * curr=_root;
            int result=0;
            while (curr!=NULL){
                    if (result==0){
                    else if (result>0){
            }//end while loop
            RedBlackNode * n=new RedBlackNode(key,element,RED,NULL,NULL,_id,parent);
            if (parent==NULL){
            if (result>0){
            if (parent->_color==RED){
    And here is the code that seg faults for the restructure

    RedBlackTree::fixDoubleRed(RedBlackNode * node){
            for (int d=0;d<32000;d++){for (int j=0;j<500;j++){}}
            RedBlackNode * A;
            RedBlackNode * B;
            RedBlackNode * C;
            RedBlackNode * x=node;
            RedBlackNode * y=node->_parent;
            RedBlackNode * z=node->_parent->_parent; // seg fault
            printf("Finish Allocation of ABCxyz\n");
            if (node->_parent->_parent->_right!=node->_parent){ //node's parent is $
                    if (node->_parent->_parent->_right->_color==BLACK)
    The line that seg faults is the RedBlackNode *z=....
    I am trying to input 1,then 7, then 4 as keys.
    Anyone got any ideas why this line seg faults? Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    A segmentation fault is when you dereference a bad pointer, whether it be NULL or some random location in memory that you don't own. The debugger is your friend, step through the code and find out why that line is accessing memory it doesn't own, then fix it.

    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    C++ code moved to the C++ board

    > RedBlackNode * z=node->_parent->_parent;
    But if node is already the root of the tree, where does this take you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault with any function call
    By LegoMan in forum C Programming
    Replies: 5
    Last Post: 04-15-2009, 05:30 PM
  2. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  5. Replies: 3
    Last Post: 10-15-2008, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21