Archive for the ‘c++’ tag
Paradigm Shift
Paradigm shifting without a clutch. That’s what it felt like.
I was ported from a world comfortable to me into another world. Partly out of choice, partly out of situation.
The Jungle
I was having a great time with the computer systems. Developing major components for the top selling anti-virus. I was into the jungle of bits, bytes, pointers, crashes, heaps, stacks, network connections, file handles, segmentation faults, memory dumps, data compression, memory leaks. C++ was the language of that jungle. I understood it. So did a few people around me. The gang was small, but it fought the battles ruthlessly. My typical nightmarish day was being handed a 16kb crash dump file. for the uninitiated, that’s a mini dump file generated by Microsoft Windows. It is generated in response to an exception which could not be handled by your application, so finally it was handled by the OS. if OS did not like what your app were doing, it’ll promptly shut the app down and generate a crash dump. The file generally contains the application stack the time of crashing and exception information. Many times, the mini dump files reported through the web (ever seen, the dialog by windows, which asks you to send the error report to Microsoft?) are corrupt. You’re left with nothing but a tool called WinDbg to analyze those dumps and fix the error. It was a gut-wrenching battle. I tried to guess which function caused it. Or, what parameter on the stack screwed up. Or, what pointer went rogue. Reproducing was often impossible as exact conditions on the user’s machine were unknown. A lot of guesses, nail-biting and food. The chances of success were low. 1 in 100 was fixed with complete accuracy. Most of the times, it was a lonely fight. It was raw.
The City
I arrived in the city. It has cool looking websites and smart people. They speak Java here. Sometimes, Javascript. It seemed all alien to me. And I seemed alien to them. I was baffled at first. Fast changing websites. Tall structures of struts, hibernate, spring, jboss, sessions, request, response, jQuery, REST, HTML, headers, footers, GET and POST. A lot of people talking about a lot of things. As a newcomer in the city, I watched in amazement. I bumped into people talking in Javascript. I watched them huddled together. I did not understand what they were talking about. I listened to Java, I could understand some of it. It was very similar to my native language. But, far more polished. I looked at those tall structures of web technolgy and wondered which one should I visit first. It was confusing. My jungle insticts were out of place. Soon, I figured it out. City was rude, but agile. It was running and responsive. City was easy. Picking up the lingo was not a challenge. The difficult part was to choose my area. So, I chose my place and entered it for once. It was different here. No fight for survival. It was about business. Customers want to get their things done. And we did it. People worked closely together to get things done fast.
What is good?
Sometimes, I think of the jungle. to the enchanting morning mist. Back to the hunting the most obscure bugs. Back to stalking the prey for days before finally finishing it off. Then, relaxing in my cubicle like there was no tomorrow. That’s the jungle. Raw, yet peaceful. Green, but presented you with a battle for survival once in a while. I can go back to it. It might take another paradigm shift back to it. I’m a better hunter now. Armed with the tools from the city. Equipped with better team skills.
The city is not such a bad place either if you know how to find your way around. You’ve to be cool here. You can get to meet a lot of random people around here. It can be a rush at first, but soon you’ll figure out how to talk. It offers it’s own joys. The city is ripe with assortment of opportunities for business and pleasure. This is my new home.
Smart Ptr
It’s a really small smart pointer. 9 lines of code. Of course, I had to cut a few corners (newlines;) to make this happen. But, the readability is maintained.
It doesn’t do much other than being smart. Fit for trivial tasks, such as tracking and deleting local memory allocations, avoiding uninitialized references, etc. It’s so small, it cannot count references. But, it does deallocate when it goes out of scope.
Features:
1. Deallocation when the smart pointer goes out of scope.
2. Invalid reference check.
3. Not dependent on any library (including STL). Useful when you need a small binary footprint.
Restrictions:
1. No reference counting. So, avoid assigning one smart_ptr to another. (Enable test2 in the attached program to see the problem.)
2. Works only with “new” and not with “new []” or “malloc”. Needs further templatization for that to be handled. But, kills the simplicity of the current class.
Code follows:
template<typename T>
class smart_ptr
{
public:
T* operator->() {if(ptr) return ptr; throw -1;}
T& operator*() {if(ptr) return *ptr; return NULL;}
smart_ptr() {ptr = NULL;}
smart_ptr(T* p):ptr(p) {}
~smart_ptr() {if (ptr) delete(ptr); ptr = NULL;}
smart_ptr& operator=(T* p) {if(ptr) delete(ptr); ptr = p; return *this;}
bool operator==(const T& p1) {return (p1.ptr == ptr);}
bool operator==(const int i) {return (i == (int)ptr);}
protected:
T* ptr;
};
Command line for VC++ compiler to build this program:
1. Open Visual Studio command prompt
2. Compile and Link: >cl smart.cpp
3. Run: >smart.exe
Command line for g++ compiler to build this program:
1. Compile and Link: >g++ smart.cpp -o smart
2. Run: >./smart
For usage, see the complete program: smart.cpp
