Thread: Accessing a quad tree data structure

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Accessing a quad tree data structure

    The lecturer has written several functions to handle a quad tree data structure, all we have to do is access it to print an ASCII image.

    The function for uploading the image works fine, but when I try to access the print function, it gives me a seg fault. In dark red is the function call I made.

    I guess I did it wrong? Also there is a lot more code, but I just included the bits that concerned this part of the program.
    Code:
    
    typedef struct qtnode QTnode;
    
    struct qtnode {
        char   type;// ' ','*' or INTERNAL
        QTnode *ne; // north east quadrant
        QTnode *nw; // north west quadrant
        QTnode *sw; // south west quadrant
        QTnode *se; // south east quadrant
        QTnode *out;// zoomed-out image
    };
    
    
    void printImage(QTnode *qt, int dim)
    {
      char *buffer;
      int i;
    
      // reserve memory for file contents
      if((buffer = (char *) malloc(dim * dim)) == NULL) {
        printf("Error - out of memory.\n");
        exit(1);
      }
      // intialise buffer
      for( i = 0; i < dim * dim; buffer[i++] = ' ' )
        ;
      // decode quad tree into image buffer
      createBuffer(qt, buffer, 0, 0, dim, dim);
      printBuffer(buffer, dim); // print buffer
      free(buffer);
    }
    
    
    
    
    
    
    QTnode *flag;
    printImage(flag->type, dimen);
    Last edited by JFonseka; 10-24-2007 at 09:28 PM.

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Sorry, it was meant to be flag->type

    But I get pointer from integer warning and still a segfault

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, with your call, you don't initialize flag.

    This translates, in printImage(), into qt not being initialized.

    The only place in printImage() where qt is used is here:
    Code:
    createBuffer(qt, buffer, 0, 0, dim, dim);
    So unless that function sets qt to something (or ignores qt entirely), in other words if it assumes that qt has a value, it is going to crash.

    I'm guessing it takes information from qt and puts it into buffer, because of this comment.
    Code:
    // decode quad tree into image buffer
    In other words, you need to get information into the QTnode *flag somehow. You haven't provided enough information for me to tell how.

    That's assuming, of course, that this is your real code.
    Code:
    QTnode *flag;
    printImage(flag, dimen);
    Casting malloc() is also probably not a good idea, see the FAQ.

    [edit] And EOF can't fit into a char, it needs an int. Also see the FAQ. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I didn't cast any mallocs or do anything with EOF, I really have no idea what that code really represents, the lecturer wrote the complicated bits. We haven't done anything with buffers or the sort. I just know I have to pass a pointer to the quad tree "type"

    So i initialized a variable called flag of type QTnode and did flag->type

    And flag is a pointer.

    But I see what you mean I think, I need to store some type of information in flag before I try passing it?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, exactly. There's probably a function that reads a QTnode from a file or something. See if you can find it.

    Or post the prototypes of all of the functions you have access to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    This is what he said:

    "You just need to pass a pointer to quad tree type,
    and the dimension, to the function printImage()
    and it will take care of the rest."

    The dimension was obtained earlier from another function which read the uploaded file and returned the dimension. So i passed the dimension.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Code:
    typedef struct qtnode QTnode;
    
    struct qtnode {
        char   type;// ' ','*' or INTERNAL
        QTnode *ne; // north east quadrant
        QTnode *nw; // north west quadrant
        QTnode *sw; // south west quadrant
        QTnode *se; // south east quadrant
        QTnode *out;// zoomed-out image
    };
    
    // Function prototypes
    
    // Print prompt
    extern void printPrompt(void);
    
    // get image from file
    QTnode *getImage( char *filename, int *dim );
    
    // Print image to screen
    void printImage( QTnode *qtree, int dim );
    
    typedef struct country Country;
    struct country {
      char name[LENGTH];
      int dimen;
      int num;
      Country *next;
    };
    
    //Create node
    Country *makeNode(char p[MAXLENGTH]);

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I essentially created that Country struct, then stored the filename, dimension of the image and the image number in there, so it's a linked list of different countries.

    And now we have to print the image of a selected file, so we need to pass the dimension and the quad tree "type"

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Nvm, got it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM
  5. C++ Data structure Tree
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-23-2001, 10:14 PM