Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-07-2010, 08:11 AM   #1
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Garbage collection in C

I was wondering if it is possible to have a garbage collector in C, and would it be too difficult to create?
Old 01-07-2010, 09:02 AM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
It is 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.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Garbage Collection Problem in FC8 baddah Fedora 2 06-27-2008 09:08 AM
How Many Kernels Have Garbage Collection? Peatmoss Linux - Newbie 9 12-03-2007 10:15 PM
LXer: Real-time garbage collection with Real-time Java LXer Syndicated Linux News 0 05-05-2007 12:16 PM
Garbage Collection in C : Local variables question duryodhan Programming 13 12-04-2006 07:16 AM
Video garbage only zonder Linux - Newbie 5 07-21-2005 03:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration