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();  

Assignment operator in RVALUE

I came across a line of code which looked a bit strange to me. It reads like this..
x= m * (n=7);
The above code is actually equivalent to
n=7
x=m*n

Actually c++ supports using assignments on the right hand side of assignments operator. And we are all familiar with that. I bet you must have seen assignments like this…
a=b=c;
This and the previous code follows the same rule.

Calling constructor from constructor


I was trying a few whiteboard programs, just for fun. In one of them I called a constructor from within another constructor. Code compiled without any fuss,but code didn’t worked the way I was expecting.  I found out that when calling a constructor from within a constructor a temporary unnamed constructor is created. That is the reason why program is not behaving the way I was expecting. Check out this…


using namespace std;
class foo
{
int x;
public:
foo()
{
x=20;
}
foo(int d)
{
x=d;
foo();//value should be changed to 20, but it wont
}
void show()
{
foo a;
a.show();// will print 20
foo b(10);
b.show();// will print 10. I was expecting it to print 20
return 0;
}
(This one is from my old blog.)

Thursday, May 10, 2012

Constant functions and Mutable

For a c++ rookie this syntax may look interesting ..

void myFunc() const;

Normally we love to see const at the beginning of a line . like "const int i = 10;". Actually above line is perfect. It tells that the function can't modify it's calling object. It is referred as read only function in some text books. It can call some more constant functions from inside. But it can't call any normal function.  This type of functions can be called from any ordinary functions as well as constant functions.

Suppose you want to declare a constant function, but at the same time you want it to modify some particular data. In that case you need to use mutable key word while declaring your variables.

 mutable int x;






Saturday, May 5, 2012

A new project(Data structure)

I was looking to start something new for a while. Am planning to work on a new STL like project. It's kind of template library. What we planned is using hash tables underneath a wrapper. User will use vector , map and other stuffs. Our library will save the data in a hash map. We plan to use this algo. My first stop is reading  G. M. Morton's paper on hashing. Will setup sun studio tomorrow. Lets see what comes next :)

Saturday, February 4, 2012

Function Pointer


Just like variables, functions also has their own memory addresses . So we have pointers for them too.
void (*fPtr)();// a void function pointer declaration which takes no args.
fPtr = &myFunc;
(*fPtr)();

int (*ptr2)(int,int);// a int function pointer declaration which takes 2 int args.
ptr2 = &myOtherFunc;
int i=0,j=2;
int result = (*ptr2)(i,j);

#{Header}

Hi,
    It's late night in 4th feb. I was wasting time re inventing wheels , i mean i was working on some basic c++ codes. I have practiced them a number of times, but at present i couldn't remember them. I think it's time to make a note of all these. So I am creating this blog for myself. I will post all cpp related stuffs which i feel necessary for me. If you find this site useful, please get in touch. I would love to see you as a contributer.

Regards
Rwik