« To be or not to be ! | Main | Interesting Developments »

Tools of the Trade I

Many a time I was bumped with software developers who thinks using one language is better than others in absolute term. I've seen too many discussion about these to mention here. I sometime see where they come from, and sometime I see how emotion sometime overrules sincere judgement.

Brushing all of these aside, I would like to try some of the tools of C++, if you happen to have to use C++ for you desgn. We all know that managing dynamic allocation and pointer manipulation is a major source of software error that ranges from memory leak to undefined behavior of a software. There are lot of software around that does wraps the languages dynamic memory allocation and management routines to catch bounces and leaks related faults. So it is certainly not a new technique I'm about to discuss!. But just as a simple exercise we will discuss some of the following interesting tools of the trade -

1) Placement address of new and when can I use?

2) Auto pointer and Smart pointer, and what it gives?

3) Unnamed namespace

 

Placement address of new is really a mechanism to preallocate a buffer from heap and use again and again for different types of object before you delete ( or free ) the preallocated buffer. Some of us are already used to this technique by using malloc and typedefing while using C. And this might very well be a known pattern to us. But the point here is that when we use the C technique it is not obvious for a reader of the code. Using the C++ language feature makes it more obvious to the reader. By the way, you can find this language feature(pattern) in lot of places !

 

Auto pointer is really one of the software engineering mantra of using c/c++ or any language that supports dynamic (de)allocation of memory. The mantra is that every dynamic memory allocation should be paired as closely as possible with a deallocation. So if I use a malloc in a function, I should free that memory before exiting from that function. But this is very limited. Quite often a function's whole purpose is to act as object provider to others. So it is not mandatory to free any local allocation. But quite often, if the dynamic memory need is very local to a function, using auto pointer mechanism is natural, and the semantic is very similar to local variable, and yet it is with varying size. At this point you should ask yourself "How does the local variables get freed?". They don't necessarily get freed. The right term is that they get out of scope. How does it happen?, you might ask yourself !. Well that is what I think is pure programming. I'm going to leave you here alone ...

Tobe continued ...

Posted on Sunday, April 15, 2007 at 10:10AM by Registered CommenterProkash Sinha | CommentsPost a Comment

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.