Sunday, November 29, 2009

Music - My Passion - Raaga Raagini

Follow my favorites on http://www.youtube.com/mrsandeepnaidu. You will definitely like it. My favorites range from Hans Zimmer, Old Telugu Devotional Songs and of course my Guruji, one and only, His Holiness Sri Sri Sri Ganapathi Sachchidananda Swamiji.

Among others Maksim is a great Piano Maestro. And I did come close to attending a Seattle Symphony Concert but could not do it without a company. Same about concert in Lincoln Center. Well I do feel a little sad about that but am Happy that I have attended a lot of concerts of Sri Swamiji in Nada Mantapa and other places in India. You know Nada Mantapa is a rarest of the rare uniquely built acoustically engineered by Sri Swamiji, to bestow his graces to the world in the form of the ancient Raaga Raagini. Raaga Raagini is an ancient practice of Music which takes the musician and listeners to the heights of Yoga and Enlightment. Ancient Scriptures suggest that Lord Hanuman was the greatest practitioner of Raaga Raagini Music. From the little I know, in this practice of Music, the musician uses music to modulate cosmic energy and pranic energy. This modulation can be used for purposes like healing, and many more. Legend says that Lord Hanuman used it to melt a mountain of crystals to reveal a hidden demon and then vanquished it. Put aside the external effects, the most interesting part is that the musician/practitioner devotes his complete energy to be one with Cosmos while practising. It is as if you are meditating though your body and mind are in constant rythm to practice music. There are a very great number of people who have in modern times benefited from Sri Swamiji's concerts. The medical benefits were enormous as Sri Swamiji uses this great science to rejuvinate the Naadi System in the body and modulate the life force in a patient. This is one more unique blend where Sri Swamiji combined Raaga Raagini Music, Ayurveda and Yoga together and also added a blend of Devotion into it to encourage pious thinking in the mankind. Trust me, they really made a lot of difference to thousands of people in many ways both physical and spiritual.

A good start will be Kaala Traya Bodhitam which is my favorite for the past 15 years. Personally that inspired me to think of the cosmos very deeply when I was 17 years old. Oops I almost gave away my age to the reader. :) I don't bother. Take these Music Ensembles and listen every night or when you are free. A lot of difference will be seen.

By the way, there are 6 Raagas and 36 Raaginis. That is all I know of Raaga Raagini Music as of now.

Earth's Magnetism and Life on earth

I was reading an interesting article in GEO (Nov 2009) edition. The article was on the Earth's interiors and evaluating on how many facts were established till now. The writer Bill Bryson, who was the original author of the article, rightly pointed out that we know more about Sun than we know about the Earth. Of all the great facts put forward, like the 13 major tectonic plates and their movement and the fact that the plates are just wafers floating on the liquid mantle and much deeper into the mantle are many unknown phenomena, the single most thought provoking subject was about the consequences of convectional currents in the liquid mantle that can/will result in the reversal of the magnetic patterns of the earth.

I started thinking what is the survival rate or effects of the so-called reversal. Since all biologically living organisms on/under Earth have biological clocks and systems built according to magnetic patterns of the earth since it is a very strong influential force, the reversal and the duration of the reversal can have very strange effects. Bird migration patterns will change. People who are healthy mentally and physically could survive longer if the duration where there is less magnetic force spitted out continues for a longer time. As the magnetic patterns are responsible for protecting the Earth from the continuous burst of cosmic rays from Sun into Van Allen belts, we may observe a total disruption of the civilized world which heavily depends on Electronics based on Silicon Chips and metal carriers. For that matter, what will happen for the babies not yet delivered.

Certainly, as I know, there was never a study on the effects of Cosmic Rays on Humans for long duration. But it will be one more interesting scientific studies that can make a huge difference, if we enter into a time were there is a reversal process of magnetic field of Earth that endures like 10 years (this is an assumed scenario) and the ways of human survival during that time when the Cosmic Rays start damaging our DNA.

Will some cheaply produced space suit help all mankind? Will Yoga help? (Don't laugh, it might as well do that, as it is to do with the energies in a human body). Will the humans have safe drinking water or will be contaminated? Will food produced over the soil be edible? Temperatures in the seasons of course is a big if? What will happen to the electric generators and the huge electric storage devices? Will some of the bacterial infection reign over Earth killing more species because those are the right climatic conditions for it? Will Human behavior and psychology change(I am not that worried here as it is not very great now)? Will that mean during this time there will be more convective burst from the mantle resulting in devastating earthquakes? What happens to the whole Eco System?

Sounds like a 2012 story... :) But certainly this is something that I know all of the scientist community is thinking about.

Sunday, August 16, 2009

Beethoven's Sonata is something I will never get satiated by hearing once. I hope you have somethings you like this in your life.

http://www.youtube.com/watch?v=oFSRs7iqAv8



Sunday, August 9, 2009

Bees and Butterflies

Bees and Butterflies everywhere in the world,
A stroll in the city makes you appall in awe,
A beggar is hungry and brat fills stomach and the dustbin,
A saint is wears nothin' and a damsel wears nothin',
An elder cries out for help and a leader cries out for vote,
Bees and Butterflies everywhere in the world,
A lady works like a man and a man rests like a pig,
A teacher teaches the kids and a rowdy teaches the panicked,
A parent forgoes the lunch and a son denies a dinner,
A drought comes upon and the paper prints mission to the moon,
Bees and Butterflies everywhere in the world,
A hungry thief is beaten and a corrupt politician is saluted,
A road is laid and a ditch is made,
A house is built and a hut is destroyed,
A labourer walks to his home and an owner never leaves home,
Bees and Butterflies everywhere in the world.

Friday, August 7, 2009

Sound of ....

The wind has shifted its course,
The tide drifted me along,
A small whisper I hear and look that way,
For song is being sung distant away,
I stop and walk that way,
Through the winding path under towering trees,
The flowers greet my sight and the grass willows,
The land sliding down and the way grows wide,
The way is no more and I look around,
To find my heart sing.

Perseverance

Vision is far,
Steps are small,
Journey long,
Hope is high,
Heart looks away but will drags it along,
Feet fall behind yet drift away,
Hands get thin but grow strong,
For it is thou I know leading the way.

A Solitary Journey

Blessed are the ones who can remember the gone, For those who have no one gone still feel lost, Lost in the thought, Thought of the one to remember, The eyes look out but the heart looks in, The sight of the single sign, The sign of love.

Tuesday, February 24, 2009

Forward Declarations in C++ and runtime Bug/Error

Forward declaration is a pretty simple concept to understand. As everyone knows it is a way of declaring the class names before a piece of
code that uses it (where we do not want to include header files) so that the compilation happens properly and does not complain about
missing class name symbols. Now why will someone not want to include header files and instead have a forward declaration.This is
especially helpful when there could be multiple header files with conflicting definitions (not for the class but let us say a macro like
MAX_PATH) and we do not want to include one header file versus including the other in a given cpp file. In this case if we need to use the
objects of the class whose header file is omitted in the code of this cpp file we need to have some symbol declaration so that the
COMPILATION happens properly. Later the linker when linking the .obj files will properly declare the sizes of the objects.
Now there is a very grave pitfall that can go unseen and result in a difficult to find bug that could end up you spending lot of valuable
time looking at the memory and guessing it. I took about an hour to figure this out when I happened to work with someone who wrote such
piece of code.
First and foremost the scenario is as follows. Let us look at the declarations of our problem candidates
=====
//In A.h
class A
{
public:// some func
int array1[5];
};

//In Ace.h
#pragma once
class Ace
{
public:
int var;
};

//In newder.h
#pragma once
class Ace;
class Der;
class newDer
{
public:
void * membervar; //points to an instance of Der as supposed by the programmer
void setMembervar(void*);
void * getMembervar();
Ace * getDerVar();
};
//In newder.cpp
#include "A.h"
#include "newder.h"
void newDer::setMembervar(void * setvar)
{
this->membervar = setvar;
}

void * newDer::getMembervar()
{
return this->membervar;
}
Ace * newDer::getDerVar()
{
//Uncomment this to understand what could be happening inside the pointer conversions
//Ace * someptr = (Ace*) this->membervar;
return (Ace*) (Der*)this->membervar;
}

//In Der.h
#pragma once
class Der: public A, public Ace
{
public:
int * pointer;
void execute();
};
//In der.cpp
#include "A.h"
#include "Ace.h"
#include "Der.h"
#include "newder.h"
#include "stdio.h"
void Der::execute()
{
newDer n;
n.setMembervar((void *)this);
printf("Before: Der* 0x%08x\n",(Der*)this);
Der* d = (Der*) n.getDerVar();
printf("After: 0x%08x\n",d);
printf("Pointer value diff %d\n",(unsigned int)this-(unsigned int)d);
}
int main()
{
printf("size of int = %d\n", sizeof(int));
printf("size of long = %d\n", sizeof(long));
void * someptr;
printf("size of pointer = %d\n", sizeof(someptr));
printf("size of class A = %d\n", sizeof(A));
printf("size of class Ace = %d\n", sizeof(Ace));
printf("size of class Der = %d\n", sizeof(Der));
Der d;
d.execute();
}

====

This seemingly naive looking code has a very subtle bug that can make the code crash and this bug is very difficult to scoop into when it
is production like (with lot of other code). For the sake of simplicity the above code only revolves around the problem and not any
business logic. If you run this code you will see the following output:
size of int = 4
size of long = 4
size of pointer = 4
size of class A = 20
size of class Ace = 4
size of class Der = 28
Before: Der* 0x001bfae8
After: 0x001bfad4
Pointer value diff 20

Notice the Before and After pointer values. Interesting isnt't it. I would expect the pointer value to be the same. But it is moved.
Imagine myself using d inside execute function to access the pointer member variable of Der class, like d->pointer and then use this
pointer to access some function etc...
How could the value of the address of Der change before the call and after the call. I guess you might have figured out by looking at the
extra print statements with the sizes of the classes. Yes, the bug is in the function "Ace * newDer::getDerVar()". When we assigned Der*
to the void * membervar in setMembervar everything was fine. membervar will still be pointing to location 0x001bfae8. But when we return
from getDerVar and cast it to Ace* the problem surfaces. Look carefully into the header file newder.h. The class Ace and Der are declared as FORWARD DECLARATION. Rings bells? Yes the class size of Ace* and Der* when newder.cpp was compiled was 0. So it did not put any offsetting instructions in when the obj was prepared. Usually the compiler offsets into a given pointer when it is typecasted in the hierarchy. That is missing in this newder.obj. So when the code gets executed the address to which membervar is pointing to (0x001bfae8) is passed out as Ace*. Now continuation of the bug is in the function "void Der::execute()". When this function is compiled and the statement "Der* d = (Der*) n.getDerVar();" is compiled the compiler knows that getDerVar() returns Ace* and we are telling it that this is actually a pointer to Der* which contains A and Ace in it content serially as defined. So it puts some offsetting code to rollback the pointer by 20 bytes since it needs to get to the root address of Der class and there we have our horrible bug.

This type of code is not healthy for large code bases. I usually avoid forward declarations as much as possible. Instead of forward declarations if we have header files included in newder.cpp for Ace.h and Der.h we విల్ not see this problem.

Saturday, February 21, 2009

Delay Loading using Visual Studio - Pitfall

This is a small post on delay loading using Visual Studio. As everyone knows that we can dynamically load a dll in a process. Let us say binary B1 depends on binary B2, one way of doing this, is to put a hard dependency on the B2 in the project settings of B1. This actually makes B2 load into the process where B1 is already loaded (or if B1 is the exe then B1 is the process itself) while starting the process. You can actually view this by starting your process in the debugger (Windbg) using gflags set with "Show loader snaps" checked. It shows all the binaries that are loaded when the process comes up.

The second option is to call LoadLibrary() in the code of B1 to load B2 and then call GetProcAddress(). This is the best delay loading method. As per my experience it wards off all non-execution errors in the field (once your solution is installed in the customer environments). But this is tedious if there is a large number of functions to be imported from B2 into B1 and used. You will end up writing function pointer definitions in B1 for every function that you need to use from B2.

The third option is a middle way of both the approaches. It is to delayload B2 in B1 using the project settings "Delay Loaded Dlls" in the project settings of B1 (under Linker options). But there is a specific way of doing this.

You will have to put B2.lib in the "Additional Dependencies" section as if you are hard depending(1st case) and in addition you will have to put B2.dll in the "Delay Loaded Dlls" entry in the same tab in the project settings to make it delay loaded. Once you do this, B2 will be loaded only when any code path using B2's functions or classes will be hit in B1. Until then B2 will not be loaded into the process space of B1.

Pitfall:
Here most of the developers, when they have multiple delay loaded dlls, let us say B2 and B3 to be delay loaded in B1 type the entries as 'B2.dll B3.dll' (ofcouse without the single quotes). They do this because they usually copy/paste or go with the notion of the same delimiter as in "Additional Dependencies" section. But doing so will actually tell the compiler that there is a binary called "B2.dll B3.dll" to be delay-loaded into the process of B1. But since the same entry is not found in "Additional Dependencies" section it ignores this directives and happily links it. Now the consequence is that B2.dll and B3.dll are linked as hard dependency into B1. And you wonder why they are getting loaded like this (let us say because of package differences B2.dll and B3.dll do not get installed always along with B1.dll and so result in load failures of B1.dll when you think everything is right).

The delimiter in the delay-loaded section is a semi-colon ";". If we put the entry as 'B2.dll;B3.dll' in the "Delay Loaded Dlls" section then everything goes fine as expected.

Second pitfall is that if you have a delay loaded dll B2 in B1 and in course of time the code in B1.dll that referes to B2.dll is yanked out (changing code because of a any reason), then the B2.dll's delay load directive you have put will be ignored during linking. Consequence is that your binary B1.dll now hard depends on B2.dll!