Deallocate memory with free() does not work [closed]

房东的猫 提交于 2019-12-12 06:48:27

问题


Here is a small program which fills some arrays and prints its content on the screen:

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

typedef struct{
    double **plist;
    int plistSize;
} ParticleList;

void sendPar(int *n, int np){
    // allocate memory for struct pl
    ParticleList pl;

    // allocate memory  for ParticleList np times
    pl.plist = malloc(sizeof(ParticleList) * np);

    // allocate memory for lists of size n[k]
    for(int k=0; k<np; k++){
        pl.plist[k] = malloc(sizeof(double) * n[k]);
    }

    // write some data to the list
    for(int k=0; k<np; k++){
        for(int l=0; l<n[k]; l++){
            pl.plist[k][l] = 100000*k+100*l;
        }
        pl.plistSize = n[k];
    }

    // print data to check
    for(int k=0; k<np; k++){
        printf("Listsize: %d\n", n[k]);
        for(int l=0; l<n[k]; l++){
            printf("Processor %d, Entry %d, Value %lf\n", k, l, pl.plist[k][l]);
        }
    }

    free(pl.plist);
}

int main(){
    int np = 3;

    int n[np];
    n[0] = 2;
    n[1] = 4;
    n[2] = 7;

    sendPar(n, np);
}

This is the output:

Listsize: 2
Processor 0, Entry 0, Value 0.000000
Processor 0, Entry 1, Value 100.000000
Listsize: 4
Processor 1, Entry 0, Value 100000.000000
Processor 1, Entry 1, Value 100100.000000
Processor 1, Entry 2, Value 100200.000000
Processor 1, Entry 3, Value 100300.000000
Listsize: 7
Processor 2, Entry 0, Value 200000.000000
Processor 2, Entry 1, Value 200100.000000
Processor 2, Entry 2, Value 200200.000000
Processor 2, Entry 3, Value 200300.000000
Processor 2, Entry 4, Value 200400.000000
Processor 2, Entry 5, Value 200500.000000

If I now want to deallocate the memory, using free(pl) does not work. I tried also free(pl.plist) which does work. But than I have still memory of plistSize which is not deallocated. What is the right thing to free the memory here?


回答1:


This memory allocation

pl.plist = malloc(sizeof(ParticleList) * np);
                  ^^^^^^^^^^^^^^^^^^^

does not make sense. I think you mean

pl.plist = malloc( sizeof( double * ) * np);
                  ^^^^^^^^^^^^^^^^

The last statement in this loop

// write some data to the list
for(int k=0; k<np; k++){
    for(int l=0; l<n[k]; l++){
        pl.plist[k][l] = 100000*k+100*l;
    }
    pl.plistSize = n[k];
    ^^^^^^^^^^^^^^^^^^^^
}

also does not make sense because the scalar object pl.plistSize is overwritten in each iteration of the outer loop.

To free the allocated memory you can write

for(int k=0; k<np; k++){
    free( pl.plist[k] )
}

free( pl.plist );



回答2:


Pl is a variabile declared statically which contains a pointer to a memory location. It is the array in the structure that needs to be freed.




回答3:


The problem could be that you misunderstand how malloc and free works.

The variable pl and its member plistSize are not allocated by you. It's allocated by the compiler when it compiles your code. You should not try to free it.

Only free what you malloc.




回答4:


The problem as I see it, you're leaving each pl.plist[k] allocated, leading to the memory leak.

Once you've free()-d pl.plist, there will be no way you can free the individual pl.plist[k]s, so the deallocation has to be in reverse order of allocation.

First free each pl.plist[k]s, then, free up the pl.plist itself.



来源:https://stackoverflow.com/questions/41678680/deallocate-memory-with-free-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!