Problem with tree and enhanced queue.

Discussion in 'C Programming' started by negative, Jan 5, 2006.

  1. negative

    negative Guest

    Dear all,

    I want to create a tree, where every node has an undetermined number
    of children (during the population of the tree, the exact number of
    children of each node , will become clear). So i thought that i could
    use in each node a queue in which we will add, every time a new child
    is born, the address of this node. Here are the structures i have used:

    // The queue node. Every node contains a pointer to a tree node and a
    pointer to the next
    // element of the queue
    typedef struct queue_tag {
    struct TreeNode_tag * child_ptr;
    struct queue_tag * next;
    } QueueNode;

    // the queue
    typedef struct {
    QueueNode *head;
    QueueNode *tail;
    } queue;

    // the tree. Every leaf contains an integer i and a queue which
    contains the pointers to node's children
    typedef struct TreeNode_tag {
    int i;
    queue *q;
    } TreeNode;

    // The tree
    typedef TreeNode * tree;

    The problem is that i get a segmentation fault every time i try to
    access the queue of the tree node. Here is the code i use to start the
    tree :

    tree *t;
    *t = (TreeNode *)malloc(sizeof(TreeNode));

    (*t)->i = 0; // a random value
    (*t)->q->head = NULL; // initially the node has no children.
    (*t)->q->tail = NULL;

    I use gcc.

    Thank you for your time.
     
    negative, Jan 5, 2006
    #1
    1. Advertising

  2. "negative" <> writes:

    >The problem is that i get a segmentation fault every time i try to
    >access the queue of the tree node. Here is the code i use to start the
    >tree :


    >tree *t;
    >*t = (TreeNode *)malloc(sizeof(TreeNode));


    Where have you allocated space for the tree's queue? I think you require:

    *t = malloc(sizeof(TreeNode));
    (*t)->q = malloc(sizeof(Queue));

    --
    Chris.
     
    Chris McDonald, Jan 5, 2006
    #2
    1. Advertising

  3. negative

    Ian Malone Guest

    negative wrote:
    > Dear all,
    >


    <snip>

    >
    > // the tree. Every leaf contains an integer i and a queue which
    > contains the pointers to node's children
    > typedef struct TreeNode_tag {
    > int i;
    > queue *q;
    > } TreeNode;
    >
    > // The tree
    > typedef TreeNode * tree;
    >
    > The problem is that i get a segmentation fault every time i try to
    > access the queue of the tree node. Here is the code i use to start the
    > tree :
    >
    > tree *t;
    > *t = (TreeNode *)malloc(sizeof(TreeNode));

    No bearing on your problem, but the cast is unnecessary and
    sizeof( *t ) (brackets optional) is often preferred to sizeof(TreeNode)
    since it's harder to get wrong.

    >
    > (*t)->i = 0; // a random value
    > (*t)->q->head = NULL; // initially the node has no children.
    > (*t)->q->tail = NULL;
    >


    (*t)->q is an uninitialised pointer.

    --
    imalone
     
    Ian Malone, Jan 5, 2006
    #3
  4. Hi,

    At 5 Jan 2006 05:18:11 -0800,
    negative wrote:

    > // The tree
    > typedef TreeNode * tree;
    >
    > The problem is that i get a segmentation fault every time i try to
    > access the queue of the tree node. Here is the code i use to start the
    > tree :
    >
    > tree *t;

    This is the same as
    TreeNode **t;

    Well, you got some errors here.

    > *t = (TreeNode *)malloc(sizeof(TreeNode));


    Why did you do a typedef, when you are using this instead of
    *t = (tree) malloc (sizeof (TreeNode));

    Here you (try) initialize the value t points to with something, but
    t points to some random place in memory. You should have done something
    like
    t = (tree *) malloc (sizeof (tree));
    first.


    Regards,
    Roland
    --
    Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
    Software Development ------ \\\ /// ----------- http://www.knapp.com
    KNAPP Logistics Automation - \\V// - mailto:
     
    Roland Csaszar, Jan 5, 2006
    #4
  5. negative

    negative Guest

    Chris McDonald wrote:
    > Where have you allocated space for the tree's queue? I think you require:
    >
    > *t = malloc(sizeof(TreeNode));
    > (*t)->q = malloc(sizeof(Queue));
    >
    > --
    > Chris.


    thanks Chris

    This was finally the problem. I had forgotten to allocate space for
    the tree's queue.

    Negative...
     
    negative, Jan 5, 2006
    #5
    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. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,147
  2. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    691
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    1,006
    Jim Langston
    Mar 14, 2006
  4. Frank Millman
    Replies:
    3
    Views:
    352
    Frank Millman
    Feb 9, 2010
  5. Kris
    Replies:
    0
    Views:
    494
Loading...

Share This Page