473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problem with tree and enhanced queue.

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.

Jan 5 '06 #1
4 1574
"negative" <pa*****@gmail.com> 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.
Jan 5 '06 #2
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
Jan 5 '06 #3
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:ro************@knapp.com
Jan 5 '06 #4

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...

Jan 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adalbert | last post by:
First, I'm sorry for my english. Second, I've a little question: is some tree stucture like collections ArrayList or Queue to hold tree of same objects in .NET? Maybe I can use TreeView to that. I...
8
by: Shock | last post by:
Hello everyone, I am having a problem with the program below. I have isolated the problem to the onclick event that is located throughout arrQuestions. The onclick event refers to a function...
7
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
7
by: Casper | last post by:
In scanning a drive and comparing file content, I need to remember filepaths to every single file I encounter so I can take actions on certain files later on. Rather than having a huge list...
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
3
by: Rob Latour | last post by:
The following snippet (vb.net 2005) is working just fine in xp but not in vista in xp it lists all related sub keys in the registry just fine. in vista it doesn't list certain ones (like...
6
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree...
3
by: slizorn | last post by:
hi guys, the error is as follows: error C2440: '=' : cannot convert from 'TreeNode<T> *' to 'char *' with 1> 1> Types pointed to are unrelated; conversion requires...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.