Share via

Free function in C

Shervan360 1,681 Reputation points
2020-10-02T17:47:53.147+00:00

Hello,

Could you please explain this?

The first sentence told us we don't need free() memory but in the second sentence told us free() memory.
Could you please give me an example?

Thanks in advance

29838-screenshot-2020-10-02-133828.jpg

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

WayneAKing 4,936 Reputation points
2020-10-03T06:25:47.537+00:00

Additional to the answers already given, note that the mention of freeing
"in a function" ensures that your program doesn't develop what is known as
a memory "leak". This occurs when you allocate memory for an object
repeatedly without ever freeing the allocations.

If you write a function that does a malloc every time it is called, but
don't free the allocation before exiting that function, then the next time
that same function is called it will malloc again and assign the return to
the same pointer as the first time. That will cause you to lose the
reference to the earlier allocation and that allocation will persist
until your program exits. Thus your program will use more and more memory
the longer it executes and keeps repeating calls to the function.

Failing to free a buffer when it is no longer needed means that the memory
allocated is not made available for reuse by your program. With today's
systems where gigabytes of memory is often available for each process the
loss may not be noticeable. But on systems with limited memory available
for each process (e.g. - embedded systems, DOS, etc.) a program may find
that an attempt to allocate memory fails because not enough is left
available. Freeing larger allocations such as for file buffers, large
arrays, etc. helps to avoid this condition.

The above alludes to programming in C in an environment that doesn't
provide for automatic "garbage collection". In C++ one can use "smart
pointers" which will automatically release memory from allocations
(new/new[]) when the pointer goes out of scope. Then there are also
allocations done when using a framework such as .NET which will do
automatic "garbage collection". But for programming in straight C
"best practice" is to cultivate the habit of freeing allocations
when they are no longer needed.

As noted by others, when a program ends under Windows the operating system
will reclaim any memory that was allocated and never released. That wasn't
always the case, and allocated memory would be lost for reuse by new
program executions until the operating system was rebooted.

  • Wayne

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. RLWA32 52,571 Reputation points
    2020-10-02T18:01:07.883+00:00

    Widows automatically deallocates the memory used by a process when it terminates. There is a good discussion of why diligently deallocating memory at process termination isn't necessarily a good thing here - When DLL_PROCESS_DETACH tells you that the process is exiting, your best bet is just to return without doing anything The discussion of memory management is applicable to EXEs as well as DLLs.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.