简体   繁体   中英

Do I “have to” free() static dynamically allocated pointers?

I have a function with a for loop and inside the loop (and depending on the input) certain variables are initialized once (the first time) using malloc() .

These variables are declared like this:

static double *vector;

and then I allocate the memory using

malloc(size*sizeof(double));

The Question is:

Do I have to free these variables in the last iteration of the loop inside the called function or not?

UPDATE: maybe I explained myself wrong for some people. The thing is the called function (func_A) allocates memory for certain vectors it uses depending on the input from the main function. Then, this func_A is called several times from a loop in the main. That is why I define the variables as static, so that they are not defined everytime the func_A is called (for a matter of time consumption), because the dimensions will not change throughout the whole run. The variables are static but not global, so I can not free them from the main (right?).

You should always balance a malloc with a call to free .

In your case, you could call it when you know you no longer need the vector .

If that's not practical then you could always make a call to atexit ; passing to it a pointer to a suitable function which will free the memory. See http://www.tutorialspoint.com/c_standard_library/c_function_atexit.htm

Although you can often rely on operating systems to clean up for you on program termination, it's rather crude to rely on that.

Do I have to free these variables in the last iteration of the loop inside the called function or not?

You can , but you don't have to .

You only have to free() the memory allocated by memory allocator functions when you don't need it anymore.

One of the major purpose of memory allocation using malloc() and family is to overcome the limitation regarding scope of local variables ( returning address of local variable from a function is illegal ), so you never have to free() the memory "inside the called function" . You can very well return the pointer (returned by malloc() ) to the caller, perform some other operations with that pointer (memory) and then, clean up ( free() ) the pointer from the caller of the sub-function.

One thing to remember though, you need to pass the exact pointer to free() which was originally returned by malloc() or family.

当不再使用数据时,每个malloc调用都需要具有相应的免费调用。

you need to free the pointer. as a rule of thumb - everything you allocate with malloc/new is on the heap and needs to be free'd/deleted everything else is on the stack and doesn't

ps if you only used a grbage collected language such as java/go/python you wouldn't need to know any of this

It seems a vector is allocated once and then used during the life time of the prorgam. The vector is stored in the static variable. Before terminating the program it would be a good idea to free the vector, but the termination will also do that for you.

It sounds like the function is not prepared to work with different (in particular: larger) vector sizes during the program's life time.

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