【发布时间】:2012-04-06 08:13:48
【问题描述】:
我正在尝试实现一个类似于 20 Questions 的程序,其中加载了一个包含问题和猜测答案的文本文件,并将其复制到一个 char 数组中(其中新的空格行被替换为 '/0'为了将问题拆分成单独的字符串)。将文本文件复制到其中后,该数组可以正常工作。建立一个树结构将短语组织成是/否问题树,其中左孩子是是的响应,而右是否响应,而叶子是程序最后用来猜测的猜测。
我遇到的问题是,在我构建树后(从 InitTree 调用 treeBuilder),复制文本文件中的短语的数组的内容被破坏了。
在调用 InitTree 之前,数组内容如下所示:
它是毛茸茸的吗?它喵喵叫吗?猫 狗 它有象牙吗?它有大耳朵吗?一头大象犀牛鳄鱼
调用后是这样的:
它是毛茸茸的吗? -???` ?p ?a dog 它有象牙吗?它有大耳朵吗?一头大象犀牛鳄鱼
我一直在测试它停止工作的位置,在 treeBuilder 中,数组的所有元素都完好无损,但是一旦对 treeBuilder 的函数调用结束,数组就会损坏。我尝试通过在分配内存时使用 calloc 来保护内存,甚至通过使字符数组静态化,这在发生这种情况的类似情况下工作。但我所有的预防措施似乎都不起作用,我不确定问题出在哪里。我已经尝试在 stackoverflow 上查看类似案例,但我无法找到与我的问题相关的任何内容。
这最终会导致段错误,当程序实际开始使用树时,原因很明显。
我尝试过运行 gdb,但无论出于何种原因,它都不允许我逐行遍历,因为它找不到行信息,并且只是跳过所有内容,直到它提示输入或出现内存错误什么的,所以在这里运行 gdb 不是很有帮助。我猜这可能是因为 main 函数在包含的文件中。但这不是重点。
这是与问题相关的代码:
struct treeStruct {
char *string;
struct treeStruct *left, *right;
};
typedef struct treeStruct *TreeType;
// Builds a tree
void treeBuilder(TreeType tree, char **phrase, long level){
// Gets the level (number of tabs) of the next phrase
long nextLevel = countTabs(*phrase + strlen(*phrase) + 1);
tree->string = *phrase + level; // Assigns the response pointer to the tree array
// Move the pointer to the next string, since the the strings need to be
// put into the tree in linear order
(*phrase) += strlen(*phrase) + 1;
if (level >= nextLevel){
// Compares the current level with the level of the next string
// to determine if returning up the tree is necessary;
// This should be the answer to a question.
tree->left = NULL;
tree->right = NULL;
return;
}
else{
// Makes sure the left and right pointers of the struct have
// allocated space
tree->left = calloc(1, sizeof(TreeType));
tree->right = calloc(1, sizeof(TreeType));
// Adds the yes and no branches to the tree, recursion will take care
// of adding sub-branches
treeBuilder(tree->left, phrase, level + 1);
treeBuilder(tree->right, phrase, level + 1);
}
return;
}
TreeType InitTree (char *file){
if(file == NULL){
printf("File '%s' does not exist.\n", file);
exit(2);
}
FILE *fp;
fp = fopen(file, "r");
// Create a space in memory for the loaded questions to occupy
static char *phrases;
phrases = (char *)malloc(MAXSTR * MAXNUMQS * sizeof(char));
copyText(fp, phrases);
fclose(fp);
// Create space in memory for the tree structure
TreeType tree;
tree = (TreeType) calloc(1, sizeof(TreeType));
// Create a pointer to a pointer so that treeBuilder can
// change what the first pointer is pointing to, so the strings in
// phrases can be added in order throughout the recursion
static char *phrase_ptr, **phrase_ptr2;
phrase_ptr = &phrases[0];
phrase_ptr2 = &phrase_ptr;
//Build the tree
treeBuilder(tree, phrase_ptr2, 0);
topNode = tree;
return tree;
}
对不起,如果这是 tl;dr,但我想尽可能清楚地说明我的问题。
【问题讨论】:
-
如果您能提供所谓的sscce.org 示例,那就太好了。仍然可以重现您的问题的最小但可编译的代码。这也可以帮助您自己找到错误。
-
例如在这里,您打开一个文件并使用我们甚至看不到的功能读取它。相反,也许
strcpy是一个字符串常量到phrases。 -
当你递归时,你真的想要
level + 1吗?还是你之前计算的nextLevel?
标签: c arrays pointers tree char