Home:ALL Converter>Free dynamically allocated memory defined by static keyword in C

Free dynamically allocated memory defined by static keyword in C

Ask Time:2013-03-21T23:42:08         Author:Pouya

Json Formatter

I am writing a code in which there several functions. In each function there are several variables to which memory should be allocated dynamically. The functions are called repeatedly so, it is logical to allocate the memory once and free it at the end of the main.

The main function looks like this:

//Some code here
while (err < tolerance){
        CalculateMismatch(M, err);
        //Update M and err and do other things
}
//Here is end of main, where I should free all the memories being allocated dynamically in other functions

This code shows that CalculateMismatch is called several times. So, I just allocate memory once in this function. Something like this:

function CalculateMismatch(Some arguments){
        //The following line is illegal in C, but I do not know how to allocate the memory just once and reuse it several times.
        static double *variable1 = malloc(lengthofVar1 * sizeof(variable1));
        //Rest of the code
}

My problem is that I do not know how to access the variables defined in CalculateMismatch in main. Kindly let me know how I should free the variables or whether or not there is a more efficient way to allocate and free the memory. Thanks for your help in advance.

To provide more details about my code: So far, I have defined all the variables globally, and allocated memory dynamically in main. But since the number of variables is large and some of them are used only in one specific function, I decided to move the definition inside the functions. But, I do not know how to free the memories allocated inside each function.

Author:Pouya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15551985/free-dynamically-allocated-memory-defined-by-static-keyword-in-c
Mats Petersson :

This is incorrect:\n\n static double *variable1 = malloc(lengthofVar1 * sizeof(variable1));\n\n\nyou probably want:\n\n static double *variable1 = malloc(lengthofVar1 * sizeof(*variable1));\n\n\nUnfortunately there is no way that you can free this variable from outside the function, unless you do something to pass it back. \n\nThere is no directly trivial solution to this. One solution would of course be to move the variable out a step:\n\nstatic double *variable1 = 0;\n\nfunction CalculateMismatch(Some arguments){\n\n if (!variable1) variable1 = malloc(lengthofVar1 * sizeof(*variable1));\n\n //Rest of the code\n}\n\n...\nint main(...)\n{\n ... \n\n free(variable1);\n}\n",
2013-03-21T15:55:14
john :

You cannot access the variables declared in CalculateMismatch from main. If you need to then you are going to have to move the declarations out of CalculateMismatch (to main for instance) and then pass them to CalculateMismatch as parameters.\n\nBut since you're about to exit main I don't think you need to free anything. All the memory will be freed when your program exits anyway.\n\nEDIT\n\nYou should move the declaration of variable1 to main. Then pass it to CalculateMismatch as a parameter.\n\nint main()\n{\n double *variable1 = malloc(...);\n ...\n while (...)\n {\n CalculateMismatch(..., variable1);\n }\n ...\n free(variable1);\n}\n",
2013-03-21T15:45:11
yy