0

to make the programs more efficient, do i need to free variables which are declared locally in the functions? Are they not disposed when the function ends?

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Narue
0

You only need to explicitly free resources that you explicitly ask for. For example, a FILE* given to you by fopen needs to be closed, and a pointer given to you by malloc needs to be freed.

0

If you call malloc, then you need to call free.

The compiler looks after allocation and deallocation of local variables.

0

You do not need to free local variables when the function ends. Where would they be anyway? Who or what could still access them?

0

So when i declare a local variable inside a function, it will be disposed from the memory automatically? And i should dispose it myself if i create it using malloc?

0

>So when i declare a local variable inside a function,
>it will be disposed from the memory automatically?
Yes.

>And i should dispose it myself if i create it using malloc?
Yes.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.