possible to have garbage collection in C.
There are either general purpose libraries such as the Boehm-Demers-Weiser one
, or you can implement something at a higher level (eg by creating your own 'smart' pointers).
Writing a general purpose collector for C is not trivial. You would need a good understanding of how data is arranged in the different segments by the compiler. The use of pointers in C is not particularly constrained, and even the type constraints can be bypassed, which can make it harder to identify which memory is still in use.
There are some advantages to garbage collection, particularly on larger projects. However, if the primary issue is one of memory leakage, it might be better to use one of the many tools such as valgrind
that help profile memory use. Manually coding memory allocation/deallocation requires some discipline, but it doesn't require much extra effort.