Click here to Skip to main content
12,447,275 members (53,494 online)
Click here to Skip to main content


70 bookmarked

A garbage collection framework for C++

, 17 Jan 2001
An article on using garbage collection in C++ through the use of smart pointers.
#if !defined(_GCPTR_H_68F9569A_2697_45DD_B71D_E1F77D95E40E_INCLUDED_)
#define _GCPTR_H_68F9569A_2697_45DD_B71D_E1F77D95E40E_INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

namespace gc_detail
	struct gc_t

	struct node_t;

	struct pointer_base
		pointer_base(node_t* h);

		node_t* node;


	struct node_t
		node_t(void* obj, int n);
		bool contains(void* obj);

		void* object;
		int size;
		bool mark;
		void (*destroy)(void*);

	template <typename T>
	struct destructor
		static void destroy(void* obj) { delete static_cast<T*>(obj); }

	node_t* get_node(void* obj, void (*destroy)(void*));

	template <typename T>
	node_t* get_node(T* obj)
		return get_node(obj, destructor<T>::destroy);
};	// namespace gc_detail

	gc_detail::gc_t gc;

void* operator new(size_t size, const gc_detail::gc_t&);
void operator delete(void* p, const gc_detail::gc_t&);

void gc_collect();
void gc_set_threshold(size_t bytes);

template <typename T>
class gc_ptr : public gc_detail::pointer_base
	gc_ptr() : gc_detail::pointer_base(0) { }
	explicit gc_ptr(T* p) : gc_detail::pointer_base(gc_detail::get_node(p)) { }
	template <typename U>
		explicit gc_ptr(const gc_ptr<U>& other) : gc_detail::pointer_base(other.node) { }

	gc_ptr<T>& operator=(T* p) { node = gc_detail::get_handle(p); return *this; }
	template <typename U>
		gc_ptr<T>& operator=(const gc_ptr<U>& other) { node = other.node; return *this; }

	T* get() const { return node ? static_cast<T*>(node->object) : 0; }
	T& operator*() const { return *get(); }
	T* operator->() const { return get(); }

#endif // !defined(_GCPTR_H_68F9569A_2697_45DD_B71D_E1F77D95E40E_INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

William E. Kempf
Web Developer
United States United States
Windows developer with 10+ years experience working in the banking industry.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 18 Jan 2001
Article Copyright 2001 by William E. Kempf
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid