quadtree structure segmentation fault

This is a discussion on quadtree structure segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi this is my first post here and i'm fairly new to C programming. So i'm trying to make a ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Unhappy quadtree structure segmentation fault

    Hi this is my first post here and i'm fairly new to C programming.


    So i'm trying to make a quadtree, and included in a header file is the node structure as follows:

    Code:
     typedef struct tnode *tree; 
      
     struct tnode 
     { 
         tree *P; 
             tree *NW; 
         tree *NE; 
         tree *SE; 
         tree *SW; 
         char colour; 
         int size; 
         int x; 
         int y; 
     };
    Also in the header file is my function declaration for making the quadtree:


    Code:
    tree makequadtree(tree node, int array[MAXSIZE][MAXSIZE], int size);
    In my C file, I have called the function as follows:


    Code:
    makequadtree(node, my_2D_array, size);
    However my quadtree function gives me a segmentation fault. I have pasted the relevant part of this function:


    Code:
    tree makequadtree(tree node, int array[MAXSIZE][MAXSIZE], int s) 
    
     { 
       int q,w,a,b;  
       int NW_array[MAXSIZE][MAXSIZE]={0}, NE_array[MAXSIZE][MAXSIZE]={0}; 
       int SE_array[MAXSIZE][MAXSIZE]={0}, SW_array[MAXSIZE][MAXSIZE]={0}; 
      
       node->size = s; 
       node->NW = NULL; 
       node->NE = NULL; 
       node->SE = NULL; 
       node->SW = NULL; 
      
       if(get_colour(array, s) == 'G') 
       { 
      
       node->colour = 'G'; 
      
        //NW BLOCK: 
       for(q=0,w=0, a=0,b=0; w<s/2, b<s/2; q++, a++) 
         { 
           NW_array[q][w] = array[a][b]; 
           if(q==(s/2)-1) 
            { 
              q=-1; 
              a=-1; 
              w++; 
              b++; 
            } 
         } 
       
       makequadtree(node->NW, NW_array, s/2); 
      
        //NE BLOCK: 
       for(q=0,w=0, a=2,b=0; w<s/2, b<s/2; q++, a++) 
         { 
           NE_array[q][w] = array[a][b]; 
           if(q==(s/2)-1) 
           {  
             q=-1; 
             a=1; 
             w++; 
             b++; 
           } 
         } 
      
      
       makequadtree(node->NE, NE_array, s/2); 
      
         //SE BLOCK: 
       for(q=0,w=0, a=2,b=2; w<s, b<s; q++, a++) 
         { 
           SE_array[q][w] = array[a][b]; 
           if(q==(s/2)-1) 
           { 
             q=-1; 
             a=1; 
             w++; 
             b++; 
           } 
         }   
      
        makequadtree(node->SE, SE_array, s/2); 
      
          //SW BLOCK: 
        for(q=0,w=0, a=0,b=2; w<s, b<s; q++, a++) 
          { 
            SW_array[q][w] = array[a][b]; 
            if(q==(s/2)-1) 
            { 
              q=-1; 
              a=1; 
              w++; 
              b++; 
            } 
          } 
      
        makequadtree(node->SW, SW_array, s/2); 
      
       } 
       else if(get_colour(array, s) == 'B') 
         { 
           node->colour = 'B'; 
         } 
      
       else node->colour = 'W'; 
         
     }
    The problem seems to lie in 'node->size = s;' and any other time I try to pass a value to a node. Perhaps I have defined my structure incorrectly?


    Any help would be much appreciated!
    Pedro.

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    100
    Segmentation fault is an obvious result as you call makequadtree with NULL pointer.
    Code:
    node->NW = NULL;
    node->NE = NULL;
    node->SE = NULL; 
    node->SW = NULL;
    // ...
    makequadtree(node->NW, NW_array, s/2); 
    makequadtree(node->NE, NE_array, s/2); 
    makequadtree(node->SE, SE_array, s/2);
    makequadtree(node->SW, SW_array, s/2);
    You declared tree as a pointer
    typedef struct tnode *tree;
    so these
    tree *P;
    tree *NW;
    tree *NE;
    tree *SE;
    tree *SW;

    are double pointers - is this what you intended?
    Last edited by DRK; 04-16-2013 at 03:44 AM.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Thanks for your reply. So they are not pointing anywhere? I intend to pass an initial node into makequadtree, and pass it values into its 'colour' and 'size' and then pass node->NW, node->NE, node->SE and node->SW recursively into makequadtree. How should I go about this?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    801
    I don't know if this is a stupid statement or not but I somehow expected more malloc's in this code. I'm fairly confident that you have to actually malloc the deeper branches of the tree, right? Or can you just statically allocate this to victory? Either way, something seems to be needed to be allocated.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Yes that's very true I should allocate memory to these nodes...I have done so now with
    Code:
    node->NE = (tnode*) malloc(sizeof(tnode));
    to each node

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-05-2011, 05:11 PM
  2. Segmentation fault while structure variable declaration
    By rk211083 in forum C Programming
    Replies: 9
    Last Post: 06-22-2010, 05:14 AM
  3. Big structure + file pointer = segmentation fault
    By ERJuanca in forum C Programming
    Replies: 6
    Last Post: 03-02-2010, 04:46 PM
  4. Structure Segmentation Fault
    By GouSan in forum C++ Programming
    Replies: 4
    Last Post: 12-05-2007, 12:06 AM
  5. Replies: 7
    Last Post: 12-10-2004, 12:58 AM

Tags for this Thread


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