Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Exception
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 12-Jan-13 1:52am
DanDv242
Edited 12-Jan-13 9:12am
v2
Comments
Andreas Gieriet at 12-Jan-13 16:28pm
   
Show us the dict struct and its dict0 and dict1 types.
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>lenwill almost certainly return true - which is not the size you wanted. I suspect you meant myDict->len
  Permalink  
Comments
nv3 at 12-Jan-13 8:00am
   
You have a sharp eye. 5
OriginalGriff at 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 at 12-Jan-13 14:13pm
   
well actually it was just in the question. my code is as you suggested. but the problem still remains
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
DanDv at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100