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."