Pouya
Pouya

Reputation: 1959

Free dynamically allocated memory defined by static keyword in C

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.

Upvotes: 0

Views: 1390

Answers (3)

teppic
teppic

Reputation: 8195

If you want to access dynamically allocated memory from several functions, but don't want to pass pointers as parameters, then declare the pointer at file scope (global). After declaration you can initialise it before it's needed with malloc, and then every function will be able to access the memory. Otherwise, just pass the pointer(s) to each function that needs to access the memory, which you can use until you free it.

A static variable inside a function in C is one which keeps its value across function calls and is initialised before your program runs, so you cannot initialise it with malloc.

Upvotes: 0

john
john

Reputation: 8027

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.

But 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.

EDIT

You should move the declaration of variable1 to main. Then pass it to CalculateMismatch as a parameter.

int main()
{
    double *variable1 = malloc(...);
    ...
    while (...)
    {
        CalculateMismatch(..., variable1);
    }
    ...
    free(variable1);
}

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129374

This is incorrect:

 static double *variable1 = malloc(lengthofVar1 * sizeof(variable1));

you probably want:

 static double *variable1 = malloc(lengthofVar1 * sizeof(*variable1));

Unfortunately there is no way that you can free this variable from outside the function, unless you do something to pass it back.

There is no directly trivial solution to this. One solution would of course be to move the variable out a step:

static double *variable1 = 0;

function CalculateMismatch(Some arguments){

    if (!variable1) variable1 = malloc(lengthofVar1 * sizeof(*variable1));

        //Rest of the code
}

...
int main(...)
{
    ... 

    free(variable1);
}

Upvotes: 2

Related Questions