Auto redefined in c++ 11

In previous versions of c++ auto was just a storage class specifier, which can be used for declaring variables of local scope. By default all the variables were treated as auto. So the “auto” keyword was rarely used. In c++ 11 , auto makes programmers life, a bit easier. Just like PHP or any other modern language, auto now enables us to declare variables without thinking much about the type of data .  But do remember to initialize the variables while declaring.

A>     auto x = 23;  // will create an integer with value 23

B>     double f() ;

auto d = f() ; // here f is double

C>    vector<int> m;

auto it = m.end(); // here it is of type “vector<int>::iterator”

 

 

 

Playing with multiple inheritance in cpp

We all know about that diamond problem..

A

B       C

D

This is a perfect situation for multiple inheritance. Here A can be reached in two way from D. So if we write a code without virtual keyword, a’s constructor will be called twice, which will create ambiguity about which object we are referring to.

Without using virtual keyword constructor call will be like this :-

A’s constructor called
b’s constructor called
A’s constructor called
c’s constructor called
d’s constructor called

After using virtual keyword :

A’s constructor called
b’s constructor called
c’s constructor called
d’s constructor called

As you can see A is accessed only once . Now suppose there are a few more classes above d. Some parents of d are using virtual keyword to access grandparent A, and some parents of d are not using virtual keyword. So what will happen in that scenario ?

Answer is very simple. A’s object will be created only once for classes using virtual keyword, for everyone else who are not using virtual , separate objects of A will be created.

Here I have added one class m which is parent of d and accessing A without virtual keyword. Output is like…

A’s constructor called
b’s constructor called
c’s constructor called
A’s constructor called
m’s constructor called
d’s constructor called

For B and C one A object and for m a separate A object is created.

Here is the code to experiment with

#include "iostream"
using namespace std;
class a
{
public:
a()
{
cout&lt; }

};
class b: virtual public a
{
public:
b()
{
cout&lt; }

};
class c: virtual public a
{
public:
c()
{
cout&lt; }

};
class m: public a
{
public:
m()
{
cout&lt; }

};
class d: public b, public c , public m
{
public:
d()
{
cout&lt; }

};

int main()
{
d obj;
return 0;
}

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 🙂

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

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_FAILUREIf 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).

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.