Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
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);
}


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?
Posted
Updated 12-Jan-13 8:12am
v2
Comments
Andreas Gieriet 12-Jan-13 16:28pm    
Show us the dict struct and its dict0 and dict1 types.
Andi

Um.

I think you mistyped:
C++
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>lenwill almost certainly return true - which is not the size you wanted. I suspect you meant myDict->len
 
Share this answer
 
Comments
nv3 12-Jan-13 8:00am    
You have a sharp eye. 5
OriginalGriff 12-Jan-13 8:25am    
Details, my friend, it's all in the details! :laugh:
(Plus it's a lot easier for me to see that than the OP - we all tend to read what we expected to write, rather than what we actually wrote)
DanDv 12-Jan-13 14:13pm    
well actually it was just in the question. my code is as you suggested. but the problem still remains
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
 
Share this answer
 
Comments
DanDv 13-Jan-13 13:01pm    
Excellent. it now works. Thanks man!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900