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

Saturday, May 12, 2012

Funny C++ Declarations

I was browsing through GNU website and I found a page full of fun. Here are some examples from that page...


1. struct dumb by[sizeof member];
2. struct by_lightning;
3. short circuit;
4. void if_removed();
5. unsigned check;
6. class dismissed : public annoyance
7. long trousers_with_holes;


For more fun head to source http://www.gnu.org/fun/jokes/declarations.html

Finding a book on Design Pattern

So far I have used singleton and factory model. But my understandings on design patterns are limited. So I decided to go in depth into it. I followed my usual book searching procedure...
     
1. Search in google and go through plenty of blog posts for book suggestions and review. Gather some names.

2. Next come to stack overflow and check what book they are gossiping about?

3. Now take out the common names and head to google books.

4. Here you can see preview of your books along with websites in your country, who actually sell them.

5. One draw back here is price comparison is not shown on google book. So you have to manually visit each and every site to find the best deal. My personal favorite is flipkart.com , just because of the quality of their service.

I have found 4 books interesting.

1. Refactoring: Improving the Design of Existing Code : 
    It's a classic book and praised by almost everyone. But some complained that it's a bit dated.

2. Clean Code: A Handbook of Agile Software Craftsmanship :
   My guess is this one will give more value for money than any other choices. It talks about a                          more wider subject. 

3. 
Head First Design Patterns : This one looks catchy. Just like other books in this series, it attempts to explain all complex stuffs in a visual manner. I guess I will settle for this :)  

Why not to use void main

In college, while using turbo c, we learn to use "void main()". But this is against c++ standard. Actually this shouldn't compile. Main function must return a int value. Main function should be declared like "Int main()". And at the end "return 0" may be added to indicate that function executed successfully. It may return 1 in order to indicate that some error has occurred. This is also referred as Exit Status. It can be EXIT_SUCCESS (traditionally 0) and EXIT_FAILURE. If return is not provided by programmer, it will return 0 by default.  For other exit statuses you may have a look in stdlib.h(cstdlib).

Friday, May 11, 2012

QVector Advantages


In our workplace  we use stl as much as possible. Even when coding in qt we are advised to import and use stl containers. I googled this issue to know justifications behind this favourism. What I found is STL is a clear winner if speed is single most important concern. On the other hand qtl has many advantages of it’s own. Let me elaborate these points with an example of vector. QVector is more easier to use than stl::vector. Qvectors are more light weight. Whenever we copy or pass qvector , only a pointer to vector is passed. A deepcopy is used only when editing data. QVector occupies more memory to prevent use of memory allocation operations each and every time expansion is required. It uses realloc() to grow by increments of 4096 bytes. Qvector has lots of interesting functions to make the life easier for developers. It has a squeeze option to remove extra spaces allocated. So I believe it’s advisable to use qvector as much as possible. And in case you want to use stl:: vector , you can always convert to stl::vector like this
 QVector vector;
 vector << 1.2 << 0.5 << 3.14;
 std::vector stdvector = vector.toStdVector();