A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Smart Pointers - Part IIIn this part we implement a Garbage Collection with C++. |
On Wednesday, June 9th 2004 at 03:22 AM By Hamed Zaghaghi (View Profile) ![]() ![]() ![]() ![]() (Rated 4.2 with 12 votes) |
||
|
Keywords:
Output: 1: class #0 doing something. 2: class #1 doing something. 3: class #2 doing something. 4: class #0 destroyed. 5: class #1 doing something. 6: class #2 doing something. 7: class #2 doing something. 8: class #1 doing something. 9: class #1 doing something. 10: class #2 doing something. 11: class #1 doing something. 12: class #2 destroyed. 13: class #1 destroyed. Have a Goooood Day, with more errorless programming. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current Commentsthe bets ive seen for a while
Smart Pointers - Part II
In this part we implement a Garbage Collection with C .
On Wednesday, June 9th 2004 at 03:22 AM
--------------------------------------------------------------------------------
By Hamed Zaghaghi (View Profile)
--------------------------------------------------------------------------------
(Rated 4.1 with 11 votes)
Contextual Ads
More C Resources
C Tutorials
C Code
C Jobs
Advertisement Keywords:
-Lifetime:
The period during witch an object exist, is the “Lifetime†of this object.
-Access Path:
“Access Path†to an object is the path that can access to an object.
-Garbage:
If all Access Paths to a Data Object (DO) loses, this Data Object is like “Garbage†in the memory.
-Dangling Reference:
If “Lifetime†of each Data Object (DO) was finished, when there is at least one “Access Path†to it, all Access Paths are “Dangling Referenceâ€.
Example of Garbage:
int * pi1 = new int; // first DO
int * pi2 = new int; // second DO
pi2 = pi1; // now pi1 and pi2 refer to first DO
// and the all access path to the second DO loses;
Example of Dangling Reference:
int * pi = new int; // first DO
delete pi;
...
//use the pi makes runtime error.
Now we want to solve garbage problem, which named Garbage Collection.
One of the ways that solve this problem is:
1.Delete all data objects that create using new or malloc.
2.When delete a reference to a data object don’t forget assign NULL to the pointer.
But I write a class in C that if I and you forgot the two rules above.
In this Class I use below techniques and if you don’t know one of them I suggested that you learn these techniques before reading the code.
-Templates
-Smart Pointers
-Overloading operators
-And other object oriented techniques.
template<typename T>
class CDataPacket
{
private:
int RefNum;
public:
T* pData;
CDataPacket();
~CDataPacket();
void DecRef();
void IncRef();
};
This class provides a data object that can be reference to a user data object, and count the references to it, and increment and decrement the reference counter.
In this object when the references counter equal to zero, user data object and this object deleted.
template<typename T>
class CPtr
{
private:
CDataPacket<T> * Refs;
public:
~CPtr();
CPtr();
T* operator->();
T
Related Tutorials
Related Source Code
C++ Job Search