Saturday, February 14, 2009

File extensions - a confusing affair

Did you wonder what ".GOOK" and ".GIFAR" could mean? nope, you didn't!! But as a geek you need to know all the exotic extensions..
I got into this mess when I installed a script editor and it changed the default setting for a host of my other files like .xml and .class files. The next time I tried to compile and view my code, I got a response that no suitable program to open such extension found!
Loads of hours on the internet later I found this great site that has a registry fixer software. My recommendation - install this software asap - it even fixed my File Extension BIN that had started pointing to different places.. I use Avira anti-virus software which also uses BIN file extension and this extension is so very common but when I forgot to uncheck "always use this program to open this kind of file" on the Open With dialogue, I struck disaster..

This registry fixer software is just great. All registry errors fixed now.. and I can go on with my coding without worrying about how to fix my registry files :)

If you want to know about the full forms and which company devised the file formats then that information is too very nicely given here..
One very interesting trivia I found about BIN files here is that they can be opened in 12 different ways.. that means there are 12 different uses of the same file format.

You can also get info about file extensions through the alphabetical file index on the same site. :p

Monday, August 11, 2008

Implementing Dictionary in C++

Working with C#, I loved the feature of the data-type : Dictionary. I was wondering how we can implement a Dictionary in C++.

Heres what could be a way out...

1. Following the C# implementation as the model, we create a class having two elements : key and value, and implement it as a doubly linked-list. This will optimize structure parsing of the dictionary.

2. We can use Templates to make it a case with generic key and value types.

3. Another internal optimization would be to use hash.

4. Sort the dictionary based on the key values.

5. Provide functions to set and unset key-value pair.
--As I noticed that dictionary is immutable during access, provide a stack say _keys_to_delete on top of which we can push the keys of entries to be deleted. Implement a garbage collection of sorts that runs a loop to delete the entries based on the keys found in the stack _keys_to_delete after the dictionary has finished iterative access (so that it again becomes mutable)...

Monday, July 21, 2008

Missing The 'String' data type in C++

After working with languages like Java and C# for some time, one tends to assume to comfort of the 'String' data type. Coming back to some hard-core programming in C++ after some time, I wished that C++ be evolved into something that is more programmer friendly but at the same time maintains the speed and efficiency that is currently provides..

With many OpenSource and other wise initiatives to use modern languages for high-end development like in writing the new-gen OSes, the forte of C++, pointers, seems to be a mixed blessing.

Saturday, March 15, 2008

Overloading the NEW operator - Part II

The NEW operator has, among many advantages, then ability to be overloaded class-wise. In other words, you can overload the NEW operator on a per-class basis, so that your new operator is specifically tailored to respond to what type of object you are applying the NEW operator to.

So you can overload the NEW operator for each class to perform in special situations like handling the case when memory runs out and so on..

Sample code:

class myClass
{
char abc;
int def;
public:
void *operator new(size_t bytes)
{
.....specific code.......
}

};
class otherClass
{
char a;
int b;
public:
void *operator new(size_t bytes)
{
.....specific code.......
}

};
void main()
{
myClass obj1= new myClass();
otherClass obj2 = new otherClass();
.......................
.......................
}

Thursday, March 06, 2008

Overloading the NEW and DELETE operators in C++

Operator Overloading - One of the important pillars of the Object Oriented Concept is a very useful tool in the hands of the right programmer.

The fact that you can even Overload the NEW operator and the DELETE operator as well adds convenience to the custom memory management.

1. Overloading the NEW Operator:

void *operator new(size_t size, int setvalue)
{
void *pointer;

p = malloc(size);
if(p==NULL)
outofMemory( );
memset(pointer,setvalue,size);
return(p);
}

void outofMemory( )
{
cout<<"OOPS ran out of free memory..";
exit(1);
{.....perform other memory freeing operations here and try to re-allocate the memory....}
}

Saturday, March 01, 2008

To set the _new_handler pointer to default

In my last post, I talked about using the _new_handler pointer.. one thing I forgot to mention was to un-set the _new_handler pointer back to default value after pointing it to a user defined function.

For that, use -

set_new_handler(0);

This will return the pointer to its default state containing NULL.

Tuesday, February 26, 2008

What to do when your C++ memory underruns..

While playing with Pointers, the worst fears of any programmer are underruns and overflows....

But there is a very elegant solution to memory exhaustion in C++, wont provide you more memory but will at-least ensure that this exception is well handled..Here is the way out..

Suppose that your new operator used for creating new objects in the memory runs out of free memory so what to do then?

C++ has an internal function pointer called the _new_handler. Usually it contains a NULL which is returned by new operator when it fails to allocate memory for the requested object.
Now there is another special construct called the set_new_handler ( ) that lets you set the _new_handler to point to a user defined function which will be called in-case the new operator fails to allocate the memory. So that particular function will be called when such a case occurs.

Here is a code snippet to explain better:

void main( )
{
void outpfMemory( );
set_new_handler(outofMemory);

char *pointer = new char[..some large value like 64000u...];
}
void outofMemory( )
{
cout<<"
OOPS Ran out of memory..";
exit(1);
}

The function outofMemory will be called when the char Pointer assignment fails due to lack to available memory in the free store.