Random Thoughts #3

There’s peace after the storm for those who are courageous enough to brave it.

All stories are worth telling, but not all  are worth listening to.

When his actions failed, his silence prevailed.

 

Advertisements

Nested Functions in C

Yes, you read the title right. No, I’m not crazy , OK maybe a bit (no pun intended). Maybe I’m sleep deprived, I honestly don’t remember when was the last time I had a good night’s sleep but it is true, C does support nested functions, All that you learnt about C was wrong (Gasp!!) Don’t trust me? Fine, go ahead and try the following code out.

         #include<stdio.h>

         int main()
         {

           void foo () {}return 0;

         }

  GCC compiles it without a hitch. See, I told you! (again, no pun intended)I search around and figured out that ANSI ( C99 ) does not allow for nested functions. However, an addon for GCC provides support for it. I haven’t checked out its scoping rules or made any progress along that path. Do let me know if you find this out.

K THNX BYE

Numbers every Programmer should know

When discussing about Memory Hierarchy in today’s ( 25th Sept. 2014) OS class, I assumed that for those paying attention the sheer magnitude in access times between the different levels of the hierarchy should’ve been mind-blowing to say the least.

While the discussion continued, I recollected something I read a long time back. It went by the Title “Numbers every Programmer should know”. It was a listing of reference time for accessing various memories (Cache, Registers, HDD, Internet .etc) . The numbers were part of a talk given by the legendary programmer , co-inventor of mapReduce and Google Fellow Jeff Dean during an internal talk on building large scale distributed systems. This was later publicized by Peter Norvig, Head of Research at Google. The list is extremely interesting and the way the numbers have evolved over time show the pace at which technology is developing. Take a look for yourself.

Try guessing where current day computers are spending most of their time on? You’ll realize that they spend most of their time waiting, waiting for data to process.
This is fine for personal computers but in data centers, servers and other applications where performance is the key, this is just not acceptable. This was one of the main reasons for developing event-driven models for servers. Nginx and node.js are two examples of event-driven servers

This video gives a very good introduction to node.js. It also talks about the waiting problem and how node seeks to solve it.

 

As an addendum you can check out the numbers in big data, 6 PB, that’s the amount of chat data facebook has ( all our Awww!!! , Ohhh, hmm, :), 😦 , :\, >.< , 😛 all are there)

http://doubleclix.wordpress.com/2011/11/01/bigdata-counts/

References

http://colin-scott.github.io/blog/2012/12/24/latency-trends/
http://www.cs.cornell.edu/projects/ladis2009/talks/dean-keynote-ladis2009.pdf
http://norvig.com/21-days.html
http://doubleclix.wordpress.com/2010/11/11/google-a-study-in-scalability-and-a-little-systems-horse-sense/
http://doubleclix.wordpress.com/2011/11/01/bigdata-counts/
https://gist.github.com/hellerbarde/2843375
http://www.cs.virginia.edu/~evans/talks/cs390-s04/

Pass-by-Reference in C

Unlike modern programming language, C has always been mysterious to even the most experienced of developers.
There’s always some feature of C that will surprise you even when you’re sure you know it inside out.

This is because C was written during a time when men were men and wrote their own operating systems in machine language.

So it was quite common for the creator of the language to put in quirks that made sense only to him. No one back then thought computers
would become common place.

So when the digital era kicked it, UNIX and C were behind its success, pushing it even further.

And C has faithfully carried forward these quirks to this date and I’m surprised every time I discover one.

Here’s an hypothetical, an interviewer tells you “C has only pass-by-value” , how would you support his argument?

It’s your first interview, and you’re already Stephen mcSweaty-Pants and he throws you this bouncer.

Quick! Think!!

Consider the following code snippet

void foo ( int * bar, int * baz)
{

//Do Something
}

You show this code to him and point to the header, pointers!!
you will be passing references to integer variables, surely this is pass-by-reference?

He still insists that C has only pass-by-value, you now think that he’s gone mad and that you probably won’t land this job.

Then he tells you consider how you would call the function

int x,y;
foo( &x, & y);

Now take a long look at that second line.

“Yeah, I’m passing references to x and y as required”

True, but try elaborating it a bit more (no puns intended)

“You are passing the VALUES of the memory address at which the values are stored”

BINGO!

To elaborate, the memory addresses of the actual parameters are copied onto the locations pointed to by the formal parameters
and and then the evaluation occurs.

So even when you’re passing references, you’re passing values of the addresses, which is effectively pass-by-value 🙂

Question: So how would you pass by reference in any language?

Turns out, Pascal supports pass by reference by using the var keyword.

procedure swap ( var x: integer, var y : integer);
var z : integer;
begin
z :=x; x:=y; y:=z;
end;

In Pascal, a formal parameter becomes a synonym for the location of an actual parameter.
keyword var is used to indicate this.

Inspired by and Pascal code snippet from “Programming Languages : concepts & constructs, Second edition” by Ravi Sethi

TL;DR

Its been a long time since wrote something substantial in a human language. In that time, I have had numerous opportunities to introspect on many things like human relations, how likely is it that I create new ones .etc. All the while, I was aware that I was breaking my promise of one tech post per week.

The hiatus was not intentional, but it did provide a lot of data and many questions have been answered. My last post on Apache web server was agreeably long and I realized that I was trying to pack in too much info into one post. Why would people read anything that long? I’m not an authority on webservers so people don’t have a motive to read it. Even as a writer, its taxing to write a long article as you need to keep a lot of things in mind and proof-reading is a real pain.

I have read some posts, really really long posts by extremely knowledgible people and I have thoroughly enjoyed reading each one of them. Some of the more memorable ones are
1. http://www.allthingsdistributed.com/2008/12/eventually_consistent.html
2. http://www.joelonsoftware.com/articles/fog0000000332.html

There are quite a few, I’m just too lazy to dig through my bookmarks collection

I know that I’m far from being called knowledgible, hopefully one day I’ll be called that 🙂

In the mean time, I have decided to stick to writing small posts, easy for people to read and understand. [Rule of Thumb: If you can split it into several distinct sections, you can split it into several posts]

No promises this time, I’ll write as much as I can, for my satisfaction. If people like it, awesome. If not, I’ll just have suck it up.

TL;DR
I’m back from a slumber, no more long posts, awesome techy and life stuff to come
(Woah, that was easy)