【发布时间】:2020-06-30 00:55:07
【问题描述】:
typedef bool list_less_func (const struct list_elem *a,
const struct list_elem *b,
void *aux);
void
list_sort (struct list *list, list_less_func *less, void *aux)
{
size_t output_run_cnt; /* Number of runs output in current pass. */
ASSERT (list != NULL);
ASSERT (less != NULL);
/* Pass over the list repeatedly, merging adjacent runs of
nondecreasing elements, until only one run is left. */
do
{
struct list_elem *a0; /* Start of first run. */
struct list_elem *a1b0; /* End of first run, start of second. */
struct list_elem *b1; /* End of second run. */
output_run_cnt = 0;
for (a0 = list_begin (list); a0 != list_end (list); a0 = b1)
{
/* Each iteration produces one output run. */
output_run_cnt++;
/* Locate two adjacent runs of nondecreasing elements
A0...A1B0 and A1B0...B1. */
a1b0 = find_end_of_run (a0, list_end (list), less, aux);
if (a1b0 == list_end (list))
break;
b1 = find_end_of_run (a1b0, list_end (list), less, aux);
/* Merge the runs. */
inplace_merge (a0, a1b0, b1, less, aux);
}
}
while (output_run_cnt > 1);
ASSERT (is_sorted (list_begin (list), list_end (list), less, aux));
}
void wordcount_sort(word_count_list_t *wclist,
bool less(const word_count_t *, const word_count_t *)) {
list_sort(wclist, less_list, less);
}
static bool less_list(const struct list_elem *ewc1,
const struct list_elem *ewc2, void *aux) {
/* TODO */
list_less_func* comparefunc;
if (comparefunc(ewc1, ewc2, aux))
return false;
else
return true;
}
大家好,我认为这是一个简单的 C++ 问题。问题出在 less_list(...) 函数中,应该是关于函数 typedef 的问题。我对此并不熟悉,但我的截止日期快到了。感谢帮助!而且list_sort中的大部分代码你可以忽略,重要的信息只是“less”函数。
【问题讨论】:
-
你有什么问题?
-
这个问题几乎没有问题,但我突然想到一个问题:
list_less_func* comparefunc;是一个指向无处的指针。 -
它说 comparefunc 没有初始化
-
你们有什么建议来解决这个问题?
-
请提供minimal reproducible example 并张贴完整的逐字错误消息。该代码应该是大约 30 行代码,其他人应该能够复制和粘贴它以重现问题。正如@user4581301 已经说过的
comparefunc是一个未初始化的指针。取消引用它会导致未定义的行为。