例外c0000005并且无法找出原因。 [英] exception c0000005 and cant find out why.

查看:263
本文介绍了例外c0000005并且无法找出原因。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{
    int lang;
    char c = 0, currWord[MAX_WORD_LEN+1], newWord[MAX_WORD_LEN+1];
    dict myDict;
    // getting the number of words in the dictionary
    scanf("%d", &(myDict.len));
    createDict(&myDict);

    // filling the dictionary
    for (int i=0; i<myDict.len; i++)
    {
        scanf("%s", myDict.dict0[i]);
        scanf("%s", myDict.dict1[i]);
    }

    // translating
    while ((scanf("%d", &lang)) != EOF && c != EOF)
    {
        printf("%d ", 1-lang);
        while ((c = getchar()) != EOF && c != '\n')
        {
            scanf("%s", currWord);
            translate(currWord, newWord, &myDict, lang);
            printf("%s\n", newWord);
        }
    }
    destroyDict(&myDict);
    return 0;
}

void createDict(struct dict* myDict)
{
	myDict->dict0 = (char (*)[MAX_WORD_LEN+1])malloc((myDict->len)*MAX_WORD_LEN);
	if ((myDict->dict0) == NULL)
            exit(1);
        myDict->dict1 = (char (*)[MAX_WORD_LEN+1])malloc((myDict->len)*MAX_WORD_LEN);
	if ((myDict->dict0) == NULL)
            exit(1);
}

void destroyDict(struct dict* myDict)
{
	free(myDict->dict0);
	free(myDict->dict1);
}





此代码应该接收一些单词,将它们放在结构字典中,然后翻译其他单词。问题是程序在大多数输入中运行良好,但是在某些程序中崩溃,抛出异常c0000005,我无法找到原因。调试没有用。有问题的输入将字典设置为30个单词,它翻译得很好,但在调用destroyDict()时,为了释放内存,程序崩溃了。有人可以帮忙吗?



This code is supposed to receive some words, put them in a struct dictionary, then "translating" other words. The thing is that the program workds fine in most inputs, however in some it crashes, throwing exception c0000005, and I can''t find out why. Debugging doesn''t help. The problematic input sets the dictionary to 30 words, it translates fine, but while calling to destroyDict(), in order to free the memory, the program crashes. Can anyone please help?

推荐答案

嗯。



我觉得你错了:

Um.

I think you mistyped:
myDict->dict0 = (char (*)[MAX_WORD_LEN+1])malloc((myDict>len)*MAX_WORD_LEN);
if ((myDict->dict0) == NULL)
        exit(1);
    myDict->dict1 = (char (*)[MAX_WORD_LEN+1])malloc((myDict>len)*MAX_WORD_LEN);
if ((myDict->dict0) == NULL)
        exit(1);



myDict> len 几乎肯定会返回true - 这不是你想要的大小。我怀疑你的意思是 myDict-> len


myDict>lenwill almost certainly return true - which is not the size you wanted. I suspect you meant myDict->len


异常c0000005似乎是访问违规 [ ^ ]例外。

这可能是由于缓冲区溢出造成的。

特别关注 scanf读数超出最大值缓冲区长度 [ ^ ]。



阅读宽度为的数据[ ^ ]以避免缓冲区溢出阅读。



最后,dict0和dict1的分配可能太小了1?



只是一些提示,没有任何线索,如果他们帮助解决问题。



祝你好运!

Andi
Exception c0000005 seems to be an Access Violation[^] exception.
This likely to be caused by buffer overrun.
Especially look for scanf reading beyond your max buffer length[^].

Read the data with a width [^] to avoid buffer overflow while reading.

Finally, the allocation of the dict0 and dict1 might be too small by 1?

Just some hints, no clue if they help solving the issue.

Good luck!
Andi


这篇关于例外c0000005并且无法找出原因。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆