Friday, August 10, 2012

Two very annoying mistakes

I am very good at doing silly mistakes in c++ syntax. Here are two of the most disturbing mistakes.
1. Forgetting to put ; after class declaration :
   class mb
   {
int a;
   }

  Error :  "expected unqualified-id at end of input"

2. Forgetting to leave a space after ">" in map declaration.
  map<string,vector<double>> *a;
    Error : ‘>>’ should be ‘> >’ within a nested template argument list
    Correct syntax : map<string,vector<double> > *a;

The second one is more annoying . It made me google many a times :)

Thursday, August 9, 2012

Limiting memory use to a % of total memory

One of my friend was very annoyed with his application. He thinks there is one unknown process which occasionally eats up total memory available. So he was asking , if there is some way to limit memory usage to a predefined portion of total available memory. I googled , and most people say this is indeed a very very bad idea. Applications should not measure ram and occupy. If ten applications are coded, such that they occupy 1/4th of ram, and they are ran together, then crash is inevitable. In the process I learnt a way to get memory related stats on a unix system. Just parse the file at /proc/meminfo

{Used cat to see meminfo}
Otherwise  a system wide setting can also be changed using ulimit .  For more info read here. {Read the warnings at bottom first.}

Tuesday, August 7, 2012

Double linked list using single pointer

At first I thought it's impossible . After all a pointer can point to one memory location at a time, But then I read this insightful discussion on stackoverflow.   And here is a possible solution to the problem http://everything2.com/title/Storing+a+doubly-linked+list+using+just+a+single+pointer+field