简体   繁体   中英

Does this free() function clean the memory?

I need your help, becouse I dont know whether I realy free the allocated memory. I also will be very grateful, if you could advice me some tools for this purpose. Thanks! PS You can skip bb_sort and swap functions.

#include <stdio.h>
#include <stdlib.h>

typedef struct DArray{
    double * array;
    int size;
} DArray;

void getArrays(DArray * arrays, int numOfArrays){
    int curArraySize, i;
    while(numOfArrays >= 0){
        printf("Input size of array: "); scanf("%d", &curArraySize);
        if(arrays[numOfArrays].array = (double*)malloc((arrays[numOfArrays].size = curArraySize)*sizeof(double))){
            for(i = 0; i < arrays[numOfArrays].size; ++i)
            arrays[numOfArrays].array[i] = rand()%100;
            --numOfArrays;
        }
        else{
            printf("Error, have no enough memory in the system!"); break;
        }
    }
}

void swap(double * arr, int pos1, int pos2) {
        int tmp = arr[pos1];
        arr[pos1]=arr[pos2];
        arr[pos2]=tmp;
    } 

double * bb_sort(double * arr, int size){
    int i, j, noChanges = 1;
    while(1){
        for(i = 0;i < size;++i){
            for(j = 0;j < size-1;++j){

                if(arr[j] < arr[j+1]){
                swap(arr, j, j+1);
                noChanges = 0;
                }
            }
            if(noChanges) return arr;
            noChanges = 1;
        }
    }
}

int main() {
    srand (time (NULL));
    int numOfArrays, i, j;

    printf("Input number of arrays: "); scanf("%d", &numOfArrays);

    DArray arrays[numOfArrays];

    getArrays(arrays ,numOfArrays-1);

    for(i = 0; i < numOfArrays; ++i){
        bb_sort(arrays[i].array, arrays[i].size);
    }

    for(i = numOfArrays-1; i >= 0; --i){
        printf("\nSorted array num#%d\n", i+1);
        for(j = 0; j < arrays[i].size; ++j)
            printf(" %.1lf ", arrays[i].array[j]);

        free(arrays[i].array); <------FREE MEMORY?
        printf("\n");
    }
    printf("Done!");
    while(1){
    }
    return 0;
}

=====================================================================================================

Short Answer: The OP ask if 'memory is cleaned'. Technically, there is no requirement for the free implementation to CLEAN the memory (eg, sets the data to zero, or some random data). Most implementation will not CLEAR to memory to save time.

If, by mistake, the code will try to read the data, it will usually find the data is still in memory, potentially modified by the free call, or by other code, which was assigned the freed memory to other tasks.

For security sensitive information (password, credit card numbers, private keys), it is common to ZERO the data, or to fill the region with other pattern, before calling free. It will make it harder/reduce the risk of the data being access by mistakes, bugs or hacking.

Some memory debuggers will fill freed memory with pattern data/guards/etc, to help detect memory problems earlier than later.

Yes it will, since your program created array pointer using malloc function, and it does not manipulate that pointer, according to C99 standard (ISO/IEC 9899:1999): 7.20.3.2 The free function (p: 313):

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc , malloc , or realloc function, or if the space has been deallocated by a call to free or realloc , the behavior is undefined.

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