Thursday, August 26, 2010

Compiling cpp with gcc

Hi,
I know the title is a bit confusing. But believe me, i am not trying to confuse :).
In very layman terms , g++ is nothing but a special version of gcc.
Ignoring all the complexities, the most trivial work it does is making cpp the default
language, which results in stdcpp library to get linked by default.

So, if you want to play a trick and compile your cpp code with gcc, just link the stdc++ lib by yourself.

$ gcc file1.cpp -lstdc++ -o output

This will do the trick.
Have tested it with basic helloworld, so if your complex template containing code doesn't compile with this, don't complain, more over you should give a thought about what template is actually doing to your code :).

Tuesday, August 24, 2010

The Old "new"

Remember the good old C days,
When after memory allocating code,
We had to check the pointer against NULL, just to make sure that the memory has actually been allocated.

int * arr = (int*)malloc(sizeof(int)*10);
if(arr==NULL)
{
// No memory allocated
}

Many people carry this habit from C to C++, which is not all together right.
As in CPP new operator throws an error, "bad_alloc" to be precise , in the case where memory was not allocated.

But if u like living in old days, and insist on comparing the pointer to NULL to check whether memory has been allocated or not. You can use

A * a = new(nothrow) A();

Now you can keep on reliving those times.

On a more serious note, this practise is not recommended in C++, but in some projects developed before C++ introduced "exception handling", this practise has to be continued.

Saturday, August 21, 2010

Dual booting on my phone

Hi Guys,
Finally i was able to install NITDroid android port to N900.
Been trying for over 2 months now, the problem was with my SD card.

Anybody wanting to try it out ( and having a N900 is a must :) ), can go to the link given below.

But having a wifi connection is a must, Data downloaded is over 100 MB.
Or there is another way.

Can download the required files on your PC.
Then on your "N900" open the terminal. Goto /home/user/MyDocs
Make folder named .nitdroid
paste all the files downloaded on the pc to this folder.

Then start the nitdroid installer, it will just pick up the files from the newly created folder.
Neat isn't it :)

Having a phone which dual boots is a dream come true, one of my ultimate tech fantasies.
Currently i have both Maemo 5 and NITDroid on my phone, and am waiting for meego community build for N900 to comes out sometime in october, so that i can triple boot and attain Moksha :)

NOTE: If anybody is facing problem post install in getting the NITDroid to work, try using some other SD Card, it might just work for you.


Friday, June 18, 2010

awk------ a jewel...

Hi,
Its been a long time friends, since i last posted.
So what's better to reboot the posting spree than Linux.

awk is the focus today.
This post is not supposed to be an extended tutorial, but just a reference for novices and first timers.

awk is not just a simple command, its an interpreter, well atleast sort of.
you can write awk scripts, and rule the world :)

consider a condition in which you want to print the second column of every line of the input, which can be a file or even the output of some command piped through.

ls -l | awk '{print $2}'

this command does it all, takes the input from ls command and prints the 2nd column.

and its just not that you can even use if/for/while etc.

So, jump into the depths of awk.
More is coming.

Monday, May 24, 2010

N900:- absolute beauty

Just got my brand new N900, and belive me friends when i say, AM LOVING IT.
Am writing this post from my N900 itself. This phone has got a variant of debian on it. And with intel and nokia's Meego OS coming soon, this phone will have a multiboot options in Maemo and Meego.

Will post more about my adventures with this wonderful device incourse.

Monday, April 26, 2010

Searching 2.0 ---- new frontiers

Searching has entered a new domain with introduction of Google's "Goggles" service, Although goggles is a comprehensive service using which we can make searches based on images, landmarks, books, logos etc... but we will be concentrating on the book title searching ( or any other search which concentrates on extracting text from images and then searching for it).

As far as searching for a book or textual logo goes it can be easily implemented using OCR to extract the text from the image and then searching the text which was earlier extracted from the image.

OCR stands for "Optical character recognition". It is widely used to convert books and documents into electronic files, to computerize a record-keeping system in an office, or to publish the text on a website. OCR makes it possible to edit the text, search for a word or phrase, store it more compactly, display or print a copy free of scanning artifacts, and apply techniques such as machine translation, text-to-speech and text mining to it. OCR is a field of research in pattern recognition, artificial intelligence and computer vision.

In this blog post, We will be discussing how to easily extract text from images ourselves and then implement out own visual searching service.

For the purpose of extracting text from the image we will be using The "Tesseract OCR engine". The Tesseract OCR engine was one of the top 3 engines in the 1995 UNLV Accuracy test. Between 1995 and 2006 it had little work done on it, but it is probably one of the most accurate open source OCR engines available. The source code will read a binary, grey or color image and output text. A tiff reader is built in that will read uncompressed TIFF images, or libtiff can be added to read compressed images.

As a wrapper for this engine we will be writing a script in python using an opensource python project called "PyTesser".

PyTesser is an Optical Character Recognition module for Python. It takes as input an image or image file and outputs a string.

PyTesser uses the Tesseract OCR engine (an Open Source project at Google), converting images to an accepted format and calling the Tesseract executable as an external script. A Windows executable is provided along with the Python scripts. The scripts should work in Linux as well.

The example we are going to discuss here is tested on python 2.5, but it should work with newer versions barring version 3.0.

Another component which needs to be added to the python2.5 is PIL. It is required to work with images in memory. PyTesser has been tested with Python 2.5 in Windows XP.

So, now moving on to more exciting part, i.e actually getting something done. Download and extract the PyTesser into the scripts folder of python25 dir. Tesseract OCR engine's binary is included with the PyTesser archive.

We have used IDLE editor for python coding, any other can be used. Eclipse too can be used for python development with "pydev" plugin installed.

After setting up the environment paste this code in the newly initiated project/file.

from pytesser import * # import all packages from pytesser script
image = Image.open('fnord.tif') # Open image object using PIL
print image_to_string(image) # Run tesseract.exe on image
print image_file_to_string('fnord.tif')


We have used the example image provided with the PyTesser package here. Running this code will analyze the image and after extracting the text embedded in the image will print it on the console.

So, now we have to use the extracted text for searching.

This relatively simple thing can be used for multiple scenarios, one can be getting info about the latest offer for a product, or searching for different branches of a chain of stores. Lets discuss the endless possibilities and unleash our creativity here.


Download Links:

python imaging library ( PIL ) : http://www.pythonware.com/products/pil/
PyTesser : http://code.google.com/p/pytesser/
Tesseract OCR engine : http://code.google.com/p/tesseract-ocr/

Saturday, April 17, 2010

Structures and Classes in c++

struct keyword is facing the downside in comparison of class because of its strong attachment to the legacy C code.
But most people ignore the fact that structs have been totally reinvented in c++.

The only difference between struct and class in c++ is that by default everything is public in structs and private in classes, even the default inheritance in struct is public.

thats all the difference between structs and classes, then why in recent programming practices structs have been totally ignored.

structs can be safely initialized using constructors, they can have polymorphic nature, support late binding, in short everything you need to write good OO code.


check out the code given below.

#include
using namespace std;

struct Whatever
{
virtual void foo() { }
virtual ~Whatever() { }
};

struct Derived : Whatever
{
void foo() { cout << "value=" << value; }
Derived(int n) : Whatever(), value(n) { }

private:
int value; // private data member
};

int main()
{
Whatever *pW = new Derived(10);
pW->foo(); // virtual call
delete pW;
return 0;
}


IMHO struct are fighting a loosing battle because of the mindset of the developers, We have been taught C++ means classes, C++ even started out as "C with classes".
anything which needs an aggregate data type can be implemented using structs and anything needed as a complex data type adhering to OOPS principles, representing some real world object needs to be implemented in terms of classes.

There is no problem in using classes or structs for that matter, just need to open our minds to the realities of C++ , rather than following and believing in myths.

Saturday, March 20, 2010

const in c++ ---- storage and linkage

This is the one thing which most people are unaware of in c++. Constants do not behave like normal types, i know you must be thinking that you knew this already, they are constants.


but wait for me to finish.


1.> STORAGE POINT OF VIEW


globally declared const variables are not allocated any storage space by default, compiler makes a mere entry of it in its symbol table.


This is done for the sake of optimization, as compilers in case of const implement constant folding.


Any other global variable ( non const ) is allocated memory.


But we can force the compiler to allocate memory to a const global variable by


--> passing it to a function as an argument


--> storing its address


Its because of this, linkage for const variables are also imlemented in a different manner.


2.> LINKAGE POINT OF VIEW


const by default has internal linkage, this is due to the fact that its not allocated any memory.


Consider the expression


external int a;


This line tells the compiler that this is a mere definition and declaration for this variable is given else where. When a variable is declared it is allocated some memory, which is not the case with const variables, Hence the need of keeping the default linkage as internal.


As we have seen earlier how compiler can be forced to allocate memory to a const variable, well there is one more way


By explicitly declaring the const variable as external. Which changes its linkage and as its linkage is external now compiler allocates the memory to the variable.



Hence for making a const variable external we need to specifically declare it as


extern const int a;



Hope this make the things a bit clear.


Monday, March 15, 2010

Free Software : The Practical Perspective

Well today's post is about the philosophy of computing.

And I in no way am an expert on this, its just what i see after a very short career in software field, may be my views are wrong but this i what i have understood so far. So please bear with my ignorance in case i am wrong.


The good versus evil debate has been raging since Richard Stallman started the “Free Software Movement” at the MIT lab.

Before starting off with the article i would like to make it clear that “Free Software” and “Open source Software” are two completely different things. Free software is always free, non free software is seen as a social evil, on the other hand Open source software is not always free.

As specified by RMS is his essay “Why Free Software is better than Open Source”

The fundamental difference between the two movements is in their values, their ways of looking at the world. For the Open Source movement, the issue of whether software should be open source is a practical question, not an ethical one. As one person put it, “Open source is a development methodology; free software is a social movement.” For the Open Source movement, non-free software is a suboptimal solution. For the Free Software movement, non-free software is a social problem and free software is the solution.


Now coming back to the article, RMS in an interview in the Documentary "Revolution OS" says free software is a way of living in the society,small children are taught in schools to share their lunch with others, good neighbours help each other, its a way of living in harmony with the society.
I completely agree with RMS here, but only on a philosophical level, what he suggest here is a perfect condition of a perfect society, but the society is not perfect. every thing cannot be shared. you don't share your money with others, software is a way of earning money for people, programmers earn their living with it.

Don't get me wrong , I myself use Slackware and Ubuntu on my machine, and the proprietary drivers provided by ATI for my graphics chip works better than the opensource alternative, so i use the ATI ones and have no problem in doing so.
What RMS is preaching is a world altering philosophy, but as it happens with every other radical preacher, it will not be accepted by the masses, unless until by some unseen trend in market makes software profits redundant.

And i am also staunchly opposed to the closed sourced corporation like microsoft n googles of the world, Opensource movement seems to be the correct way to go right now. We have to attain a balance and live with the necessary evil, the need to earn money.

Monday, March 8, 2010

Intel 8086 Memory addressing architecture

Before Coming to the point, a small history lesson for everybody.
The first microprocessor launched by Intel was 4004, specifically designed for a Japanese firm making calculators.
The Intel design engineer conceived the idea of producing a general-purpose programmable chip, instead of the custom logic for each different customer that was the rule at the time.
So they came up with a 4-bit microprocessor, which was runaway success.

This was how Intel started, after 4004 Intel came up with many successive microprocessors, like 8008,8080,8085,8086...
Out of all these Intel's 8086 is the one we are going to discuss.

8086 was different from its predecessor, the Intel 8085.
8086 had 20-bit address bus, thus enabling it to address memory up to 1 Mb. But the processor itself was 16 bit.
So how was this possible, it should have been able to address only 2^16 i.e. 64 Kb of memory.
Intel achieved this by overlapping 2 16 bit words to form 20 bit address. Interesting point is that they could have used these 2 16 bit words to get 32 bit addresses, thus resulting in being able to get addresses up to 2^32. But this was considered unnecessary at that time.

Intel used the first 16 bit word as offset address and second 16 bit word as segment address, 4 segment registers present in 8086 did all the bit shifting and addition required.

And as MS-DOS was created for IBM pc around this time, which in turn used Intels 8086 chips, it had 1 Mb limit the address space, out of which 640 Kb was taken by MS-DOS itself, remaining 640 Kb was used for user processes. But that in itself is a different story altogether.

Hacking grub2 ---- part 1

Hi,
Ubuntu 9.10 karmic koala ships with Grub2 now, and they have implemented structural changes in the new grub system.

So, i'll be taking u guys along on the journey of discovering this new grub system and modifying it along the way.

The trick we are going to see today lets us play with the boot menu.
By changing names of few file or by replacing some we can change the order of grub boot menu, and even remove some entries from the boot menu.

These file are stored in /etc/grub.d folder

These files are

1. 00_header
2. 05_debian_theme
3. 10_linux
4. 20_memtest86+
5. 30_os-prober
6. 40_custom
7. README

these file are read in order of their names and their entries are created in a read only configuration file for grub
We will get to that file later.

Now by default according to the file names the boot order is

linux kernels
memtest utilities
other os ( windows )
other custom kernels

So if we want windows to be placed before linux in the grub menu
All we have to do is change the name of the file 30_os-prober to something alphabetically smaller than 10_linux

And run update-grub2 as sudo to recreate the grub.cfg file with new settings.

Also if u want to completely remove the windows entry from the boot menu
Open a terminal
Go to the directory /etc/grub.d/
And rename the file 30_os-prober to .30_os-prober, don't forget the "." before the new file name.
This can be done by issuing the command "mv 30_os-prober .30_os-prober"

And don't forget to run update-grub2 command as a root.

To bring back the windows just rename the file back to 30_os-prober and run update-grub2 command.
And you will have your windows back.


Well guys, that's all for this post.
Stay tuned for more.

Mystery behind a.out in unix/linux

Many people are intrigued by the output file a.out formed as a result of compilation of C/C++ code on linux environment.
Why a.out?

Well, the reason behind this is more historical than logical, a.out stands for assembler output, although in case of C/C++ its actually linker output that is the final product, On the PDP-7 (even before the B language), there was no linker. Programs were created by assembling the catenation of all the source files, and the resulting assembler output went in a.out. The convention stuck around for the final output even after a linker was finally written for the PDP-11; the name had come to mean "a new program ready to try to execute."

Thursday, January 28, 2010

So We Begin !

Hi everybody!

This is going to be a place where i'll post things which will catch my interest during the day to day indulgence in the geekiness we all love.

I'll be reviewing some hardware/software i'll come across.
some cool tricks and tutorials for the c++/networking/communication protocol enthusiasts.

Am not an expert , will never be, as there is so much to learn, a never ending ocean of knowledge.

Join me o fellow geeks, as i impart on this journey of discoveries, to quench my thirst of knowledge, and hope we all are benefited in some way or the other.

Regards,
Ishan