简体   繁体   中英

Malloc/Realloc wrong memory size and values

I have run across a problem with the memory allocation of the variable tempWord . I have placed the problem code below the entirety of my code. It is supposed to read in a dictionary (list of words) and a wordSearch grid (2d char array), then iterate through said list looking for all words in the dictionary. I have it set up to look through the 2d array block by block creating temporary words of length 1 through 19 looking in all 8 directions before moving to the next block. My problem is that the tempWord dynamically allocated char array STORES INCORRECT DATA OR IS DISPLAYED INCORRECTLY. Please assist me.

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

The Problem Code, Part of main() function:

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                   

The Output:

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            

You did

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

throwing away the result of realloc() , and realloc() moved the memory pointed to by tempWord . Hello heap corruption.

Always always always check the return value of realloc().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM