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?

Recommended Answers

All 5 Replies

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.

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

The compiler looks after allocation and deallocation of local variables.

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

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.