H
Hendrik Maryns
Hi all,
I received a tiny program from a colleague. I want to use it in my Java
program. I invoke it with Java’s ProcessBuilder. That not being very
relevant, the problem is the following: it causes a segfault. Debugging
tells me the following line is the culprit:
while(fscanf(fp, "%s -> %s -> %s -> %s", name, left, right, symbol) == 4)
This is the stack:
Thread [0] (Suspended: Signal 'SIGSEGV' received. Description:
Segmentation fault.)
4 _IO_vfscanf() 0x00002ae4d5a0565f
3 fscanf() 0x00002ae4d5a11a58
2 loadTree() /home/hendrik/workspace/ivb/ivb.c:54 0x0000000000402cbc
1 main() /home/hendrik/workspace/ivb/ivb.c:23 0x0000000000402a95
The relevant function is below. (Since the problematic line is in the
beginning, you might not need all of it but I am really too new in C to
be able to shorten this. Pointers to a relevant site are welcome.)
What it does is it takes a file which contains a tree in some funny
format and constructs the tree in way MONA [1] can handle them.
Thank you very much.
[1]: http://www.brics.dk/mona/
mgTreeNode *loadTree(char *file)
{
hcreate(MAX_NODES);
char symbol[MAX_SYMBOL];
char name[MAX_NODENAME];
char left[MAX_NODENAME];
char right[MAX_NODENAME];
ENTRY node;
char* cache[MAX_NODES][3];
size_t nodeCounter=0;
size_t i;
FILE *fp = fopen(file, "r");
while(fscanf(fp, "%s -> %s -> %s -> %s", name, left, right, symbol) == 4)
{
// construct the node
mgTreeNode *t = tAlloc();
// convert the characters to numbers:
unsigned int len = strlen(symbol);
t->a = (char*)malloc(len*(sizeof(char))); // freed in freeTree
for(i = 0; i < len; ++i) {
t->a = symbol - '0';
}
t->left = NULL;
t->right = NULL;
// insert it into the hashtable
node.key = strdup(name); // this memory is freed after hdestroy
node.data = t;
hsearch(node, ENTER);
// insert it into an array
cache[nodeCounter][0] = node.key;
cache[nodeCounter][1] = strdup(left); // freed after hdestroy
cache[nodeCounter][2] = strdup(right); // freed after hdestroy
++nodeCounter;
}
fclose(fp);
// loop over the array and fill in the links
for(i=0; i < nodeCounter; ++i)
{
node.key = cache[0];
ENTRY *n = hsearch(node, FIND);
node.key = cache[1];
ENTRY *l = hsearch(node, FIND);
node.key = cache[2];
ENTRY *r = hsearch(node, FIND);
if(l != NULL) ((mgTreeNode *) n->data)->left = (l->data);
if(r != NULL) ((mgTreeNode *) n->data)->right = (r->data);
}
node.key = ROOT_NODE_NAME;
ENTRY *root = hsearch(node, FIND);
mgTreeNode *rootNode = NULL;
if(root != NULL) rootNode = root->data;
// free the hashtable
hdestroy();
// free the keys
for(i=0; i < nodeCounter; ++i)
{
int j;
for(j=0; j<3; ++j)
{
free(cache[j]);
}
}
return rootNode;
}
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHVZune+7xMGD3itQRAkz/AJ4lhLapj6GsFtG08AkWNjx0ejuU7gCdFz08
Fbg3Iwrij5ykLmPcwomp24Y=
=VPbd
-----END PGP SIGNATURE-----
I received a tiny program from a colleague. I want to use it in my Java
program. I invoke it with Java’s ProcessBuilder. That not being very
relevant, the problem is the following: it causes a segfault. Debugging
tells me the following line is the culprit:
while(fscanf(fp, "%s -> %s -> %s -> %s", name, left, right, symbol) == 4)
This is the stack:
Thread [0] (Suspended: Signal 'SIGSEGV' received. Description:
Segmentation fault.)
4 _IO_vfscanf() 0x00002ae4d5a0565f
3 fscanf() 0x00002ae4d5a11a58
2 loadTree() /home/hendrik/workspace/ivb/ivb.c:54 0x0000000000402cbc
1 main() /home/hendrik/workspace/ivb/ivb.c:23 0x0000000000402a95
The relevant function is below. (Since the problematic line is in the
beginning, you might not need all of it but I am really too new in C to
be able to shorten this. Pointers to a relevant site are welcome.)
What it does is it takes a file which contains a tree in some funny
format and constructs the tree in way MONA [1] can handle them.
Thank you very much.
[1]: http://www.brics.dk/mona/
mgTreeNode *loadTree(char *file)
{
hcreate(MAX_NODES);
char symbol[MAX_SYMBOL];
char name[MAX_NODENAME];
char left[MAX_NODENAME];
char right[MAX_NODENAME];
ENTRY node;
char* cache[MAX_NODES][3];
size_t nodeCounter=0;
size_t i;
FILE *fp = fopen(file, "r");
while(fscanf(fp, "%s -> %s -> %s -> %s", name, left, right, symbol) == 4)
{
// construct the node
mgTreeNode *t = tAlloc();
// convert the characters to numbers:
unsigned int len = strlen(symbol);
t->a = (char*)malloc(len*(sizeof(char))); // freed in freeTree
for(i = 0; i < len; ++i) {
t->a = symbol - '0';
}
t->left = NULL;
t->right = NULL;
// insert it into the hashtable
node.key = strdup(name); // this memory is freed after hdestroy
node.data = t;
hsearch(node, ENTER);
// insert it into an array
cache[nodeCounter][0] = node.key;
cache[nodeCounter][1] = strdup(left); // freed after hdestroy
cache[nodeCounter][2] = strdup(right); // freed after hdestroy
++nodeCounter;
}
fclose(fp);
// loop over the array and fill in the links
for(i=0; i < nodeCounter; ++i)
{
node.key = cache[0];
ENTRY *n = hsearch(node, FIND);
node.key = cache[1];
ENTRY *l = hsearch(node, FIND);
node.key = cache[2];
ENTRY *r = hsearch(node, FIND);
if(l != NULL) ((mgTreeNode *) n->data)->left = (l->data);
if(r != NULL) ((mgTreeNode *) n->data)->right = (r->data);
}
node.key = ROOT_NODE_NAME;
ENTRY *root = hsearch(node, FIND);
mgTreeNode *rootNode = NULL;
if(root != NULL) rootNode = root->data;
// free the hashtable
hdestroy();
// free the keys
for(i=0; i < nodeCounter; ++i)
{
int j;
for(j=0; j<3; ++j)
{
free(cache[j]);
}
}
return rootNode;
}
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
iD8DBQFHVZune+7xMGD3itQRAkz/AJ4lhLapj6GsFtG08AkWNjx0ejuU7gCdFz08
Fbg3Iwrij5ykLmPcwomp24Y=
=VPbd
-----END PGP SIGNATURE-----