HOME -> C without memory errors
C without memory errors
By Dejan Jelović
....................................... ...Clavial Erro.
An article with more details will be ready soon.
Memory Errors Are The Worst Kind of Errors In C and C . They Are Hard To Reproduce and Thus Hard To Debug. Have you EVER USED A NON-TRIVIAL C OR C Program That Never CraShed?
I've Been Programming in Java for The Last Year. In Java One Sees Almost No Hard To Reproduce Problems. Why is what?
Is it because Java is a safer language? No. Java makes you use too many casts and lacks support for types with value semantics. It does not have destructors so resource management is really hard. It is less safe than C .
The Real Reason is Garbage Collection.
How does Garbage Collection Help? Does It Free Us from Freeing Memory? Not really. TRY IMPLEMENTING A Vector in Java and You Will See How Easy It Is To Forget To Set a Reference To Null, Thus Leaking Memory.
The Benefit of Garbage Collection is this: It Guarantees That a Pointer Will Either Point To a Valid Allocated Object OR Be Null.
Think About It: if a Pointer Is Either Valid Or Null, There Can Be No Weird Errors.
How do we do what IN C ?
How About Add Garbage Collection TO C ? A Company Named Geodesic Claims Their Garbage Collector for C Works Great.
.
There is also a performance penalty Programs with garbage collection use 50% more memory than programs with manual memory management That translates into much slower execution because it increases the chances of memory being swapping out to disk.Here's the solution:.. Use a smart pointer .
What Would Be THE CHARACTERISTICS OF SUCH A Smart Pointer?
It always points either to a valid allocated object or is NULL. It deletes the object once there are no more references to it. It can be used with existing code. Programs that do not do low-level stuff can be written exclusively using this pointer. Native pointers do not have to be used at all. Native pointers can be converted into smart pointers via a special construct. It should be easy to search for this construct using grep since it is unsafe. Thread-safe. Exception safe. Fast. It Should Have Zero Dereferencing Overhead and Minimal Manipulation Overhead. It Shouldn't Have Problems with Circular References.
IF WE DISREGARD The Requirement # 9, THEN'S Easy. Simply Have A Smart Pointer That Is Made Out of Two Native Pointers. One Points To The Real Object, The Other To The Reference Count:
WE Also Provide A Template Function Called Create That Creates A New Object Using The Operator New and The Returns A Smart Pointer. That Guarance Is Valid.
We also provide a back door to ease the transition. There is a wrapPtr function that takes a native pointer and makes a smart pointer. This is unsafe. So we did not use a constructor, but a special function that is easy to search for If you have anyproblems.
This Leaves US with The Problem of Circular References.
. Circular references and reference counting do not go well together Maybe we can find a simpler problem In real-world programs you almost never have real cyclic data structures Usually you have hierarchies, like the window hierarchy or a directory tree?.:
THE PROBLEM WITH A Reference Count Points to a Child, And a Child Points To The Parent. Hence They Both Have A Reference Count of One, And Are Never deleted.
............... ..
BUT THIS NOT A Good Solution, Because We Are Trying Not to Use Native Pointers. We Want Pointers That Are Either Pointing to a Valid Object OR ARE NULL.
Here's a Solution: We need weak smart pointers!
They will be just like regular reference counter pointers, only their references will not count. If you have a "strong" pointer and a weak pointer pointing to the same object, and the strong pointer is destroyed, the weak pointer immediately becomes null.
How do we import this? WE Keep TWO Reference Count Count: One for Total Number of Pointers, and ONE for strong pointers.
WHEN Dereferencing a Weak Pointer, We Check The Strong Reference Count. If IT IS ZERO, We Return NULL.
Try it out! The code is right here: safeptr.zip.
The Code Is Done Using The Intel C Compiler for the Windows Platform.
You NEED A COMPILER That Can Handle Templates Correctly. Visual C (Version 6, Service Pack 4) Chokes on the AND Either Silently Makes A Compiration Error Or Gives An Internal Compiler Error.
If you are not using Windows you will need to modify the SmartPtr.h file. It uses the Windows API function InterlockedIncrement and InterlockedDecrement to modify the reference count. These functions are nothing more than a thread-safe way to increment and decrement a long. Each Os or Compiler Should Have The Equivalents, You Just Have To Find Them.There Are Three Files in The Archive:
Safeptr.h - this is the one what you need the smart point in order;
Test.cpp - this is a program That Puts the smart pointer through the hoops
Demo.cpp - Look at this one in you want to learn how to use the library
Read more ...
Content of this Site Is © Dejan Jelovic. All Rights Reserved.