我的程序中有什么导致异常c0000005? (可能是内存错误)

时间:2014-02-12 15:42:38

标签: c arrays exception pointers memory

我的代码:

#include <stdio.h>
#include <stdint-gcc.h>
#include <string.h>

int checkAnagram(char *word1, char *word2, int length){ //This function compares the two strings by storing the occurrences of their letters in a histogram, and then comparing that histogram.
    printf("test4");
    int i, n;
    int letterCount1[26], letterCount2[26];
    char letter;
    for(i=0;i<length;i++){
        letter = word1[i];
        letterCount1[letter-'a']++;
    }
    for(n=0;n<length;n++){
        letter = word2[n];
        letterCount2[letter-'a']++;
    }

    for(i=0;i<26;i++){
        for(n=0;n<26;n++){
            if(letterCount1[i]==letterCount2[n]){
                i++;
        } else {
        return 0;}
    }
}
return 1;
}

void main(){
    int length1, length2,i,n;

    scanf("%d", &length1);
    int lengthArray1[length1]; //Array used to store the length of each string (without white spaces)
    char *sentenceArray1[length1];
    char tempString[100000];
    //The array for storing the first set of sentences, and a temporary string used
    //for allocating memory in the next loop
    for(i=0;i<=length1;i++){
        fgets(tempString, 100000, stdin); //Reads the first line of input (up to and including \0), with a maximum line length which will probably be sufficient.
        sentenceArray1[i]=malloc((strlen(tempString))*sizeof(char)); //Allocates just enough memory for each string (including \0).
        int index = 0;
        for(n=0;n<(strlen(tempString));n++){
            if(tempString[n] != ' ' && tempString[n] != '.') { //Copies only from the input if the character is not a whitespace.
                sentenceArray1[i][index++]=tolower(tempString[n]);
            }
        }
        sentenceArray1[i][index] = '\0';
        lengthArray1[i]=strlen(sentenceArray1[i]);
        printf("test1\n");
    }
    scanf("%d", &length2);//Same stuff as above, now for the second set of strings.
    int lengthArray2[length2], index;
    char *sentenceArray2[length2];
    for(i=0;i<=length2;i++){
        fgets(tempString, 100000, stdin);
        sentenceArray2[i]=malloc((strlen(tempString))*sizeof(char));
        index = 0;
        for(n=0;n<(strlen(tempString));n++){
            if(tempString[n] != ' ' && tempString[n] != '.') {
                sentenceArray2[i][index++]=tolower(tempString[n]);
            }
        }
        sentenceArray2[i][index] = '\0';
        lengthArray2[i]=strlen(sentenceArray2[i]);
        printf("test2\n");
    }
    printf("test3");
    for(i=0;i<length1;i++){
        for(n=0;n<length2;n++){
            if(lengthArray2[i]==lengthArray1[i]){
                if(checkAnagram(*sentenceArray1[n],*sentenceArray2[i], length1)==1){ //Sends strings only to the checkAnagram function if they are of the same length.
                    printf("%d ",i);
                }
            }
        }
        printf("\n");
    }

}

假设输入和输出: I/O

我必须把某些数组和指针搞砸了,但是我的控制台的有限反馈+我对C编程的有限经验使得很难找到错误。我的输出到目前为止打印“test4”一次,然后崩溃,标题中给出了异常。

我希望我想要达到的目标是明确的,但不幸的是,我对这个错误不能更精确。

3 个答案:

答案 0 :(得分:1)

还有一个问题:

for(i=0;i<=length1;i++){

你有一次超过数组大小的迭代:

int lengthArray1[length1];

如果length1为5,则lengthArray1包含元素0,1,2,3和4,但您的循环计数为5.条件应为 i < length1 < / strong>,而不是i <= length1

您还应该使用运算符周围的空格来使代码更具可读性。

答案 1 :(得分:0)

主要错误发生在:

if(checkAnagram(*sentenceArray1[n],*sentenceArray2[i], length1)==1){

根据checkAnagram的定义,它似乎需要character-array。但是你只是在这两个数组中传递第一个char的值。所以改成它:

if(checkAnagram(sentenceArray1[n],sentenceArray2[i], length1)==1){

这实际上传递了指向两个数组的开头的指针。当我运行你的代码并给出你指定它成功执行的输入时,但是所需的输出还没有到来。

休息取决于您的算法。再次重新检查。

答案 2 :(得分:0)

这一行:

if(checkAnagram(*sentenceArray1[n],*sentenceArray2[i], length1)==1){ //Sends strings only to the checkAnagram function if they are of the same length.  

错误输出“参数1中的类型错误导致checkAnagram;找到char预期pointer to char
错误输出“参数2中的类型错误为checkAnagram;找到char预期pointer to char

从参数1&amp;中删除“*” 2.