Friday, October 24, 2008

I know this code does not work as expected

Well I came across this amazing piece of code which does not crash as expected.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    int array[4];
    for(i=0; i<=8; i++)
    {
        array[i]=0;
    }

  return 0;
}

This seems to be perfectly ok code. Obviously it falters at the point where I set value to the array at index 4 - 8. Everyone would generally expect it to crash, but amazingly it did not. I tried it on wxDevcpp and it runs in an infinite loop. I was amazed at why it did. Its interesting to know what happened.

Let me first explain what actually happened. Casual observation revealed that the for loop runs forever. It seems that value of i is reset to 1 each time it reaches 7. Wow this is amazing. But what happened.

What happened is simple. Our incorrect program overwrites 'i' when it tries to put zero at array[7]. That because &array[7] is same as &i. Why? well thats because our amazing manner in which local variables are stored in the stack. So what happens is i  is there in the stack some place above array and we happen it make is zero each time.

Amazing simple reason why this kind of crappy thing happens.

Thursday, March 6, 2008

virtual function -- the key to OOP

I actually never understood this concept until I had to code something in it. It was really awesome to realize what this virtual keyword can do. I actually believe that we should make all our class member functions virtual. Though this may generate some sort of an overhead but I feel that the compiler optimization should take care of it. The main use of this virtual keyword is in cases where we need to reuse certain classes. Often the framework in which we are working in identifies a single base class. so now when this framework wants to make use of the new class, it can do so without any problem. Though the framework calls the same function it would now be able to access the function modified for the new class. Often when we have written code for drawing certain objects on the screen, we would have anticipated line, circle, triangle and a polygon. But later we realize the need for having quadrilaterals and squares also as they are frequently used. So in this case had we made use of a base class shapes, which would have had a virtual function called draw. The framework would just call 'draw' for the entire list of shapes it had. Since the 'draw' is virtual, you can expect the object specific 'draw' to be called. So you can easily extend you library. This is also called factory pattern. But this is not the only pattern to use virtual, there are others also but this example give us a very good reason to why we need virtual function, though there are ways to live without it also. But why use a knife to cut a tree when u have an axe.

Friday, February 29, 2008

inline functions, the good, bad and ugly

I learnt while reading from C++ FAQ Lite at http://www.parashift.com/c++-faq-lite/ about the good, bad and ugly aspects of inline functions. So here they are

Inline function can make you code faster if used, but then if its used a lot of places as inline function, the code bloats up. This can also lead to more page faults and hence leading to serious performance issues. Sometimes function optimizations can actually lead to a smaller code being generated because code for function calls(basically stack usage) is optimised.

Did I say page faults can increase, but then if there were two function on different pages, then thrashing could have been more without inline functions. Also its more likely that code would be at the boundary of cache, so cache memory thrashing is induced there.  But then again if the code is at right in the middle of the cache then fetching code of another function in another page would take toll on memory via thrashing.

So is inline good or bad. Now that something very subjective I would say. Just like not every garment looks beautiful to the customer, inlineing is also based on the usage. A simple direct answer may very well be wrong.

Tuesday, February 26, 2008

inline function, a move over #define

One the feature of C++ that I find very cool is inline function. It actually kills all those crazy interview questions which are asked just to confuse you over brackets. I dont remember ever using the complicated #define in C to anything more than example programs. Crazy part of inline funtion is that though u code almost as a normal function call but it gives the advantages of writing code right where u called the function. No more making sure that you have brackets in place and how its going to expand at all the places, how its going to take in the parameters passed to it, how will the return value be determined. Dump all that. How to stuff is here

void foo()
{
  int a,b, c;
  c = bar(a,b);
}

inline int bar(int a, int b)
{
  //Just do something here
   return something;
}

So you have the function foo calling function bar, but as bar is inline code for it would be placed right where it was called in foo. So we see how inline function has simplified life in C++. Any question of yours in this regard would be appreciated and I would try my best to answer it.

Monday, February 25, 2008

Multiparadigm Programming

I had never ever known if any term like that ever existed in this world. But recently I did. Now what it actually means is that instead one we use several programming paradigms to the best of effect while coding.

As Leda designer Tim Budd holds it: The idea of a multiparadigm language is to provide a framework in which programmers can work in a variety of styles, freely intermixing constructs from different paradigms. := (wikipedia)

From what I understood is that C++ provides us means to make use of objects and design patterns as well as give us the ability to make use of Templates. That is to say that we make use of OOPS as well as generic programming paradigms makes C++ really powerful.

Saturday, February 23, 2008

Why is this blog here

I am software guy. I love programming in C/C++. I feel these two are the coolest of all available in the world. C is actually a sea and C++ is obviously one step ahead. No one can completely learn it. Of late I have not been into either and have not been learning more about it. So I hope with this blog I will be learning as well as keeping the knowledge somewhere easily accessible to me as well as other. I hope this blog makes good reading for all. My general blog is at http://the100rabh.blogspot.com