Archive for April, 2008

It's my Birthday!!!

Birthday Icon

Do you know what’s special today???
Yes! its my 25th Birthday. Its a bit more special.
Its the first and last b’day on which my age and day are the same – 25!!!

Its too late now. But i’m in a good mood to tell some stories. Do you know that the number 25 is special to me. Might be a coincidence – this blog took birth on March 25th. And more over that, my fathers birthday is on March 25th. My first b’day after starting this blog, which is on 25 – made me 25 years old. I’m pretty imaginative. huh? ;)

Do you know, whats the inspiration for me to start this blog? I was an ordinary guy who finish my work and reach home early. One day as a part for technical activities, my project lead asked me to send technical tips to group members. But one condition – One tip per day! To be honest, at first – i tried to escape, because i felt its a burden.

From next day onwards, i searched the net for Visual C++ tips, found a lot, i copied and send it to team members as mails. But later on, i realize that the thing which i felt as a burden is slowly becoming a passion. I’m learning a lot, my technical skills are getting sharpened. I stopped the art of copy-pasting and began to work hard and find interesting tips myself. (None of the items in this blog are copy-pastes :) ) I began to dig MSDN during late nights. Began to read a lot of books and articles out of them, searching for a spark for an innovative idea. Still the quest goes on…

Then several other persons requested me to include them in the mailing list for daily tips. The list began to grow! After that i got onsite assignment to Europe and the daily tips was stopped for a while. Now after coming back, i though of restarting it even more stronger. And thats how this blog took birth. previously only a group of persons enjoy my tips, now by being online, it reach all over the world! If any of my efforts made your life easier, then i am happy that my efforts have meaning!

I’ll continue my quest – quality tips per day. Watch on and thanks for all visitors who motivates me by visiting frequently and by listining me. Thanks For all your support!

For WeSeeTips
Jijo.

STL valarray – An easy way to manipulate values.


Assume you need to find the sum of 100 integers. You can create an array or use some STL containers, fill the values and by iterating you can find the sum. Instead of that you can use valarray container for storing any value type and do a number of mathematical operations for all items in just a function call.


See the code snippet for how to use valarray.

// Integer array of 10 members.
valarray<int> IntegerArray( 10 );

// Filling the array.
for( int nIndex = 0; nIndex < 10; ++nIndex )
{
    IntegerArray[ nIndex ] = nIndex;
}

// I need to get the sum of all number.
int nSum = IntegerArray.sum();

If you want to manipulate bulk amount of numbers, better use valarray.


The valarray contains only sum() and misc functions are its member function. Still they are useful, if you want to keep bulk amount of numerals.


Targeted Audience – Beginners.

Assertion while calling set_new_handler(). Why?


C++ allows us to set a exception handling function, to handle memory allocation failures. When ever memory allocation failure occurs, this function will be called. See the following code block.

// Memory allocation error handler.
void __cdecl MyNewHandler()
{
    std::cout << "Allocation failed.";
}

void main()
{
    // Set the new handler
    set_new_handler( MyNewHandler );

    int *pi = new int[500000000];
}

Its valid, it calls the function to register the memory allocation failure handling function and it compiles. Perfect! But when you run it, it will show assertion. Do you know why?


You’ve missed an underscore. Yes, i really mean it! Actually the function you’ve to call for registering “new handler” is _set_new_handler(), not the set_new_handler(). The set_new_handler() is just a stub function, which is provided in the C runtime library to compile the STL. Its not indented to be used by us. See the correct usage of _set_new_handler() below.

#include <iostream>
#include <new.h>

// Memory allocation error handler.
int MyNewHandler( size_t size )
{
    std::cout << "Memory allocation failed!";
    return 0;
}

void main()
{
    // Set the new handler
    _set_new_handler( MyNewHandler );

    int *pi = new int[500000000];
}


This often happens when you use third party application such as Visual Assist for improved intellisense. The function it auto completes can be an existing one, but may waste several hrs of effort. isn’t it? ;)


Targeted Audience – Advanced.