马尔可夫链(windows 上的 c 代码失败)
Markov chain (c code on windows fails)
我正在尝试使用 Kernighan 书中的这段代码在我的工作站上进行编程实践(windows 7 + vs2015 社区版)
我收到一个奇怪的错误。
void generate(int nwords) {
State *sp;
Suffix *suf;
char *prefix[NPREF];
char *w = NULL;
int i, nmatch;
for (i = 0; i < NPREF; i++)
prefix[i] = NONWORD;
for (i = 0; i < nwords; i++) {
sp = lookup(prefix, 0);
nmatch = 0;
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
if (nmatch == 0)
printf("internal error: no suffix %d %s", i, prefix[0]);
if (strcmp(w, NONWORD) == 0)
break;
printf("%s ", w);
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = w;
}
}
}
for (suf = sp->suf; suf != NULL; suf = suf->next)
Unhandled exception at 0x000000013F5C1564 in CompareCandCsharp.exe:
0xC0000005: Access violation reading location 0x0000000000000010.
我的实现与此处描述的类似 - Working with arrays and reading text files
似乎算法有效 - 但在我的计算机上它失败了。我找不到为此的正念 porpose。能否给点建议。
经过几个小时的调试,我在预期的方法中发现了一个小错误。
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
这一行的 if 后面没有括号,所以方法中的所有其他代码都尝试多次设置 w,当然这会导致内存错误 =)。感谢您在阅读之前忽略我的问题 =))
我正在尝试使用 Kernighan 书中的这段代码在我的工作站上进行编程实践(windows 7 + vs2015 社区版)
我收到一个奇怪的错误。
void generate(int nwords) {
State *sp;
Suffix *suf;
char *prefix[NPREF];
char *w = NULL;
int i, nmatch;
for (i = 0; i < NPREF; i++)
prefix[i] = NONWORD;
for (i = 0; i < nwords; i++) {
sp = lookup(prefix, 0);
nmatch = 0;
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
if (nmatch == 0)
printf("internal error: no suffix %d %s", i, prefix[0]);
if (strcmp(w, NONWORD) == 0)
break;
printf("%s ", w);
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = w;
}
}
}
for (suf = sp->suf; suf != NULL; suf = suf->next)
Unhandled exception at 0x000000013F5C1564 in CompareCandCsharp.exe: 0xC0000005: Access violation reading location 0x0000000000000010.
我的实现与此处描述的类似 - Working with arrays and reading text files
似乎算法有效 - 但在我的计算机上它失败了。我找不到为此的正念 porpose。能否给点建议。
经过几个小时的调试,我在预期的方法中发现了一个小错误。
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
这一行的 if 后面没有括号,所以方法中的所有其他代码都尝试多次设置 w,当然这会导致内存错误 =)。感谢您在阅读之前忽略我的问题 =))