Malloc / Realloc错误的内存大小和值

时间:2017-01-30 02:46:31

标签: c memory malloc realloc memory-movement

我遇到了变量tempWord的内存分配问题。我已将问题代码放在我的整个代码之下。它应该在字典(单词列表)和wordSearch网格(2d字符数组)中读取,然后遍历所述列表以查找字典中的所有单词。我将它设置为逐块查看2d数组,在移动到下一个块之前创建长度为1到19的临时单词,查看所有8个方向。我的问题是tempWord动态分配的char数组存储不正确的数据或不正确地显示。请帮助我

int main(){
    //READ IN DICTIONARY AND ALLOCATE MEMORY
    FILE* dictionary = fopen("dictionary.txt", "r");
    int dicLength; fscanf(dictionary, "%d", &dicLength);

    char** dicArray = malloc(dicLength * sizeof(char*));
    int i, j;
    for(i = 0; i < dicLength; i++)
        dicArray[i] = malloc(20 * sizeof(char)); //change 20 to value define in PDF malloc((ID_LEN+1) * sizeof(char));

    for(i = 0; i < dicLength; i++)
        fscanf(dictionary, "%s", dicArray[i]);


    //SCAN IN WORD GRIDS AND ALLOCATE MEMORY
    int numCases;
    scanf("%d", &numCases);

    int x, y;
    scanf("%d %d", &y, &x);

    int q;
    for(q = 0; q < numCases; q++){
        char** wordArray = malloc(y * sizeof(char*));
        for(i = 0; i < y; i++) {
            wordArray[i] = malloc(x * sizeof(char));
            scanf("%s", wordArray[i]);
        }
        int* wordLocations = malloc(dicLength * sizeof(int));
        int location = 0;
        for(i = 0; i < y; i++){
            for(j = 0; j < x; j++){
                int k, l;
                for(k = 0; k < DX_SIZE; k++){
                    char* tempWord = malloc(sizeof(char));
                    tempWord[0] = wordArray[i][j];
                    if(i + DY[k] >= 0 && i + DY[k] <= y && j + DX[k] >= 0 && j + DX[k] <= x){

                        for(l = 1; l < 20; l++){
                            if(i + ((l-1)*DY[k]) >= 0 && i + ((l-1)*DY[k]) <= y && j + ((l-1)*DX[k]) >= 0 && j + ((l-1)*DX[k]) <= x){
                                realloc(tempWord, l * sizeof(char));
                                tempWord[l-1] = wordArray[ i + ((l-1)*DY[k]) ][ j + ((l-1)*DX[k]) ];
                                //printf("%c %c \n", tempWord[l-1], tempWord[l]);
                                printf("Temporary word: %s \n", tempWord);
                                if(checkCurrentWord(tempWord, dicArray, dicLength) != 0){
                                    break;
                                }
                                else{
                                    wordLocations[location] = dicArray[checkCurrentWord(tempWord, dicArray, dicLength)];
                                    location++;
                                }
                            }


                        }
                    }


                }

            }
        }


        for(i = 0; i < y; i++)
            free(wordArray[i]);
        free(wordArray);
    }

    //FREE MEMORY IN THE DICTIONARY
    for(i = 0; i < dicLength; i ++)
        free(dicArray[i]);
    free(dicArray);
    return 0;
}

//Binary Search function

int checkCurrentWord(char* tempWord, char** dicArray, int dicLength){
    int first = 0, last = dicLength - 1, middle = (first+last)/2;
    while(first <= last){
            printf("TEMP WORD: %s, DICTIONARY WORD: %s \n", tempWord, dicArray[middle]);
        if( strcmp(tempWord, dicArray[first]) < 0){
            first = middle + 1;
        }
        else if( strcmp(tempWord, dicArray[first]) == 0 ){
            break;
        }
        else{
            last = middle - 1;
        }
        middle = (first + last) / 2;
    }

    if(first > last){
        return 0;
    }
    return middle;
}

问题代码,main()函数的一部分:

for(l = 1; l < 20; l++){
    if(i + ((l-1)*DY[k]) >= 0 && i + ((l-1)*DY[k]) <= y && j + ((l-1)*DX[k]) >= 0 && j + ((l-1)*DX[k]) <= x){
        realloc(tempWord, l * sizeof(char));
        tempWord[l-1] = wordArray[ i + ((l-1)*DY[k]) ][ j + ((l-1)*DX[k]) ];
        //printf("%c %c \n", tempWord[l-1], tempWord[l]);
        printf("Temporary word: %s \n", tempWord);
        if(checkCurrentWord(tempWord, dicArray, dicLength) != 0){
            break;
        }
        else{
            wordLocations[location] = dicArray[checkCurrentWord(tempWord, dicArray, dicLength)];
            location++;
        }
    }
}

THE GRID:

syrt                  
gtrp                    
faaq                    
pmrc                   

输出:

Temporary word: ≡s╠                        
TEMP WORD: ≡s╠, DICTIONARY WORD: lisper            
TEMP WORD: ≡s╠, DICTIONARY WORD: dishonoring     
TEMP WORD: ≡s╠, DICTIONARY WORD: canalicular          
...                            
TEMP WORD: ≡s╠, DICTIONARY WORD: aahing        
Temporary word: ≡sg                         
TEMP WORD: ≡sg, DICTIONARY WORD: lisper            
TEMP WORD: ≡sg, DICTIONARY WORD: dishonoring            
TEMP WORD: ≡sg, DICTIONARY WORD: canalicular             
...                                             
TEMP WORD: ≡sgf└, DICTIONARY WORD: aahing                 
Temporary word: ≡sgfp                 
TEMP WORD: ≡sgfp, DICTIONARY WORD: lisper                   
TEMP WORD: ≡sgfp, DICTIONARY WORD: dishonoring              
TEMP WORD: ≡sgfp, DICTIONARY WORD: canalicular            
...                 
TEMP WORD: ≡sgfp, DICTIONARY WORD: aahing            

1 个答案:

答案 0 :(得分:2)

你做到了

realloc(tempWord, l * sizeof(char));

丢弃realloc()的结果,而realloc()移动了tempWord指向的内存。你好堆损坏。

始终总是检查realloc()的返回值。

char *tempword2 = realloc(tempWord, l * sizeof(char));
if (tempword2 == NULL) { fputs("OOM\n", stderr); exit(3); /* handle OOM */ }
tempword = tempword2;