Простой способ - использовать gdb / lldb.
Я скомпилировал ваш код и отладил:
$ g++ -g -ggdb test.cc
$ lldb a.out
(lldb) r
Process 83386 launched: '/path/to/a.out' (x86_64)
Process 83386 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
frame #0: 0x0000000100000ec1 a.out`searchforexist(root=0x0000000000000000, key="<link") at test.cc:104
101 void searchforexist(TreeNode *root, char *key)
102 {
103 if (root == NULL || root->key == key)
-> 104 root->freq ++;
105
106
107 if (root->key < key)
Target 0: (a.out) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
* frame #0: 0x0000000100000ec1 a.out`searchforexist(root=0x0000000000000000, key="<link") at test.cc:104
frame #1: 0x0000000100000eed a.out`searchforexist(root=0x0000000100102e50, key="<link") at test.cc:108
frame #2: 0x0000000100000efe a.out`searchforexist(root=0x0000000100102b00, key="<link") at test.cc:111
frame #3: 0x0000000100000d34 a.out`insertItem(root=0x0000000100102b00, gword="<link") at test.cc:78
frame #4: 0x0000000100000c28 a.out`main at test.cc:47
(lldb)
Мне кажется, это сообщение об ошибке содержит достаточно информации.
Я сделал несколько модификаций. Просто сделай так, чтобы это сработало.
Я не мог представить себе, как мог бы работать предыдущий, если каждое слово не является коротким и уникальным.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct listNode {
int id;
struct listNode *next;
} ListNode;
typedef struct treeNode {
char *word;
char *key;
int freq;
ListNode *head;
struct treeNode *left;
struct treeNode *right;
} TreeNode;
TreeNode *insertItem(TreeNode *root, char *gword);
void printTreeInorder(TreeNode *v);
void searchforexist(TreeNode *root, char *key);
void freeNodes(TreeNode *root);
#define MAX 25
int main() {
char word[MAX];
TreeNode *root = NULL;
FILE *fp = fopen("input.txt", "r");
memset(word, 0, MAX);
if (fp != NULL) {
// why fsanf("%s \n") ? is this a very special format?
while (fscanf(fp, "%24s \n", word) != EOF) {
// fprintf(stderr, "got: [%s]\n", word);
root = insertItem(root, word);
if (strcmp(word, "eof") == 0) break;
}
fclose(fp);
}
printTreeInorder(root);
printf("\n");
freeNodes(root);
return 0;
}
TreeNode *insertItem(TreeNode *root, char *gword) {
TreeNode *v = root;
TreeNode *pv = NULL;
while (v != NULL) {
pv = v;
int comp = strcmp(gword, v->word);
if (comp < 0) {
v = v->left;
} else if (comp > 0) {
v = v->right;
} else {
// char *key = v->word;
char *word = v->word;
searchforexist(root, word);
return root;
}
}
TreeNode *tmp = (TreeNode *)malloc(sizeof(TreeNode));
// why both key and word?
tmp->word = strdup(gword);
tmp->left = tmp->right = NULL;
tmp->freq = 1;
if (root != NULL) {
if (strcmp(gword, pv->word) < 0) {
pv->left = tmp;
} else {
pv->right = tmp;
}
} else
root = tmp;
return root;
}
void searchforexist(TreeNode *root, char *word) {
if(root == NULL) {
return;
}
int comp = strcmp(word, root->word);
if(comp == 0) {
root->freq++;
} else {
searchforexist(comp < 0 ? root->left : root->right , word);
}
}
void printTreeInorder(TreeNode *v) {
if (v==NULL) return;
printf("(");
printTreeInorder(v->left);
printf(")");
printf(" %.4s ", v->word);
printf("(");
printTreeInorder(v->right);
printf(")");
}
void freeNodes(TreeNode *root) {
if (root == NULL) {
return;
}
freeNodes(root->left);
freeNodes(root->right);
if(root->word != NULL) free(root->word);
if(root->key != NULL) free(root->key);
free(root);
return;
}