Valgrind: Memory Mismanagement Detector

Download


wget http://valgrind.org/downloads/valgrind-3.11.0.tar.bz2

Install

bzip2 -d valgrind-3.11.0.tar.bz2
tar -xvf valgrind-3.11.0.tar
cd valgrind-3.11.0
./configure
make
make install ____ ## Usage

An example:

__ valgrind –tool=memcheck –leak-check=yes __

Traffic Matrices for Switching Simulations

Let be the average arrival rate of the entry in the traffic matrix, let be the normalized load, let be the number of (input or output) ports. Let .

  1. Uniform:
  2. Diagonal: , , for any , and for all other .
  3. Log Diagonal: and

Examples: , .

Uniform:

  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 
  0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250   0.1250 

Diagonal:

  0.6667   0.3333   0.0000   0.0000   0.0000   0.0000   0.0000   0.0000 
  0.0000   0.6667   0.3333   0.0000   0.0000   0.0000   0.0000   0.0000 
  0.0000   0.0000   0.6667   0.3333   0.0000   0.0000   0.0000   0.0000 
  0.0000   0.0000   0.0000   0.6667   0.3333   0.0000   0.0000   0.0000 
  0.0000   0.0000   0.0000   0.0000   0.6667   0.3333   0.0000   0.0000 
  0.0000   0.0000   0.0000   0.0000   0.0000   0.6667   0.3333   0.0000 
  0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   0.6667   0.3333 
  0.3333   0.0000   0.0000   0.0000   0.0000   0.0000   0.0000   0.6667

Log Diagonal:

  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 
  0.5020   0.2510   0.1255   0.0627   0.0314   0.0157   0.0078   0.0039 

An example of C++ Polymorphism

Source code:

#include <iostream>

using namespace std;

class A{
public:
    virtual void vFunc()
    {
        cout << "A::vFunc() is called!" << endl;
    }
};

class B: public A{
public:
    virtual void vFunc()
    {
        cout << "B::vFunc() is called!" << endl;
    }   
};

class C: public A{
public:
    virtual void vFunc()
    {
        cout << "C::vFunc() is called!" << endl;
    }   
};

class D: public B{
public:
    virtual void vFunc()
    {
        cout << "D::vFunc() is called!" << endl;
    }   
};

int main()
{
    A a;
    B b;
    C c;
    D d;
    A *pa = &a;
    B *pb = &b;
    C *pc = &c;
    D *pd = &d;

    cout << "pa->vFunc() \n";
    cout << "  ";
    pa->vFunc();

    cout << "pb->vFunc() \n";
    cout << "  ";
    pb->vFunc();

    cout << "pc->vFunc() \n";
    cout << "  ";
    pc->vFunc();

    cout << "pd->vFunc() \n";
    cout << "  ";
    pd->vFunc();
    
    cout << endl;
    pa = pb;
    cout << "pa = pb \npa->vFunc() \n";
    cout << "  ";
    pa->vFunc();

    cout << "pa = pc \npa->vFunc() \n";
    cout << "  ";
    pa = pc;
    pa->vFunc();

    cout << "pa = pd \npa->vFunc() \n";
    cout << "  ";
    pa = pd;
    pa->vFunc();


    return 0;
}

Output:

pa->vFunc() 
  A::vFunc() is called!
pb->vFunc() 
  B::vFunc() is called!
pc->vFunc() 
  C::vFunc() is called!
pd->vFunc() 
  D::vFunc() is called!

pa = pb 
pa->vFunc() 
  B::vFunc() is called!
pa = pc 
pa->vFunc() 
  C::vFunc() is called!
pa = pd 
pa->vFunc() 
  D::vFunc() is called!

Preparations: Properties of Monotone Sequences

Theorem A monotone sequence of real numbers converges if and only if it is bounded. Moreover,

  1. If is monotone increasing and bounded, then
  2. If is monotone decreasing and bounded, then
  3. If is monotone increasing and unbounded, then
  4. If is monotone decreasing and unbounded, then

Definitions

Definition Let be a sequence of real numbers, let , and , then and

Proposition Every bounded sequence has and , and the sequence converges if and only if .

In C++, the corresponding constructor would be called whenever an object is created. In this post, we deeply dive in the constructors.

Object Arrays

Sometimes, we want to create a static object array. One way to do it is to use object pointers. Are there any other ways to do it? Let’s consider the following simple example.

class Position{
    int x;
    int y;
public:
    Position()
    {
        x = 0;
        y = 0;
    }
    Position(int x_,int y_ = 0)
    {
        x = x_;
        y = y_;
    }
    void PrintXY()
    {
        cout << "x = " << x
             << ", y = " << y << endl;
    }
};

Take the following Position class as an example, you want to create an array of positions whose length is 3. Of course, you can do like that,

Position p1[3];

However, the above method can only work for all positions initializing as 0’s. If we want customized initialization, we could we achieve it. Actually, we can do like the following,

Position p2[3] = { {1,2},3,{0,4} };

By using the above method, you create three position objects, one is initialized as (1,2), one is (3,0), one is (0,4).