c - Malloc/Realoc 错误的内存大小和值

标签 c memory malloc realloc memory-movement

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

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++;
        }
    }
}

网格:

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            

最佳答案

你做到了

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;

关于c - Malloc/Realoc 错误的内存大小和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41928383/

相关文章:

c - 重新分配后结构的无效大小

c - 结构中的二维数组 - 可能吗?

c - 打印返回结构的成员

c - 是否可以编写 C 函数来修改 Go 代码中定义的类型的结构?

java - 为什么 newInstance 使用的内存比预期多?

c++ - 为什么每个类不都有一个虚拟 ptr 呢?

np.loadtxt 和 iter_loadtxt 中的 Python MemoryError 或 ValueError

c - free() 崩溃,但前提是分配的内存超过一定大小

基于指向 const 的指针的 C 函数

C和动态结构元素访问